fix(tools): stringify Enum values in legacy schema declarations - #6981
Open
chelsealong wants to merge 1 commit into
Open
fix(tools): stringify Enum values in legacy schema declarations#6981chelsealong wants to merge 1 commit into
chelsealong wants to merge 1 commit into
Conversation
`_parse_schema_from_parameter` declared enum parameters as `type: STRING` but copied `e.value` verbatim into `schema.enum`, so an `IntEnum` (or any enum with non-string values) produced an int list under a STRING schema. That's invalid by `types.Schema`'s own contract: constructing the same schema through the public constructor raises `ValidationError`, and serializing the ADK-built one emits `PydanticSerializationUnexpectedValue` warnings. Stringify enum values (and the default, if any) so the declaration matches the STRING type it declares. `str`-valued enums are unaffected since `str(value) == value` for them.
chelsealong
force-pushed
the
fix-6978-intenum-string-values
branch
from
September 3, 2026 00:33
33c3853 to
db4f73f
Compare
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
Link to an existing issue:
Problem:
When a tool function has a parameter annotated with an
enum.IntEnum(orany
Enumwhose member.values are not strings), the legacy_parse_schema_from_parameterbuilds a declaration withtype: STRINGbutcopies the raw
e.valueintoschema.enum, e.g.[1, 2]. That is invalidby
types.Schema's own contract —Schema.enumislist[str]— so:(
types.Schema(type='STRING', enum=[1, 2])) raises aValidationError.PydanticSerializationUnexpectedValuewarnings, because the assignmenthappens via
schema.enum = [...]after construction, bypassingvalidation.
Solution:
Stringify enum values (
str(e.value)) when building theSTRINGschema,and stringify the default the same way before comparing/assigning it. This
keeps the declaration internally consistent with the
STRINGtype italready declares.
str-valued enums are unaffected, sincestr(value) == valuefor them (confirmed by the existingtest_enumstest, which stillpasses unchanged).
The call-path question the issue raised (how a string-vs-name enum value
maps back onto the Python
Enumwhen the model calls the tool) is aseparate, already-preexisting gap — there is no enum re-hydration in
FunctionTool._preprocess_argstoday forstrenums either — and is outof scope for this fix, which only makes the generated declaration valid.
Testing Plan
Unit Tests:
Added
TestBuildFunctionDeclarationLegacy.test_int_enumintests/unittests/tools/test_build_function_declaration.py, which builds adeclaration for an
IntEnum-typed parameter and asserts:schema.type == 'STRING'schema.enum == ['1', '2'](strings, not ints)schema.default == '1'(type, enum)pair round-trips throughtypes.Schema(...)without raising.
Verified the test fails without the fix:
And passes with the fix:
Full
tests/unittests/tools/directory (2119 tests):Manual End-to-End (E2E) Tests:
Reproduced the exact repro from the issue (
from_function_with_optionsona function with an
IntEnum-typed parameter, promoting the pydanticserializer
UserWarningto an error). Before the fix:After the fix,
decl.parameters.properties['level'].model_dump(...)returns{'enum': ['1', '2'], 'type': 'STRING'}with no warnings.Checklist
Additional context
Reported with AI assistance (this PR was authored by an AI coding agent);
the fix was implemented, tested, and reviewed following the reproduction
steps and root cause in the issue report.