Python: fix: preserve explicit null tool arguments#5997
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a Python tool-invocation bug where explicitly provided null/None arguments were being dropped after Pydantic validation, causing required-but-nullable tool parameters to be omitted (notably in auto function-calling flows).
Changes:
- Introduces
_dump_tool_arguments()to keepexclude_none=Truebehavior while re-including explicitly-suppliedNonefields. - Updates
FunctionTool.invoke()and the auto function-call execution path to use_dump_tool_arguments(). - Adds regression tests covering both direct
FunctionTool.invoke()and automatic function-call execution.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_tools.py | Adds _dump_tool_arguments() and uses it in tool invocation and auto function-call execution to preserve explicit null arguments. |
| python/packages/core/tests/core/test_tools.py | Adds a regression test ensuring direct invoke() preserves explicitly provided None for a required nullable parameter. |
| python/packages/core/tests/core/test_function_invocation_logic.py | Adds a regression test ensuring auto function calling preserves null arguments from model-generated function calls. |
| for name in include_none_from if include_none_from is not None else model.model_fields_set: | ||
| if getattr(model, name, None) is None: |
|
Thanks for working on this, but we're going with #5944 - same root cause, but it handles the fix recursively, which matters since exclude_none=True itself recurses (nested cases like options.unit = null would still be dropped here). Also worth flagging for next time: the getattr(model, name, None) is None check (Copilot caught this too) will re-inject any unknown key as None, since getattr returns None for missing attributes, so extra keys get forwarded as unexpected kwargs. Iterating model.model_fields_set directly avoids that. Appreciate the effort as the diagnosis was spot on. Closing in favor of #5944. |
Summary
nulltool arguments after Pydantic validationFunctionTool.invoke()and automatic function-call executionexclude_nonebehavior for fields that were not explicitly suppliedFixes #5934.
Validation