Skip to content

**kwargs tools nest under kwargs; flat model args silently invoke with empty kwargs #5170

Closed

Description

@michaelkillgta

Describe the bug

@function_tool on a **kwargs function (with strict_mode=False) builds a schema with a nested kwargs object property. Models typically emit flat argument objects ({\"a\": 1, \"b\": \"x\"}). Those are accepted by the Pydantic wrapper but the tool is invoked with empty kwargs (got=[]), silently dropping the payload.

Nested {\"kwargs\": {\"a\": 1, \"b\": \"x\"}} works.

Default strict_mode=True raises UserError about additionalProperties with no hint about strict_mode=False / Unpack.

Distinct from #4859 (PEP 692 Unpack) and #4669 (kwargs key collision in to_call_args).

To Reproduce

from agents import function_tool

@function_tool(strict_mode=False)
def with_kwargs(**kwargs) -> str:
    \"\"\"Collect kwargs.\"\"\"
    return f\"got={sorted(kwargs.items())}\"

# Schema shape:
# {\"properties\": {\"kwargs\": {\"additionalProperties\": true, \"type\": \"object\"}}, ...}

# Nested invoke → got=[('a', 1), ('b', 'x')]
# Flat invoke {\"a\": 1, \"b\": \"x\"} → got=[]   # silent data loss

Expected behavior

Non-strict **kwargs tools should expose open object parameters the way models emit them (root additionalProperties, flat keys), or reject flat payloads instead of calling with {}.

Environment

  • openai-agents==0.22.3
  • Python 3.13

Additional context

Reproduced offline with ScriptedModel.

Activity

  1. goran-revops commented on Sep 25, 2026

    @goran-revops

    Reproduced on openai-agents 0.22.3.

    function_schema turns **kwargs into a nested object property named after the parameter. With strict_mode=False the model is a normal Pydantic model, so extra keys are ignored. to_call_args then reads that empty nested dict and calls the function with no keyword arguments. The tool returns success and the payload is gone.

    {"a": 1, "b": "x"} -> got=[]
    {"kwargs": {"a": 1, "b": "x"}} -> got=[('a', 1), ('b', 'x')]

    With the default strict schema, decoration raises UserError: "additionalProperties should not be set for object types..." That comes from ensure_strict_json_schema seeing the open object inside the nested property. The message points at an old Pydantic version. The cause is **kwargs.

    Fix, in src/agents/function_schema.py:

    1. Non-strict. In the VAR_KEYWORD branch, do not add a field named kwargs. Build the args model with extra="allow" so named parameters stay properties and any other key is kept. Put additionalProperties on the root schema, not under a kwargs property. A function def f(a: int, **kwargs) should accept {"a": 1, "b": "x"} and bind b into kwargs.

    2. Call path. In to_call_args, for VAR_KEYWORD, read those extra keys (model_extra / __pydantic_extra__) instead of instance_values["kwargs"]. Run the existing _raise_on_var_keyword_collisions check on that dict, then splat it. If the payload is only the old nested object, {"kwargs": {"a": 1, "b": "x"}}, unwrap that dict so current callers keep working. If flat keys and the nested object are both present, reject the call.

    3. Strict. Before ensure_strict_json_schema, if the function has **kwargs and strict_json_schema is true, raise UserError that says a strict schema cannot take undeclared keys, and that strict_mode=False is the way through. Do not let it fall through to the additionalProperties error.

    A test should cover the flat call, the old nested call, a named parameter plus extra keys, and the strict error.

  2. michaelkillgta commented on Sep 25, 2026

    @michaelkillgta
    Author

    Thanks @goran-revops — your 0.22.3 repro and the flat-vs-nested breakdown match what I saw. The three-part fix (non-strict extra="allow" + read extras in to_call_args with nested unwrap + explicit strict UserError for **kwargs) looks right to me. I’ll wait on maintainer direction before opening a PR so we don’t collide.

  3. added this to the 0.22.x milestone on Sep 25, 2026
  4. michaelkillgta commented on Sep 25, 2026

    @michaelkillgta
    Author

    Thanks @jbeckwith-oai — appreciate the fix in #5174.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions