Skip to content

validate_schema skips validation for non-BaseModel schemas (list[str], dict[…], raw JSON Schema) #7280

Description

@michaelkillgta

Description

validate_schema (google/adk/utils/_schema_utils.py) and SchemaType advertise support for list[str], list[int], dict[str, T], raw dict JSON schemas, and types.Schema.

Only BaseModel / list[BaseModel] go through Pydantic. The else branch is a bare safe_json_loads — no TypeAdapter / jsonschema check — so invalid values are silently accepted into session state / tool results.

Environment

  • google-adk==2.9.2
  • Offline unit repro (no live API / GCP)

Repro

from google.adk.utils._schema_utils import validate_schema
from pydantic import BaseModel, ValidationError

class M(BaseModel):
    a: str

# Control: BaseModel still validates
try:
    validate_schema(M, '{"a": 1}')
except ValidationError:
    print("basemodel rejects bad: OK")

print(validate_schema(list[str], "[1, 2, 3]"))       # → [1, 2, 3]  WRONG
print(validate_schema(dict[str, int], '{"a": "x"}')) # → {'a': 'x'} WRONG
schema = {"type": "object", "properties": {"a": {"type": "string"}}, "required": ["a"]}
print(validate_schema(schema, '{"a": 1}'))           # → {'a': 1}   WRONG

Observed (google-adk==2.9.2)

  • list[str] + "[1, 2, 3]" → [1, 2, 3]
  • dict[str, int] + '{"a": "x"}' → {'a': 'x'}
  • Raw JSON Schema requiring string a + '{"a": 1}' → {'a': 1}
  • BaseModel path still raises ValidationError (control OK)

Expected

Non-BaseModel schemas should be validated (e.g. TypeAdapter(schema).validate_json(...) for GenericAlias types; jsonschema for raw dict / types.Schema), or unsupported types should fail loudly — not silently accept invalid values.

Impact

Agents with output_schema=list[str] (etc.) + output_key write invalid values into session state. AgentTool returns unvalidated payloads to the parent. Downstream agents/tools treat garbage as schema-valid.

Suggested fix

For GenericAlias / non-dict schema types, use TypeAdapter(schema).validate_json(json_text) (same as list[BaseModel]). For raw dict / types.Schema JSON schemas, validate with jsonschema (already a dependency) or reject unsupported types loudly.

Notes

Nearby #5054 (output_schema=str + tools loop) and #6747 (python-mode dump) are different root causes. Happy to open a PR.

Activity

  1. chelsealong commented on Sep 25, 2026

    @chelsealong
    Contributor

    Picking this one up now — opening a PR shortly. Flagging it here so nobody duplicates the work; if someone is already on it, say so and I will drop mine.

  2. michaelkillgta commented on Sep 25, 2026

    @michaelkillgta
    Author

    Thanks for picking this up so quickly — and for the PRs (#7285 / #7284 / #7283). Happy to review or clarify anything from the original repros if useful.

  3. added
    core[Component] This issue is related to the core interface and implementation
    on Sep 28, 2026
  4. added
    request clarification[Status] The maintainer need clarification or more information from the author
    on Sep 29, 2026
  5. llalitkumarrr commented on Sep 29, 2026

    @llalitkumarrr
    Collaborator

    Hello @michaelkillgta,

    Your observations in this report are correct validation is being skipped which may cause issues for agents with an output_schema. The linked PR addresses this concern but it requires an in-depth review. We appreciate your patience while the reviewer examines it.

    Additionally if you could share your observations on the PR, that would be a great help.

  6. michaelkillgta commented on Sep 30, 2026

    @michaelkillgta
    Author

    Looked at #7285. Routing generic aliases through TypeAdapter, and dict / types.Schema through jsonschema, matches the report. The new tests cover the list[str] and dict[str, int] rejections, plus raw JSON Schema and types.Schema.

    Callers that only catch pydantic's ValidationError will still miss jsonschema.ValidationError on the dict and types.Schema path, so it may be worth wrapping those into one error type. I reviewed the diff only and have not run the suite.

  7. michaelkillgta commented on Sep 30, 2026

    @michaelkillgta
    Author

    Follow-up after running the tests. On #7285 (8dff62f), test_schema_utils.py passed 71 tests.

    The split is real. list[str] and dict[str, int] raise pydantic's ValidationError. A raw JSON Schema dict and a types.Schema raise jsonschema.ValidationError for the same kind of bad value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementationrequest clarification[Status] The maintainer need clarification or more information from the author

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions