Skip to content

Commit b36b598

Browse files
authored
Fix OTel for built-in tools returning a list (e.g. Anthropic web search) (#2959)
1 parent f5231e8 commit b36b598

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,11 +1161,7 @@ def otel_message_parts(self, settings: InstrumentationSettings) -> list[_otel_me
11611161
if settings.include_content and part.content is not None: # pragma: no branch
11621162
from .models.instrumented import InstrumentedModel
11631163

1164-
return_part['result'] = (
1165-
part.content
1166-
if isinstance(part.content, str)
1167-
else {k: InstrumentedModel.serialize_any(v) for k, v in part.content.items()}
1168-
)
1164+
return_part['result'] = InstrumentedModel.serialize_any(part.content)
11691165

11701166
parts.append(return_part)
11711167
return parts

tests/models/test_instrumented.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,22 @@ def test_message_with_builtin_tool_calls():
13591359
BuiltinToolCallPart('code_execution', {'code': '2 * 2'}, tool_call_id='tool_call_1'),
13601360
BuiltinToolReturnPart('code_execution', {'output': '4'}, tool_call_id='tool_call_1'),
13611361
TextPart('text2'),
1362+
BuiltinToolCallPart(
1363+
'web_search',
1364+
'{"query": "weather: San Francisco, CA", "type": "search"}',
1365+
tool_call_id='tool_call_2',
1366+
),
1367+
BuiltinToolReturnPart(
1368+
'web_search',
1369+
[
1370+
{
1371+
'url': 'https://www.weather.com/weather/today/l/USCA0987:1:US',
1372+
'title': 'Weather in San Francisco',
1373+
}
1374+
],
1375+
tool_call_id='tool_call_2',
1376+
),
1377+
TextPart('text3'),
13621378
]
13631379
),
13641380
]
@@ -1387,6 +1403,26 @@ def test_message_with_builtin_tool_calls():
13871403
'result': {'output': '4'},
13881404
},
13891405
{'type': 'text', 'content': 'text2'},
1406+
{
1407+
'type': 'tool_call',
1408+
'id': 'tool_call_2',
1409+
'name': 'web_search',
1410+
'builtin': True,
1411+
'arguments': '{"query": "weather: San Francisco, CA", "type": "search"}',
1412+
},
1413+
{
1414+
'type': 'tool_call_response',
1415+
'id': 'tool_call_2',
1416+
'name': 'web_search',
1417+
'builtin': True,
1418+
'result': [
1419+
{
1420+
'url': 'https://www.weather.com/weather/today/l/USCA0987:1:US',
1421+
'title': 'Weather in San Francisco',
1422+
}
1423+
],
1424+
},
1425+
{'type': 'text', 'content': 'text3'},
13901426
],
13911427
}
13921428
]

0 commit comments

Comments
 (0)