fix(fast_depends): keep positional args out of **kwargs when passed by name#2807
Open
obchain wants to merge 1 commit into
Open
fix(fast_depends): keep positional args out of **kwargs when passed by name#2807obchain wants to merge 1 commit into
obchain 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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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