Skip to content

Commit 5b94841

Browse files
authored
Record tool response in tool run span (#2109)
1 parent 95f6ce3 commit 5b94841

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

pydantic_ai_slim/pydantic_ai/tools.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'ToolDefinition',
3333
)
3434

35+
from .messages import ToolReturnPart
3536

3637
ToolParams = ParamSpec('ToolParams', default=...)
3738
"""Retrieval function param spec."""
@@ -346,15 +347,31 @@ async def run(
346347
{
347348
'type': 'object',
348349
'properties': {
349-
**({'tool_arguments': {'type': 'object'}} if include_content else {}),
350+
**(
351+
{
352+
'tool_arguments': {'type': 'object'},
353+
'tool_response': {'type': 'object'},
354+
}
355+
if include_content
356+
else {}
357+
),
350358
'gen_ai.tool.name': {},
351359
'gen_ai.tool.call.id': {},
352360
},
353361
}
354362
),
355363
}
356-
with tracer.start_as_current_span('running tool', attributes=span_attributes):
357-
return await self._run(message, run_context)
364+
with tracer.start_as_current_span('running tool', attributes=span_attributes) as span:
365+
response = await self._run(message, run_context)
366+
if include_content and span.is_recording():
367+
span.set_attribute(
368+
'tool_response',
369+
response.model_response_str()
370+
if isinstance(response, ToolReturnPart)
371+
else response.model_response(),
372+
)
373+
374+
return response
358375

359376
async def _run(
360377
self, message: _messages.ToolCallPart, run_context: RunContext[AgentDepsT]

tests/test_logfire.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,46 @@ async def add_numbers(x: int, y: int) -> int:
554554
]
555555

556556
if include_content:
557-
assert tool_attributes['tool_arguments'] == snapshot('{"x":42,"y":42}')
557+
assert tool_attributes == snapshot(
558+
{
559+
'gen_ai.tool.name': 'add_numbers',
560+
'gen_ai.tool.call.id': IsStr(),
561+
'tool_arguments': '{"x":42,"y":42}',
562+
'tool_response': '84',
563+
'logfire.msg': 'running tool: add_numbers',
564+
'logfire.json_schema': IsJson(
565+
snapshot(
566+
{
567+
'type': 'object',
568+
'properties': {
569+
'tool_arguments': {'type': 'object'},
570+
'tool_response': {'type': 'object'},
571+
'gen_ai.tool.name': {},
572+
'gen_ai.tool.call.id': {},
573+
},
574+
}
575+
)
576+
),
577+
'logfire.span_type': 'span',
578+
}
579+
)
558580
else:
559-
assert 'tool_arguments' not in tool_attributes
581+
assert tool_attributes == snapshot(
582+
{
583+
'gen_ai.tool.name': 'add_numbers',
584+
'gen_ai.tool.call.id': IsStr(),
585+
'logfire.msg': 'running tool: add_numbers',
586+
'logfire.json_schema': IsJson(
587+
snapshot(
588+
{
589+
'type': 'object',
590+
'properties': {
591+
'gen_ai.tool.name': {},
592+
'gen_ai.tool.call.id': {},
593+
},
594+
}
595+
)
596+
),
597+
'logfire.span_type': 'span',
598+
}
599+
)

0 commit comments

Comments
 (0)