Skip to content

require-pydantic-for-external-json

Proven external JSON record fields are consumed before runtime schema validation.

Why

Annotations, casts, and partial key checks do not validate a wire protocol; a maintained runtime schema makes required fields, types, and protocol versions explicit at the boundary.

Fix

Validate raw JSON with Model.model_validate_json(...) or TypeAdapter(Model).validate_json(...); for an already-decoded response, use model_validate, validate_python, or another maintained runtime schema validator before reading fields.

Examples

Before — flagged External JSON read as a dictionary
protocol.py
import httpx
def fetch() -> object:
report = httpx.get("https://api.example/report").json()
return report.get("version")
After — preferred External JSON validated by a boundary model
protocol.py
import httpx
from pydantic import BaseModel
class Report(BaseModel):
version: int
def fetch() -> Report:
raw = httpx.get("https://api.example/report").json()
report = Report.model_validate(raw)
return report