fix(fast_depends): keep positional args out of **kwargs when passed by name#2807
fix(fast_depends): keep positional args out of **kwargs when passed by name#2807obchain wants to merge 1 commit into
Conversation
…y name
When a function had positional-or-keyword parameters alongside a **kwargs and
all arguments were supplied as keyword arguments (e.g. an LLM tool call that
delivers every argument by name), the positional parameter names were not
pulled out of the kwargs dict before the rest was assigned to var_keyword_arg.
The whole input — including arg1, arg2 — was then placed inside the kwargs
field, and pydantic validation reported the positional fields as missing:
Field required [type=missing, input_value={'kwargs': {'arg1': ...}}]
Pop the positional_arg names from kwargs first whenever var_keyword_arg is
set, mirroring the existing pop loop for keyword_args. The subsequent
positional-from-*args loop is unaffected: it only runs when args is supplied,
which is mutually exclusive with passing the same name as a keyword.
Add a regression test in both the sync and async test_cast suites.
Fixes ag2ai#1790
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 38 files with indirect coverage changes 🚀 New features to boost your workflow:
|
marklysze
left a comment
There was a problem hiding this comment.
Correct fix. When var_keyword_arg is present, positional-or-keyword params arriving as keyword args (the normal LLM tool-call delivery path) were falling through to kw[self.var_keyword_arg] = kwargs unextracted, so they'd land in **kwargs rather than the named parameter. The fix pulls them out before the remainder assignment, matching how var_positional_arg params are handled. Sync and async regression tests both added. Approved.
|
This is a duplicate of #2813 which was already merged to main ( |
Why are these changes needed?
When a function has positional-or-keyword parameters alongside
**kwargsand is invoked with all arguments supplied as keyword arguments — exactly what happens when an LLM tool call delivers every argument by name — the positional parameter names were swept into the**kwargsfield and pydantic reported them as missing:The root cause is in
autogen/fast_depends/core/model.py::CallModel._solve. The existing loop only poppedkeyword_argsout of the incomingkwargsdict before assigning the remainder tovar_keyword_arg;positional_argswere left in there and ended up nested inside the**kwargsvalue.Fix
When the function has a
**kwargs(self.var_keyword_arg is not None), pop thepositional_argsnames fromkwargstoo, mirroring the existing pop loop forkeyword_args. The subsequent positional-from-*argsloop is unaffected: it only runs whenargsis supplied, which is mutually exclusive with passing the same name as a keyword argument.Behaviour for purely positional invocation, for keyword-only params, for
*args, and for functions without**kwargsis unchanged — the new pop loop is gated onvar_keyword_arg is not None.Repro
Tests
uv run pytest test/fast_depends/ -q→ 131 passed (was 129). New regression tests:test/fast_depends/sync/test_cast.py::test_positional_args_passed_by_name_with_var_keywordtest/fast_depends/async/test_cast.py::test_positional_args_passed_by_name_with_var_keywordBoth invoke a function
(arg1, arg2, **kwargs)with every arg by name and assert the positional values land inarg1/arg2rather than insidekwargs.Related issue number
Fixes #1790
Checks
AI assistance