Repository navigation
Description
Problem
Pydantic doesn't auto-call custom model_dump() on nested models — if a parent's children have model_dump() overrides, the parent's super().model_dump() produces the children via the default Pydantic serialization, not via the children's overrides. Adopters that need custom serialization on a child class (most commonly: tenant-internal fields excluded with exclude=True, or AdCP-spec-shaped overrides on extended types) end up writing this in every response model:
class GetCreativesResponse(AdCPBaseModel):
creatives: list[Creative]
def model_dump(self, **kwargs):
result = super().model_dump(**kwargs)
if \"creatives\" in result and self.creatives:
result[\"creatives\"] = [c.model_dump(**kwargs) for c in self.creatives]
return resultWe have ~59 such overrides across src/core/schemas/. Every override is mechanical: walk the children, call their model_dump. Adding a new response type means writing one again. Forgetting one is silent — the test fails on a serialization-difference, but only if the test exercises the nested path.
Proposed SDK shape
Either of:
A) AdCPBaseModel recursively dispatches custom model_dump() on children. Walk __pydantic_fields__, detect when a field's annotated type has a custom model_dump, and call it directly instead of relying on Pydantic's default child serialization. Same for lists/dicts of models.
B) Codegen flag that emits nested-aware model_dump() on the generated response types — adopters extending those types via inheritance get the nested-walking for free.
(A) is broader; (B) is narrower but ships with no risk to non-adopters.
Why this is the highest-LOC win on our open list
The other open framework gaps (spec defaults, agent-card URL, transport contextvar, test app builder) each kill 50-275 LOC of middleware. This one kills hundreds of LOC of mechanical override boilerplate spread across the schema package.
Files
- Our overrides live across src/core/schemas/_base.py, product.py, creative.py, delivery.py, account.py.
- Pattern documented in our CLAUDE.md as "Critical Pattern chore(main): release 1.0.0 #4: Pydantic Explicit Nested Serialization" — we'd love to delete the section.
Activity
bokelley commentedon May 10, 2026
Triage
Classification: Feature request → resolved as documentation gap
Bucket(s): client, docs (note: no
docslabel exists in this repo — flagging the gap)Status: drafting-pr
PR: #630
What the experts said:
Field(exclude=True)works at all nesting depths — confirmed.@model_serializerdoes NOT fire from a parent when the field is declared as the base type;serialize_as_any=Trueis required. Option A'sinclude/excludekwargs-forwarding is a correctness blocker; detection is also one-level-deep only.Field(exclude=True)already solves the field-exclusion case (proven bytest_response_builder_subclass.py);@model_serializerhandles custom logic. Implementing Option A would paper over the anti-pattern and introduce its own silent bugs.My take: Option A has a structural one-level-deep limit and kwargs-contract brittleness that make it a trap for the next adopter who hits a deeper nesting chain. Redirecting to the canonical Pydantic v2 patterns eliminates the boilerplate entirely — and the migration path (move
exclude=Trueto the child field) is mechanical. Draft PR #630 adds the missing sections todocs/extending-types.mdand a one-line note onAdCPBaseModel.model_dump().What PR #630 ships:
Field(exclude=True)recommended pattern with a runnable nested example,@model_serializer(mode='wrap')section with explicitserialize_as_any=Truecaveat, migration guide for the 59-style parent-dispatch overrides, and fixes for pre-existing wrong type names (CreateMediaBuySuccess→CreateMediaBuySuccessResponse,WebhookPayload→McpWebhookPayload) throughout the doc.Triaged by Claude Code. Session: https://claude.ai/code/session_01P7MQW9tW7z4rYm13zghrVC
Generated by Claude Code
feat(types)!: default serialize_as_any=True in AdCPBaseModel.model_du…