Skip to content

Include content.1571 #2180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ class UserPromptPart:
"""Part type identifier, this is available on all parts as a discriminator."""

def otel_event(self, settings: InstrumentationSettings) -> Event:
content: str | list[dict[str, Any] | str]
content: str | list[dict[str, Any] | str] | dict[str, Any]
if isinstance(self.content, str):
content = self.content
content = self.content if settings.include_content else {'kind': 'text'}
else:
content = []
for part in self.content:
Expand Down Expand Up @@ -743,7 +743,7 @@ def new_event_body():
'type': 'function', # TODO https://github.com/pydantic/pydantic-ai/issues/888
'function': {
'name': part.tool_name,
'arguments': part.args,
**({'arguments': part.args} if settings.include_content else {}),
},
}
)
Expand Down
9 changes: 8 additions & 1 deletion tests/models/test_instrumented.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ def test_messages_without_content(document_content: BinaryContent):
ModelRequest(parts=[ToolReturnPart('tool', 'tool_return_content', 'tool_call_1')]),
ModelRequest(parts=[RetryPromptPart('retry_prompt', tool_name='tool', tool_call_id='tool_call_2')]),
ModelRequest(parts=[UserPromptPart(content=['user_prompt2', document_content])]),
ModelRequest(parts=[UserPromptPart('simple text prompt')]),
]
settings = InstrumentationSettings(include_content=False)
assert [InstrumentedModel.event_to_dict(e) for e in settings.messages_to_otel_events(messages)] == snapshot(
Expand Down Expand Up @@ -896,7 +897,7 @@ def test_messages_without_content(document_content: BinaryContent):
{
'id': IsStr(),
'type': 'function',
'function': {'name': 'my_tool', 'arguments': {'a': 13, 'b': 4}},
'function': {'name': 'my_tool'},
}
],
'gen_ai.message.index': 3,
Expand All @@ -922,5 +923,11 @@ def test_messages_without_content(document_content: BinaryContent):
'gen_ai.message.index': 6,
'event.name': 'gen_ai.user.message',
},
{
'content': {'kind': 'text'},
'role': 'user',
'gen_ai.message.index': 7,
'event.name': 'gen_ai.user.message',
},
]
)
108 changes: 108 additions & 0 deletions tests/test_logfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,114 @@ class MyOutput:
)


@pytest.mark.skipif(not logfire_installed, reason='logfire not installed')
def test_instructions_with_structured_output_exclude_content(get_logfire_summary: Callable[[], LogfireSummary]) -> None:
@dataclass
class MyOutput:
content: str

settings: InstrumentationSettings = InstrumentationSettings(include_content=False)

my_agent = Agent(model=TestModel(), instructions='Here are some instructions', instrument=settings)

result = my_agent.run_sync('Hello', output_type=MyOutput)
assert result.output == snapshot(MyOutput(content='a'))

summary = get_logfire_summary()
assert summary.attributes[0] == snapshot(
{
'model_name': 'test',
'agent_name': 'my_agent',
'logfire.msg': 'my_agent run',
'logfire.span_type': 'span',
'gen_ai.usage.input_tokens': 51,
'gen_ai.usage.output_tokens': 5,
'all_messages_events': IsJson(
snapshot(
[
{
'content': 'Here are some instructions',
'role': 'system',
'event.name': 'gen_ai.system.message',
},
{
'content': {'kind': 'text'},
'role': 'user',
'gen_ai.message.index': 0,
'event.name': 'gen_ai.user.message',
},
{
'role': 'assistant',
'tool_calls': [
{
'id': IsStr(),
'type': 'function',
'function': {'name': 'final_result'},
}
],
'gen_ai.message.index': 1,
'event.name': 'gen_ai.assistant.message',
},
{
'role': 'tool',
'id': IsStr(),
'name': 'final_result',
'gen_ai.message.index': 2,
'event.name': 'gen_ai.tool.message',
},
]
)
),
'final_result': '{"content": "a"}',
'logfire.json_schema': IsJson(
snapshot(
{
'type': 'object',
'properties': {'all_messages_events': {'type': 'array'}, 'final_result': {'type': 'object'}},
}
)
),
}
)
chat_span_attributes = summary.attributes[1]
assert chat_span_attributes['events'] == snapshot(
IsJson(
snapshot(
[
{
'content': 'Here are some instructions',
'role': 'system',
'gen_ai.system': 'test',
'event.name': 'gen_ai.system.message',
},
{
'event.name': 'gen_ai.user.message',
'content': {'kind': 'text'},
'role': 'user',
'gen_ai.message.index': 0,
'gen_ai.system': 'test',
},
{
'event.name': 'gen_ai.choice',
'index': 0,
'message': {
'role': 'assistant',
'tool_calls': [
{
'id': IsStr(),
'type': 'function',
'function': {'name': 'final_result'},
}
],
},
'gen_ai.system': 'test',
},
]
)
)
)


def test_instrument_all():
model = TestModel()
agent = Agent()
Expand Down