Repository navigation
Description
media-buy/create-media-buy-response.json @ 3.1 types confirmed_at as ["string", "null"]
and lists it in the Success branch's required. That pair means the key must be present and
its value may be null. The generated CreateMediaBuyResponse1 types it non-nullable, so the
model cannot hold the value its own source schema permits.
import json, os, adcp
base = os.path.join(os.path.dirname(adcp.__file__), "_schemas", "3.1")
d = json.load(open(os.path.join(base, "media-buy", "create-media-buy-response.json")))
branch = d["oneOf"][0]
print(branch["properties"]["confirmed_at"]["type"], "confirmed_at" in branch["required"])
# ['string', 'null'] True
from adcp.types.generated_poc.media_buy.create_media_buy_response import CreateMediaBuyResponse1 as C
print(C.model_fields["confirmed_at"].annotation) # <class 'pydantic.types.AwareDatetime'>
C(media_buy_id="x", packages=[], status="completed", confirmed_at=None, revision=1)
# pydantic_core.ValidationError: confirmed_at — Input should be a valid datetimeWhy the null is a real state, not a theoretical one
confirmed_at is documented as the instant the seller committed the buy. A buy created in
pending_creatives has not been committed — the seller is holding it until creatives arrive —
so there is no instant to report, and the schema says so by permitting null while still
requiring the key. A seller that must supply a datetime has two choices, and both are wrong:
report a commitment that did not happen, or drop a key the schema requires.
Scope
Three required-AND-nullable declarations exist across every pinned response schema in the
3.1 tree:
confirmed_at CreateMediaBuySuccess media-buy/create-media-buy-response.json
confirmed_at CreateMediaBuySuccess bundled/media-buy/create-media-buy-response.json (same one, bundled)
cursor Registry Feed Response core/registry-feed-response.json
So this is one field, not a pattern — but it is the field on the busiest response in the
protocol, and the codegen rule that produced it (required read as "not Optional", without
consulting the declared type array) would narrow any future one the same way.
Second-order effect: exclude_none=True then drops the key
AdCPBaseModel.model_dump serializes with a blanket exclude_none=True. That is right for an
optional field — AdCP omits rather than sending null — and wrong for a field the schema lists
in required while typing it nullable. So a downstream model that widens the annotation to
express the permitted null gets the key dropped from the wire at exactly the moment the value
is legal, producing a document that fails validation against the pinned schema. The two
behaviours are individually defensible and jointly make the correct document unreachable.
Worth considering alongside the type fix: exclude None only for fields the model does not
mark required. That is derivable from model_fields alone, needs no schema lookup, and
reproduces today's behaviour everywhere except the case above.
Workaround in use
Widening the annotation on our subclass, with a # type: ignore[assignment] for the Liskov
violation, plus a serializer hook that re-inserts required-and-nullable fields after
exclude_none has removed them. Both delete when the generated type matches the schema.
Suggested fix
Generate AwareDatetime | None for a property whose type array contains "null", keeping
it required (no default) when the schema lists it in required. required and nullable are
independent axes in JSON Schema; the generator currently collapses them.
Activity
bokelley commentedon Sep 6, 2026
Triage
Classification: Bug
Bucket(s): validation (schema source fidelity, codegen rule)
Status: ready-to-implement
What the experts said:
datamodel-code-generatordropped thenullbranch oftype: ["string", "null"]and emittedAwareDatetimeinstead ofAwareDatetime | None(no default). Notably inconsistent:buy_products_response.pyandaccept_proposal_response.pyboth emitAwareDatetime | Nonecorrectly. Theexclude_nonesecondary fix is a separate, wider blast radius — changing theAdCPBaseModeldefault is a breaking serialization change and belongs in its own scoped PR.pending_creativeshas no seller commitment yet. The schema'sallOf/if/thenblock mechanically enforces the null invariants downstream. JSON Schema draft-07 §6.5.3 makesrequiredandtypeindependent axes; the generator collapsed them.My take: This is a correctness fix — the generated type rejects schema-valid wire payloads the protocol deliberately allows. The
cursorfield incore/registry-feed-response.jsonhas the same pattern and needs the same fix.Implementation scope:
generated_poc/. Fix via one of: (a) patch_forward_compat.pyusing the existing_patch_model_field+model_rebuild(force=True)pattern (stopgap); or (b) fixgenerate_types.pyto detectrequiredfields with"null"in theirtypearray and emitT | Nonewith no default (preferred — survives re-runs).cursoronCreateRegistryFeedResponse.AdCPBaseModel.model_dump/exclude_nonefor a separate issue — the workaround the issue author is already using (serializer hook on the subclass) is the correct narrow escape hatch until that is addressed.CreateMediaBuyResponse1(confirmed_at=None, ...)should validate; round-trip throughmodel_dump()should preserve the key.mypy src/andpytest tests/after the change.Triaged by Claude Code. Session: https://claude.ai/code/session_01Y1aHdgmymeGi8i32SyayR8
Generated by Claude Code
refactor: one A2A dispatch path, and message becomes a field the impl…
fix(codegen): keep required nullable response fields nullable
fix(codegen): keep required nullable response fields nullable (#1143)