Skip to content

Commit a9b1ce7

Browse files
fix(core): reject silently discarded kwargs tool arguments (#5174)
fix: Reject silently discarded kwargs tool arguments
1 parent f9108bd commit a9b1ce7

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

‎src/agents/function_schema.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# griffelib exposes the `griffe` package at runtime but currently does not ship typing markers.
1212
from griffe import Docstring, DocstringSectionKind # type: ignore[import-untyped]
13-
from pydantic import BaseModel, Field, create_model
13+
from pydantic import BaseModel, ConfigDict, Field, create_model
1414
from pydantic.fields import FieldInfo
1515

1616
from .exceptions import ModelBehaviorError, UserError
@@ -421,6 +421,7 @@ def function_schema(
421421
# We will collect field definitions for create_model as a dict:
422422
# field_name -> (type_annotation, default_value_or_Field(...))
423423
fields: dict[str, Any] = {}
424+
model_config = ConfigDict()
424425

425426
for name, param in filtered_params:
426427
ann = type_hints.get(name, param.annotation)
@@ -469,6 +470,15 @@ def function_schema(
469470
)
470471

471472
elif param.kind == param.VAR_KEYWORD:
473+
if strict_json_schema:
474+
raise UserError(
475+
f"Variadic keyword parameter `**{name}` in function {func.__name__} cannot"
476+
" use a strict schema. Set strict_mode=False on the function tool"
477+
" (or strict_json_schema=False on function_schema) and pass keyword arguments"
478+
f" in the nested `{name}` object, or use explicit parameters for a strict tool."
479+
)
480+
# Reject flat keyword arguments instead of silently dropping them before invocation.
481+
model_config["extra"] = "forbid"
472482
# **kwargs handling: a ``**kwargs: X`` annotation applies to each keyword *value*
473483
# (PEP 484), so the collected container is always ``dict[str, X]``. Preserve the full
474484
# annotation as the value type -- mirroring the variadic-positional handling above,
@@ -518,7 +528,9 @@ def function_schema(
518528
)
519529

520530
# 3. Dynamically build a Pydantic model
521-
dynamic_model = create_model(f"{func_name}_args", __base__=BaseModel, **fields)
531+
dynamic_model = create_model(
532+
f"{func_name}_args", __base__=BaseModel, __config__=model_config, **fields
533+
)
522534

523535
# 4. Build JSON schema from that model
524536
json_schema = dynamic_model.model_json_schema()

‎tests/test_function_tool.py‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
15871655
async 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

Comments
 (0)