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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions