Skip to content

Python: Preserve null arguments during tool invocation#5944

Open
shusingh wants to merge 1 commit into
microsoft:mainfrom
shusingh:fix-null-tool-arguments
Open

Python: Preserve null arguments during tool invocation#5944
shusingh wants to merge 1 commit into
microsoft:mainfrom
shusingh:fix-null-tool-arguments

Conversation

@shusingh
Copy link
Copy Markdown

Summary

  • Preserve explicitly provided null values when dumping validated tool arguments
  • Apply the same behavior to direct FunctionTool.invoke() and automatic function calling
  • Add regression coverage for required nullable tool parameters

Fixes #5934

Tests

  • python -m pytest tests/core/test_tools.py::test_tool_invoke_preserves_explicit_null_for_required_nullable_argument tests/core/test_function_invocation_logic.py::test_auto_function_calling_preserves_explicit_null_arguments -q
  • python -m pytest tests/core/test_tools.py tests/core/test_function_invocation_logic.py -q
  • python -m ruff check agent_framework/_tools.py tests/core/test_tools.py tests/core/test_function_invocation_logic.py

Copilot AI review requested due to automatic review settings May 19, 2026 01:57
@github-actions github-actions Bot changed the title Preserve null arguments during tool invocation Python: Preserve null arguments during tool invocation May 19, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Python core tool invocation to preserve explicitly supplied null values through Pydantic argument validation, addressing required nullable tool parameters during direct and automatic invocation.

Changes:

  • Adds a helper for dumping Pydantic argument models while restoring explicit top-level None values.
  • Applies the helper in FunctionTool.invoke() and automatic function calling.
  • Adds regression tests for direct and automatic nullable required arguments.

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 Updates argument dumping in tool invocation paths to preserve explicit None values.
python/packages/core/tests/core/test_tools.py Adds direct FunctionTool.invoke() regression coverage.
python/packages/core/tests/core/test_function_invocation_logic.py Adds automatic function calling regression coverage.

Comment on lines +194 to +200
def _model_dump_preserving_explicit_none(model: BaseModel) -> dict[str, Any]:
"""Dump a model without dropping fields that were explicitly set to None."""
dumped = model.model_dump(exclude_none=True)
for field_name in model.model_fields_set:
if isinstance(field_name, str) and getattr(model, field_name, None) is None:
dumped[field_name] = None
return dumped
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Updated the helper to recursively restore explicitly provided null values in nested Pydantic models, mappings, and lists/tuples, and added nested regression coverage for both direct invoke and automatic function calling.

@shusingh shusingh force-pushed the fix-null-tool-arguments branch from 35a9f72 to c0fedbb Compare May 19, 2026 15:20
@shusingh
Copy link
Copy Markdown
Author

Updated the PR to handle nested explicit null values as well. The argument dump helper now recursively restores explicitly provided nulls in nested Pydantic models, mappings, and lists/tuples, and I added regression coverage for both direct tool invocation and automatic function calling with nested nullable arguments.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Auto function calling removes null arguments

3 participants