Skip to content

Commit bbd4409

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

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

pydantic_ai_slim/pydantic_ai/agent/__init__.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -687,46 +687,46 @@ 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))),
714+
'pydantic_ai.new_message_index': new_message_index,
717715
**settings.system_instructions_attributes(last_instructions),
718716
}
719717

720-
# Store an attribute that indicates that the instructions from this agent run were not always the same
718+
# If the instructions for this agent run were not always the same, store an attribute that indicates that.
721719
# This can signal to an observability UI that different steps in the agent run had different instructions
722720
# 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 (
721+
if any(
722+
(
725723
isinstance(m, _messages.ModelRequest)
726724
and m.instructions is not None
727725
and m.instructions != last_instructions
728-
):
729-
attrs['pydantic_ai.variable_instructions'] = True
726+
)
727+
for m in message_history[new_message_index:]
728+
):
729+
attrs['pydantic_ai.variable_instructions'] = True
730730

731731
return {
732732
**usage.opentelemetry_attributes(),

0 commit comments

Comments
 (0)