Skip to content

Commit 074b847

Browse files
authored
fix: Allow empty tool call args (#1407)
1 parent d676f16 commit 074b847

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

pydantic_ai_slim/pydantic_ai/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ async def _run(
333333
) -> _messages.ToolReturnPart | _messages.RetryPromptPart:
334334
try:
335335
if isinstance(message.args, str):
336-
args_dict = self._validator.validate_json(message.args)
336+
args_dict = self._validator.validate_json(message.args or '{}')
337337
else:
338338
args_dict = self._validator.validate_python(message.args)
339339
except ValidationError as e:

tests/test_tools.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,18 @@ async def call_tools_first(messages: list[ModelMessage], info: AgentInfo) -> Mod
829829
ToolCallPart(tool_name='my_tool', args={'a': 13, 'b': 4}),
830830
ToolCallPart(tool_name='my_tool_plain', args={'b': 17}),
831831
ToolCallPart(tool_name='my_tool_plain', args={'a': 4, 'b': 17}),
832+
ToolCallPart(tool_name='no_args_tool', args=''),
832833
]
833834
)
834835
else:
835836
return ModelResponse(parts=[TextPart('finished')])
836837

837838
agent = Agent(FunctionModel(call_tools_first))
838839

840+
@agent.tool_plain
841+
def no_args_tool() -> None:
842+
return None
843+
839844
@agent.tool
840845
def my_tool(ctx: RunContext[None], a: int, b: int = 2) -> int:
841846
return a + b
@@ -858,9 +863,10 @@ def my_tool_plain(*, a: int = 3, b: int) -> int:
858863
{'a': 13, 'b': 4},
859864
{'b': 17},
860865
{'a': 4, 'b': 17},
866+
'',
861867
]
862868
)
863-
assert tool_returns == snapshot([15, 17, 51, 68])
869+
assert tool_returns == snapshot([15, 17, 51, 68, None])
864870

865871

866872
def test_schema_generator():

0 commit comments

Comments
 (0)