Skip to content

fix: filter **kwargs and *args from tool call schema generation#2619

Open
Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Ricardo-M-L:fix/kwargs-tool-schema-generation
Open

fix: filter **kwargs and *args from tool call schema generation#2619
Ricardo-M-L wants to merge 2 commits intoag2ai:mainfrom
Ricardo-M-L:fix/kwargs-tool-schema-generation

Conversation

@Ricardo-M-L
Copy link
Copy Markdown
Contributor

Summary

  • Filter VAR_KEYWORD (**kwargs) and VAR_POSITIONAL (*args) parameter kinds from tool schema generation in autogen/tools/function_utils.py
  • Fixed all 5 functions that iterate inspect.Signature.parameters: get_typed_signature(), get_param_annotations(), get_required_params(), get_default_values(), get_missing_annotations()
  • Added 12 comprehensive tests covering: kwargs-only functions, args+kwargs, regular params with kwargs, and regression tests for functions without variadic params

Why

When a function has **kwargs, AG2's tool schema generation treats kwargs as a regular required parameter. The LLM sees it in the schema and nests all arguments inside a kwargs field, causing Pydantic validation failure. See the issue for a detailed reproduction.

Fixes #1790

Test plan

  • All 12 new tests pass (test_get_typed_signature_filters_var_keyword, test_get_typed_signature_filters_var_positional, test_get_typed_signature_filters_both_var_positional_and_keyword, test_get_param_annotations_excludes_kwargs, test_get_required_params_excludes_kwargs, test_get_required_params_excludes_args, test_get_default_values_excludes_kwargs, test_get_missing_annotations_excludes_var_params, test_get_function_schema_with_kwargs_only, test_get_function_schema_with_regular_params_and_kwargs, test_get_function_schema_with_args_and_kwargs, test_get_function_schema_no_kwargs_regression)
  • All 27 pre-existing tests still pass (1 async test fails due to missing pytest-asyncio — pre-existing, unrelated)
  • Verified the exact reproduction case from the issue: hello_world_v3(arg1, arg2, **kwargs) now generates a schema without kwargs in properties or required

🤖 Generated with Claude Code

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 11, 2026

CLA assistant check
All committers have signed the CLA.

yuj and others added 2 commits April 24, 2026 14:45
When a function has `**kwargs` or `*args`, the tool schema generation
incorrectly treats them as regular parameters. This causes LLMs to nest
all arguments inside a `kwargs` field, leading to Pydantic validation
failures.

Filter VAR_KEYWORD and VAR_POSITIONAL parameter kinds in all five
functions that iterate over inspect.Signature.parameters:
- get_typed_signature()
- get_param_annotations()
- get_required_params()
- get_default_values()
- get_missing_annotations()

Fixes ag2ai#1790

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@Ricardo-M-L Ricardo-M-L force-pushed the fix/kwargs-tool-schema-generation branch from 10bcc92 to 3e25c69 Compare April 24, 2026 06:46
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 24, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
autogen/tools/function_utils.py 90.47% <100.00%> (ø)

... and 64 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Tool call doesn't work when **kwargs in function arguments

2 participants