Skip to content

Commit 84361ab

Browse files
committed
Address Douwe's feedback
1 parent 02f0eba commit 84361ab

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -687,46 +687,50 @@ async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
687687
if instrumentation_settings and run_span.is_recording():
688688
run_span.set_attributes(
689689
self._run_span_end_attributes(
690-
state, usage, instrumentation_settings, graph_deps.new_message_index
690+
instrumentation_settings, usage, state.message_history, graph_deps.new_message_index
691691
)
692692
)
693693
finally:
694694
run_span.end()
695695

696696
def _run_span_end_attributes(
697697
self,
698-
state: _agent_graph.GraphAgentState,
699-
usage: _usage.RunUsage,
700698
settings: InstrumentationSettings,
699+
usage: _usage.RunUsage,
700+
message_history: list[_messages.ModelMessage],
701701
new_message_index: int,
702702
):
703703
if settings.version == 1:
704704
attrs = {
705705
'all_messages_events': json.dumps(
706-
[
707-
InstrumentedModel.event_to_dict(e)
708-
for e in settings.messages_to_otel_events(state.message_history)
709-
]
706+
[InstrumentedModel.event_to_dict(e) for e in settings.messages_to_otel_events(message_history)]
710707
)
711708
}
712709
else:
713710
# Store the last instructions here for convenience
714-
last_instructions = InstrumentedModel._get_instructions(state.message_history) # pyright: ignore[reportPrivateUsage]
711+
last_instructions = InstrumentedModel._get_instructions(message_history) # pyright: ignore[reportPrivateUsage]
715712
attrs: dict[str, Any] = {
716-
'pydantic_ai.all_messages': json.dumps(settings.messages_to_otel_messages(list(state.message_history))),
713+
'pydantic_ai.all_messages': json.dumps(settings.messages_to_otel_messages(list(message_history))),
717714
**settings.system_instructions_attributes(last_instructions),
718715
}
719716

720-
# Store an attribute that indicates that the instructions from this agent run were not always the same
721-
# This can signal to an observability UI that different steps in the agent run had different instructions
717+
# If this agent run was provided with existing history, store an attribute indicating the point at which the
718+
# new messages begin.
719+
if new_message_index > 0:
720+
attrs['pydantic_ai.new_message_index'] = new_message_index
721+
722+
# If the instructions for this agent run were not always the same, store an attribute that indicates that.
723+
# This can signal to an observability UI that different steps in the agent run had different instructions.
722724
# Note: We purposely only look at "new" messages because they are the only ones produced by this agent run.
723-
for m in state.message_history[new_message_index:]:
724-
if (
725+
if any(
726+
(
725727
isinstance(m, _messages.ModelRequest)
726728
and m.instructions is not None
727729
and m.instructions != last_instructions
728-
):
729-
attrs['pydantic_ai.variable_instructions'] = True
730+
)
731+
for m in message_history[new_message_index:]
732+
):
733+
attrs['pydantic_ai.variable_instructions'] = True
730734

731735
return {
732736
**usage.opentelemetry_attributes(),

tests/test_logfire.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,13 +2846,15 @@ def instructions(ctx: RunContext[None]):
28462846
]
28472847
)
28482848
),
2849+
'pydantic_ai.new_message_index': 2,
28492850
'gen_ai.system_instructions': '[{"type": "text", "content": "Instructions for the current agent run"}]',
28502851
'logfire.json_schema': IsJson(
28512852
snapshot(
28522853
{
28532854
'type': 'object',
28542855
'properties': {
28552856
'pydantic_ai.all_messages': {'type': 'array'},
2857+
'pydantic_ai.new_message_index': {},
28562858
'gen_ai.system_instructions': {'type': 'array'},
28572859
'final_result': {'type': 'object'},
28582860
},

0 commit comments

Comments
 (0)