Repository navigation
fix: validate non-BaseModel schemas in validate_schema - #7285
Open
chelsealong wants to merge 2 commits into
Open
chelsealong wants to merge 2 commits into
chelsealong wants to merge 2 commits into
Conversation
validate_schema() only ran Pydantic validation for BaseModel and list[BaseModel] schemas. Every other supported schema kind (generic aliases like list[str]/dict[str, int], raw JSON Schema dicts, and types.Schema) fell through to a bare JSON parse with no validation, so malformed values (e.g. list[str] receiving [1, 2, 3]) were silently accepted into session state and tool results. Generic aliases now go through TypeAdapter.validate_json, matching the existing list[BaseModel] path. Raw dict schemas and types.Schema are validated with jsonschema (already a direct dependency), with types.Schema's uppercase type names lowercased first via the existing lowercase_schema_types helper. Fixes google#7280
model_dump() without by_alias=True emitted snake_case field names (any_of, additional_properties, max_items, etc.) instead of their JSON Schema keyword aliases (anyOf, additionalProperties, maxItems). jsonschema.validate() silently ignores unknown keywords, so any types.Schema using these aliased fields was not actually validated, reproducing the same "validation appears to run but doesn't" bug the prior commit fixed for other constraints. Adds tests covering any_of and additional_properties enforcement.
This was referenced Sep 25, 2026
Closed
5 of 6 tasks
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
Fixes #7280.
validate_schema()(src/google/adk/utils/_schema_utils.py) only ran realvalidation for
BaseModelandlist[BaseModel]schemas. Every othersupported
SchemaType— generic aliases likelist[str]/dict[str, int],raw JSON Schema
dicts, andtypes.Schema— fell through to a barejson.loadswith no validation, so malformed values were silently accepted:This matters because
output_schemaaccepts all of these types, and anunvalidated result gets written straight into session state
(
output_key) or returned to a parent agent viaAgentTool.Fix
list[str],dict[str, int], etc.) now go throughTypeAdapter(schema).validate_json(...), the same mechanism already usedfor the
list[BaseModel]branch.dicts andtypes.Schemainstances are now validated withjsonschema.validate(jsonschemais already a direct dependency, andalready used elsewhere in the codebase, e.g.
cli/built_in_agents/tools/write_config_files.py).types.Schemausesuppercase type names (
"STRING","OBJECT"), so it's dumped to a JSONSchema dict and passed through the existing
lowercase_schema_typeshelper before validation.
types.Schema.model_dump()is called withby_alias=True.types.Schema'sPython fields are snake_case (
any_of,additional_properties,max_items, ...) with JSON Schema keyword aliases (anyOf,additionalProperties,maxItems, ...). Withoutby_alias=True,model_dump()emitted the snake_case names, whichjsonschema.validatesilently ignores as unknown properties — so
any_of,additionalProperties,and the min/max constraints were never actually enforced, even though the
rest of the schema validated correctly. This was caught in review; added
regression tests for
any_ofandadditional_propertiesbelow.No changes to
pyproject.toml/ lockfiles —jsonschemawas already adeclared dependency.
Test plan
Added 8 tests to
tests/unittests/utils/test_schema_utils.py::TestValidateSchemacovering the reject case for each affected schema kind (
list[str],dict[str, int], raw JSON Schema dict,types.Schema), the accept casefor the two new schema kinds that weren't exercised before, and two
aliased-field regression cases (
any_of,additional_properties) ontypes.Schema.Confirmed the new reject-case tests fail on the pre-fix code
(
git stashthe source file, keep the tests, run again):With the fix restored:
Also ran the broader suite for callers of
validate_schemaplus the rest oftests/unittests/utils/, all green:pre-commit runpasses on the changed files (ruff, isort, pyink, addlicense,codespell).
mypyreports the same 6 pre-existing errors in this file withand without the change (unrelated to this fix, present on unmodified
main).AI disclosure
This change was written with the assistance of Claude Code (Anthropic).
🤖 Generated with Claude Code