Skip to content

Commit e03c118

Browse files
[bugfix] Handle empty args for ToolCallPart (#1515)
1 parent b94ee19 commit e03c118

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ def args_as_dict(self) -> dict[str, Any]:
508508
"""
509509
if isinstance(self.args, dict):
510510
return self.args
511+
if isinstance(self.args, str) and not self.args:
512+
return {}
511513
args = pydantic_core.from_json(self.args)
512514
assert isinstance(args, dict), 'args should be a dict'
513515
return cast(dict[str, Any], args)

tests/test_tools.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,20 @@ def test_init_plain_tool_invalid():
549549
Tool(ctx_tool, takes_ctx=False)
550550

551551

552+
@pytest.mark.parametrize(
553+
'args, expected',
554+
[
555+
('', {}),
556+
({'x': 42, 'y': 'value'}, {'x': 42, 'y': 'value'}),
557+
('{"a": 1, "b": "c"}', {'a': 1, 'b': 'c'}),
558+
],
559+
)
560+
def test_tool_call_part_args_as_dict(args: Union[str, dict[str, Any]], expected: dict[str, Any]):
561+
part = ToolCallPart(tool_name='foo', args=args)
562+
result = part.args_as_dict()
563+
assert result == expected
564+
565+
552566
def test_return_pydantic_model():
553567
agent = Agent('test')
554568

0 commit comments

Comments
 (0)