-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Record instructions on the agent run span even when they are dynamic #3131
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| usage: _usage.RunUsage, | ||
| settings: InstrumentationSettings, | ||
| new_message_index: int, | ||
| ): | ||
| literal_instructions, _ = self._get_instructions() | ||
|
|
||
| if settings.version == 1: | ||
| attrs = { | ||
| 'all_messages_events': json.dumps( | ||
|
|
@@ -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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also store the |
||
| 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 | ||
|
||
|
|
||
| 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'}, | ||
| }, | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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 andnew_message_indexare clear.