Repository navigation
fix(tools): take the first non-null union member in _remove_any_of - #7989
Open
PratikDhanave wants to merge 1 commit into
Open
PratikDhanave wants to merge 1 commit into
PratikDhanave wants to merge 1 commit into
Conversation
`_remove_any_of` collapses a parameter's `anyOf` to a single schema for the Gemini (non-Vertex) function declaration path used by LangChain and CrewAI tools. Its comment says "Take the first non-null type", but the loop had no `break` and merged every non-null branch with `.update()`. For a heterogeneous union this had two wrong effects: the last non-null branch won instead of the first, and `.update()` left keywords from the earlier branch behind. For example `Union[List[str], int]` produced a schema typed `integer` that still carried the array-only `items` keyword (an invalid schema), and picked `int` rather than the first member. Optional[T] is unaffected because it has a single non-null branch. Add `break` so only the first non-null member is taken, matching the documented contract and producing a valid single-type schema.
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.
Link to Issue or Description of Change
Describe the change:
Problem:
_remove_any_ofinsrc/google/adk/tools/_automatic_function_calling_util.pycollapses a parameter'sanyOfinto a single schema on the Gemini (non-Vertex) function-declaration path used bybuild_function_declaration_for_langchainandbuild_function_declaration_for_params_for_crewai(i.e. LangChain / CrewAI tools).Its comment says "Take the first non-null type", but the loop has no
breakand merges every non-null branch withproperty_schema.update(type_):For a heterogeneous union this has two wrong effects:
update()merges rather than replaces, so keywords from an earlier branch leak into the result, producing an invalid schema.Example — a tool argument typed
Union[List[str], int]:Union[int, str]similarly yieldsSTRING(the last member) instead of the documented first memberINTEGER.Optional[T]is unaffected, because after_annotate_nullable_fieldsit has a single non-null branch.Solution:
Add
breakafter the first non-null branch is applied, so only the first non-null member is taken. This matches the function's own documented contract and produces a valid single-type schema. The commonOptional[T]path is unchanged (single branch).Testing Plan
Unit Tests:
Added two regression tests to
tests/unittests/tools/test_build_function_declaration.py:test_for_langchain_collapses_a_union_to_its_first_non_null_membertest_for_crewai_collapses_a_union_to_its_first_non_null_memberBoth fail on
main(the first collapses toINTEGERwith a strayitems; the second collapses to the last memberSTRING) and pass with this change.Fail-pre / pass-post for the new tests (fix reverted vs applied):
Manual End-to-End (E2E) Tests:
Not run — this is a pure schema-generation fix with no runtime/UI surface; it is covered by the unit tests above.
Checklist