@@ -1583,6 +1583,74 @@ def kwargs_collision_function(x: int, *rest: int, **kw: Any) -> str:
15831583 return f"x={ x } rest={ rest } kw={ kw } "
15841584
15851585
1586+ @pytest .mark .asyncio
1587+ @pytest .mark .parametrize ("payload" , [{"a" : 1 , "b" : "x" }, {"kwargs" : {"a" : 1 }, "b" : "x" }])
1588+ async def test_kwargs_tool_rejects_flat_arguments_before_invocation (payload ):
1589+ calls = []
1590+
1591+ def collect (** kwargs ):
1592+ calls .append (kwargs )
1593+ return kwargs
1594+
1595+ tool = function_tool (collect , strict_mode = False , failure_error_function = None )
1596+ arguments = json .dumps (payload )
1597+ context = ToolContext (None , tool_name = tool .name , tool_call_id = "1" , tool_arguments = arguments )
1598+
1599+ with pytest .raises (ModelBehaviorError , match = "Invalid JSON input for tool collect" ):
1600+ await tool .on_invoke_tool (context , arguments )
1601+
1602+ assert calls == []
1603+ assert tool .params_json_schema ["additionalProperties" ] is False
1604+ assert tool .params_json_schema ["properties" ]["kwargs" ]["additionalProperties" ] is True
1605+
1606+
1607+ @pytest .mark .asyncio
1608+ @pytest .mark .parametrize (
1609+ "payload, expected" , [({"kwargs" : {"a" : 1 , "b" : "x" }}, {"a" : 1 , "b" : "x" }), ({}, {})]
1610+ )
1611+ async def test_kwargs_tool_preserves_nested_arguments_and_empty_default (payload , expected ):
1612+ def collect (** kwargs ):
1613+ return kwargs
1614+
1615+ tool = function_tool (collect , strict_mode = False , failure_error_function = None )
1616+ arguments = json .dumps (payload )
1617+ context = ToolContext (None , tool_name = tool .name , tool_call_id = "1" , tool_arguments = arguments )
1618+
1619+ assert await tool .on_invoke_tool (context , arguments ) == expected
1620+
1621+
1622+ @pytest .mark .asyncio
1623+ async def test_kwargs_tool_preserves_named_arguments_and_typed_nested_values ():
1624+ calls = []
1625+
1626+ async def collect (label : str , ** options : int ):
1627+ calls .append ((label , options ))
1628+ return options
1629+
1630+ tool = function_tool (collect , strict_mode = False , failure_error_function = None )
1631+ arguments = '{"label": "sample", "options": {"a": 1}}'
1632+ context = ToolContext (None , tool_name = tool .name , tool_call_id = "1" , tool_arguments = arguments )
1633+ assert await tool .on_invoke_tool (context , arguments ) == {"a" : 1 }
1634+ assert calls == [("sample" , {"a" : 1 })]
1635+
1636+ for arguments in (
1637+ '{"label": "sample", "a": 1}' ,
1638+ '{"label": "sample", "options": {"a": "invalid"}}' ,
1639+ ):
1640+ context = ToolContext (None , tool_name = tool .name , tool_call_id = "2" , tool_arguments = arguments )
1641+ with pytest .raises (ModelBehaviorError ):
1642+ await tool .on_invoke_tool (context , arguments )
1643+ assert calls == [("sample" , {"a" : 1 })]
1644+
1645+
1646+ def test_kwargs_tool_strict_error_explains_non_strict_nested_input ():
1647+ def collect (** options : Any ):
1648+ return options
1649+
1650+ with pytest .raises (UserError , match = r"\*\*options.*strict_mode=False.*nested.*options" ):
1651+ function_tool (collect )
1652+
1653+
15861654@pytest .mark .asyncio
15871655async def test_kwargs_key_colliding_with_param_is_reported_as_model_behavior_error ():
15881656 """The collision reaches the model as feedback, not as an unhandled TypeError.
0 commit comments