fix(genai): don't mutate caller's tool dict in convert_to_genai_function_declarations#1889
Open
Andrew Chen (chuenchen309) wants to merge 1 commit into
Open
Conversation
…schema
`_format_to_genai_function_declaration` assigned `function` to the
caller's tool dict (or its nested `"function"`) and then normalized
`function["parameters"]` in place. So converting an OpenAI-format tool
whose `parameters` lacked a `properties` key wiped the caller's
`parameters` to `{}` (losing `required` / `additionalProperties`), and a
parameterless `{name, description}` dict had a `parameters` key added to
it.
Shallow-copy `function` before normalizing so the caller's tool dict is
left untouched. The produced FunctionDeclaration is unchanged.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Author
|
CI note: the single red check is |
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.
Why
_format_to_genai_function_declarationbindsfunctionto the caller's tool dict (or its nested"function") and then normalizesparametersin place:So
convert_to_genai_function_declarations([tool])mutates the caller's object:parameterslack apropertieskey has itsparameterswiped to{}(losingrequired,additionalProperties, …);{"name", "description"}dict gets aparameterskey added to it.Since tool dicts are frequently shared/reused across providers and calls, this silently corrupts the caller's data.
What
Shallow-copy
functionbefore normalizingparameters. The lines only reassign the top-levelparameterskey, so a shallow copy fully prevents caller mutation while leaving the producedFunctionDeclarationbyte-identical.Tests
Added
test_convert_to_genai_function_declarations_does_not_mutate_input(OpenAI-format-without-properties and parameterless cases). It fails onmain(the input dict is mutated) and passes with the fix. Fulltest_function_utils.py: 33 passed;ruffandmypyclean.🤖 AI disclosure: found by an automated code-review pass and fixed/tested with Claude Code (an AI coding agent). The regression is pinned by the new test.