Python: fix(core): preserve null arguments in tool function calls#6199
Open
hanhan761 wants to merge 1 commit into
Open
Python: fix(core): preserve null arguments in tool function calls#6199hanhan761 wants to merge 1 commit into
hanhan761 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR changes how tool input arguments are serialized after Pydantic validation, by no longer excluding None values when model_dump() is called.
Changes:
- Remove
exclude_none=Truefrommodel_dump()when normalizing mapping inputs. - Remove
exclude_none=Truefrommodel_dump()when normalizing BaseModel inputs. - Remove
exclude_none=Truefrommodel_dump()in the auto-invocation path.
| parsed_arguments = self.input_model.model_validate(parsed_arguments).model_dump( | ||
| exclude_none=True | ||
| ) | ||
| parsed_arguments = self.input_model.model_validate(parsed_arguments).model_dump() |
| ): | ||
| raise TypeError(f"Expected {self.input_model.__name__}, got {type(arguments).__name__}") | ||
| parsed_arguments = arguments.model_dump(exclude_none=True) | ||
| parsed_arguments = arguments.model_dump() |
Comment on lines
+1493
to
1496
| args = tool.input_model.model_validate(parsed_args).model_dump() | ||
| else: | ||
| args = dict(parsed_args) | ||
| args = _validate_arguments_against_schema( |
| try: | ||
| if not cast(bool, getattr(tool, "_schema_supplied", False)) and tool.input_model is not None: | ||
| args = tool.input_model.model_validate(parsed_args).model_dump(exclude_none=True) | ||
| args = tool.input_model.model_validate(parsed_args).model_dump() |
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.
Summary
Remove
exclude_none=Truefrommodel_dump()calls inFunctionTool.invoke()and_auto_invoke_function()so that null arguments from the LLM are preserved and passed to the tool function.Previously, calling a tool with a nullable required parameter (e.g.
unit: Literal["C", "F"] | None) and passingnullwould strip the key viamodel_dump(exclude_none=True), causing_validate_arguments_against_schemato raiseTypeErrorfor "Missing required argument".Issue
Fixes #5934
Verification
FunctionTool.invoke(arguments={"location": "Seattle", "unit": None})now works correctly