Skip to content

Commit 9ca4bca

Browse files
authored
Fix include_content not working as expected (#2206)
1 parent 7d50564 commit 9ca4bca

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

pydantic_ai_slim/pydantic_ai/agent.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -843,14 +843,15 @@ async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
843843
agent_run = AgentRun(graph_run)
844844
yield agent_run
845845
if (final_result := agent_run.result) is not None and run_span.is_recording():
846-
run_span.set_attribute(
847-
'final_result',
848-
(
849-
final_result.output
850-
if isinstance(final_result.output, str)
851-
else json.dumps(InstrumentedModel.serialize_any(final_result.output))
852-
),
853-
)
846+
if instrumentation_settings and instrumentation_settings.include_content:
847+
run_span.set_attribute(
848+
'final_result',
849+
(
850+
final_result.output
851+
if isinstance(final_result.output, str)
852+
else json.dumps(InstrumentedModel.serialize_any(final_result.output))
853+
),
854+
)
854855
finally:
855856
try:
856857
if instrumentation_settings and run_span.is_recording():

pydantic_ai_slim/pydantic_ai/models/instrumented.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ def messages_to_otel_events(self, messages: list[ModelMessage]) -> list[Event]:
156156
events: list[Event] = []
157157
instructions = InstrumentedModel._get_instructions(messages) # pyright: ignore [reportPrivateUsage]
158158
if instructions is not None:
159-
events.append(Event('gen_ai.system.message', body={'content': instructions, 'role': 'system'}))
159+
events.append(
160+
Event(
161+
'gen_ai.system.message',
162+
body={**({'content': instructions} if self.include_content else {}), 'role': 'system'},
163+
)
164+
)
160165

161166
for message_index, message in enumerate(messages):
162167
message_events: list[Event] = []

tests/test_logfire.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,6 @@ class MyOutput:
448448
snapshot(
449449
[
450450
{
451-
'content': 'Here are some instructions',
452451
'role': 'system',
453452
'event.name': 'gen_ai.system.message',
454453
},
@@ -480,7 +479,6 @@ class MyOutput:
480479
]
481480
)
482481
),
483-
'final_result': '{"content": "a"}',
484482
'logfire.json_schema': IsJson(
485483
snapshot(
486484
{
@@ -497,7 +495,6 @@ class MyOutput:
497495
snapshot(
498496
[
499497
{
500-
'content': 'Here are some instructions',
501498
'role': 'system',
502499
'gen_ai.system': 'test',
503500
'event.name': 'gen_ai.system.message',

0 commit comments

Comments
 (0)