Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 26 additions & 7 deletions pydantic_ai_slim/pydantic_ai/agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,21 @@ async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
finally:
try:
if instrumentation_settings and run_span.is_recording():
run_span.set_attributes(self._run_span_end_attributes(state, usage, instrumentation_settings))
run_span.set_attributes(
self._run_span_end_attributes(
state, usage, instrumentation_settings, graph_deps.new_message_index
)
)
finally:
run_span.end()

def _run_span_end_attributes(
self, state: _agent_graph.GraphAgentState, usage: _usage.RunUsage, settings: InstrumentationSettings
self,
state: _agent_graph.GraphAgentState,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only use state.message_history, lets send in just that, so that the compatibility between that list and new_message_index are clear.

usage: _usage.RunUsage,
settings: InstrumentationSettings,
new_message_index: int,
):
literal_instructions, _ = self._get_instructions()

if settings.version == 1:
attrs = {
'all_messages_events': json.dumps(
Expand All @@ -704,19 +710,32 @@ def _run_span_end_attributes(
)
}
else:
attrs = {
# Store the last instructions here for convenience
last_instructions = InstrumentedModel._get_instructions(state.message_history) # pyright: ignore[reportPrivateUsage]
attrs: dict[str, Any] = {
'pydantic_ai.all_messages': json.dumps(settings.messages_to_otel_messages(list(state.message_history))),
**settings.system_instructions_attributes(literal_instructions),
**settings.system_instructions_attributes(last_instructions),
}

# Store an attribute that indicates that the instructions from this agent run were not always the same
# This can signal to an observability UI that different steps in the agent run had different instructions
# Note: We purposely only look at "new" messages because they are the only ones produced by this agent run.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also store the new_message_index so that the UI can show which messages are from older agent runs? Similar to how I think we should show the ModelResponse.model_name if it's not the same between different messages

for m in state.message_history[new_message_index:]:
if (
isinstance(m, _messages.ModelRequest)
and m.instructions is not None
and m.instructions != last_instructions
):
attrs['pydantic_ai.variable_instructions'] = True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify this into an any()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this suggestion

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmontagu attrs['pydantic_ai.variable_instructions'] = any(isinstance(m, _messages.ModelRequest) and m.instructions is not None and m.instructions != last_instructions for m in state.message_history[new_message_index:])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, sure


return {
**usage.opentelemetry_attributes(),
**attrs,
'logfire.json_schema': json.dumps(
{
'type': 'object',
'properties': {
**{attr: {'type': 'array'} for attr in attrs.keys()},
**{k: {'type': 'array'} if isinstance(v, str) else {} for k, v in attrs.items()},
'final_result': {'type': 'object'},
},
}
Expand Down
Loading