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.
Description
validate_schema(google/adk/utils/_schema_utils.py) andSchemaTypeadvertise support forlist[str],list[int],dict[str, T], raw dict JSON schemas, andtypes.Schema.Only
BaseModel/list[BaseModel]go through Pydantic. Theelsebranch is a baresafe_json_loads— noTypeAdapter/ jsonschema check — so invalid values are silently accepted into session state / tool results.Environment
google-adk==2.9.2Repro
Observed (
google-adk==2.9.2)list[str]+"[1, 2, 3]"→[1, 2, 3]dict[str, int]+'{"a": "x"}'→{'a': 'x'}a+'{"a": 1}'→{'a': 1}BaseModelpath still raisesValidationError(control OK)Expected
Non-
BaseModelschemas should be validated (e.g.TypeAdapter(schema).validate_json(...)for GenericAlias types;jsonschemafor raw dict /types.Schema), or unsupported types should fail loudly — not silently accept invalid values.Impact
Agents with
output_schema=list[str](etc.) +output_keywrite invalid values into session state.AgentToolreturns 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 aslist[BaseModel]). For raw dict /types.SchemaJSON schemas, validate withjsonschema(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.