Python: Preserve null arguments during tool invocation#5944
Conversation
There was a problem hiding this comment.
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
Nonevalues. - 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. |
| 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 |
There was a problem hiding this comment.
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.
35a9f72 to
c0fedbb
Compare
|
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. |
Summary
nullvalues when dumping validated tool argumentsFunctionTool.invoke()and automatic function callingFixes #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 -qpython -m pytest tests/core/test_tools.py tests/core/test_function_invocation_logic.py -qpython -m ruff check agent_framework/_tools.py tests/core/test_tools.py tests/core/test_function_invocation_logic.py