Build a typed JSON API
Prerequisites: Python 3.12+, Dexpot with its CLI, and a terminal. Create a project and install the framework before following this guide:
python -m venv .venv. .venv/bin/activatepython -m pip install 'dexpot[cli]'On Windows PowerShell, activate with .venv\Scripts\Activate.ps1. This guide uses a local-only listener; it does not expose the example to a network.
For older v0.9.0 on Python 3.14/3.14t, install with
python -m pip install 'dexpot[cli]==0.9.0' 'msgspec>=0.20'. Its metadata does not enforce
this native-decoder floor; older decoders can silently omit ordinary Struct fields.
Dexpot v0.9.1 and newer enforce msgspec>=0.20 on 3.14 and retain msgspec>=0.19
on Python 3.12/3.13. Optional generic
schema support still follows native interpreter/msgspec availability.
1. Declare the wire shapes and routes
Section titled “1. Declare the wire shapes and routes”Save as main.py:
import msgspecfrom dexpot import Dex
class ItemIn(msgspec.Struct): name: str price: float
class ItemOut(msgspec.Struct): id: int name: str price: float
app = Dex()
@app.get('/items/{item_id}', response=ItemOut)def get_item(item_id: int) -> ItemOut: return ItemOut(item_id, f'item-{item_id}', 9.99)
@app.post('/items', body=ItemIn, response=ItemOut)def create_item(item: ItemIn) -> tuple[int, ItemOut]: return 201, ItemOut(1, item.name, item.price)The explicit body=ItemIn selects and precompiles a msgspec JSON decoder; the matching handler parameter receives the decoded ItemIn. A path capture is converted according to the int annotation. response=ItemOut strictly validates and projects the returned value onto the public schema; invalid output produces a sanitized 500. Annotate an optional request: Request parameter to receive the frozen public request context; see routes and handlers.
Path annotations support only str, int, unannotated parameters, and Any.
float, bool, Enum, Annotated, unions, and explicit None are unsupported.
In dexpot 0.8.1 and newer, unsupported path annotations raise TypeError during registration
instead of silently receiving strings as in v0.8.0. This is separate from the richer
JSON body and response schema support shown above. For raw string captures, validate and
convert explicitly in application code before numeric comparisons or boolean decisions.
2. Run and exercise the public HTTP interface
Section titled “2. Run and exercise the public HTTP interface”dexpot serve main:app --host 127.0.0.1 --port 8000In another terminal:
curl -i http://127.0.0.1:8000/items/7curl -i -X POST http://127.0.0.1:8000/items \ -H 'Content-Type: application/json' \ -d '{"name":"keyboard","price":79.0}'curl -i -X POST http://127.0.0.1:8000/items \ -H 'Content-Type: application/json' -d '{"name":false}'curl -i -X PUT http://127.0.0.1:8000/itemsThe first request returns a JSON ItemOut with id 7. The valid POST returns status 201 and the submitted name and price. Invalid JSON or body validation returns 422; a path that exists only for another method returns 405 with Allow. These are deterministic contract expectations; consult the linked response checks for the repository’s executable examples.
3. Verify against the repository
Section titled “3. Verify against the repository”The repository’s socket-level example test launches each example as a subprocess and checks actual HTTP responses:
uv sync --all-extrasuv run pytest tests/test_examples.py -qSee examples/typed_crud.py for mutable CRUD state and application-owned locking. Do not share mutable state between concurrent handlers without synchronization. For binding details—including positional and keyword-only parameters, registration failures, postponed annotations, and annotation_locals—continue to routes and handlers. For bounded limits and parser behavior, read the HTTP boundary.
4. Explicit scalar inputs
Section titled “4. Explicit scalar inputs”Included in v0.9.0: typed query/header/cookie inputs.
The public-install workflow above supports Annotated[T, Query('name')], Header('name')
and Cookie('name'); no source-only installation is needed for these released bindings.
To run the complete repository example from a checkout, use:
uv sync --all-extrasuv run python examples/typed_inputs.pyIn another terminal:
curl -i 'http://127.0.0.1:8000/items/drinks?limit=1&min_price=3&include_archived=false' \ -H 'X-Request-ID: example-1' -H 'Cookie: theme=dark'curl -i 'http://127.0.0.1:8000/items/drinks?include_archived=yes' \ -H 'X-Request-ID: example-2'curl -i 'http://127.0.0.1:8000/items/drinks?limit=1&limit=2' \ -H 'X-Request-ID: repeated'curl -i 'http://127.0.0.1:8000/items/drinks?limit=1&limit=1' \ -H 'X-Request-ID: identical'The example applies query pagination/filtering, a required request-id header and a cookie
preference. The first call uses converted int/float/bool arguments; the second returns 422
before the handler. Request-id is trace metadata, not authentication. Defaults apply only to
missing values. Only exact str/int/finite float/bool are supported. Requested query/cookie
duplicates fail, including identical values: both limit=1&limit=2 and
limit=1&limit=1 return 422 with {"detail":"query parameter 'limit': repeated"}.
A declared scalar accepts one occurrence; it never chooses the first or last.
Headers retain normalized/comma-folded behavior. Path converters are unchanged.
See typed input sources for encoding/cookie
rules, defaults, sanitized errors and precedence. Typed binding does not implicitly enable OpenAPI or DI.
5. Export the OpenAPI contract
Section titled “5. Export the OpenAPI contract”Included in v0.9.0: OpenAPI export.
Create Dex(openapi=True) before declaring routes to capture OpenAPI 3.1 metadata at
registration. app.openapi(title="Example API", version="development") returns a detached
JSON-compatible document and freezes further registration after successful export. API
info.version is caller metadata, not the package version. Ordinary applications do not
load the exporter or capture schemas.
The export uses existing endpoint bindings and msgspec wire schemas, not fresh handler introspection. It describes path/query/header/cookie aliases, required values, scalar defaults, JSON bodies and declared response schemas. Runtime-selected statuses are not guessed: checked responses include the declared payload or a detail error envelope; unchecked/raw response media and shapes remain unspecified. Statuses 204/205/304 have no content. Query lexical rules, normalized headers and strict cookies still govern requests; JSON Schema does not implement those transport parsers.
There is no automatic endpoint, documentation UI, authentication/security inference or DI.
The caller chooses whether to save or serve the document. Exported defaults and docstrings are
public metadata: review them before serving the document. Unsupported schema/component names,
external/unresolved references, ignored OpenAPI headers (Accept, Content-Type, Authorization),
and contradictory templated paths fail instead of producing a misleading document.
Postponed schema field annotations containing calls are excluded for Struct, dataclass, attrs,
TypedDict and NamedTuple owners, including NewType wrappers, because re-evaluation can
disagree with an existing decoder. Construct constrained aliases eagerly (for example,
Name = Annotated[str, msgspec.Meta(min_length=1)]) and use name: Name in the Struct.
References must be statically resolvable: inherited class aliases and literal lookups in
built-in dictionaries/lists/tuples are supported. Opaque attribute getters/descriptors, custom subscriptions or
union operators, conditional type expressions and unresolved references reject rather than
silently skipping a schema. Literal payloads and literal Annotated metadata remain values.
Postponed model annotations follow native module-before-class lookup, including
PEP 695 type parameters; class-local aliases remain fallbacks when unshadowed.
Generic-model availability still follows native interpreter/msgspec support.
Opaque module namespace getters, including replacement __dict__ descriptors,
are rejected before the exporter reads their namespace.
Model metaclasses may not replace structural metadata readers (such as __dict__
or __annotations__) or native field/default metadata with opaque descriptors;
ordinary/native descriptors remain supported.
Custom fallback attribute getters cannot fabricate schema metadata; standard enum
attribute lookup remains supported.
Model annotations, referenced aliases, defaults and schema metadata must stay unchanged
from their first native/msgspec use. Captured models have weak, identity-based process-wide
fingerprints: later conflicting captures reject across routes and applications, while existing
snapshots remain intact. Declare a new model type for a new contract. The exporter cannot
reconstruct bindings from an external native cache created before its first capture; changing
a model in that unobserved interval is unsupported.
Run the complete source example (not a public v0.8.1 install):
uv run python examples/openapi.pycurl -s http://127.0.0.1:8000/openapi.jsoncurl -i -X POST 'http://127.0.0.1:8000/items/7?limit=2' \ -H 'Content-Type: application/json' -H 'X-Request-ID: example' \ -d '{"name":"tea","price":3.5}'The example explicitly registers its document route and serves a precomputed JSON snapshot;
requests do not regenerate schemas. The valid POST returns 201. Replacing limit=2 with
limit=bad returns 422 before the handler. To write a file without opening a listener:
uv run python examples/openapi.py --export openapi.json