Skip to content

Record tool response in tool run span #2109

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

Merged
merged 1 commit into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 20 additions & 3 deletions pydantic_ai_slim/pydantic_ai/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'ToolDefinition',
)

from .messages import ToolReturnPart

ToolParams = ParamSpec('ToolParams', default=...)
"""Retrieval function param spec."""
Expand Down Expand Up @@ -346,15 +347,31 @@ async def run(
{
'type': 'object',
'properties': {
**({'tool_arguments': {'type': 'object'}} if include_content else {}),
**(
{
'tool_arguments': {'type': 'object'},
'tool_response': {'type': 'object'},
}
if include_content
else {}
),
'gen_ai.tool.name': {},
'gen_ai.tool.call.id': {},
},
}
),
}
with tracer.start_as_current_span('running tool', attributes=span_attributes):
return await self._run(message, run_context)
with tracer.start_as_current_span('running tool', attributes=span_attributes) as span:
response = await self._run(message, run_context)
if include_content and span.is_recording():
span.set_attribute(
'tool_response',
response.model_response_str()
if isinstance(response, ToolReturnPart)
else response.model_response(),
)

return response

async def _run(
self, message: _messages.ToolCallPart, run_context: RunContext[AgentDepsT]
Expand Down
44 changes: 42 additions & 2 deletions tests/test_logfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,46 @@ async def add_numbers(x: int, y: int) -> int:
]

if include_content:
assert tool_attributes['tool_arguments'] == snapshot('{"x":42,"y":42}')
assert tool_attributes == snapshot(
{
'gen_ai.tool.name': 'add_numbers',
'gen_ai.tool.call.id': IsStr(),
'tool_arguments': '{"x":42,"y":42}',
'tool_response': '84',
'logfire.msg': 'running tool: add_numbers',
'logfire.json_schema': IsJson(
snapshot(
{
'type': 'object',
'properties': {
'tool_arguments': {'type': 'object'},
'tool_response': {'type': 'object'},
'gen_ai.tool.name': {},
'gen_ai.tool.call.id': {},
},
}
)
),
'logfire.span_type': 'span',
}
)
else:
assert 'tool_arguments' not in tool_attributes
assert tool_attributes == snapshot(
{
'gen_ai.tool.name': 'add_numbers',
'gen_ai.tool.call.id': IsStr(),
'logfire.msg': 'running tool: add_numbers',
'logfire.json_schema': IsJson(
snapshot(
{
'type': 'object',
'properties': {
'gen_ai.tool.name': {},
'gen_ai.tool.call.id': {},
},
}
)
),
'logfire.span_type': 'span',
}
)