fastapi/fastapi
Encoders
Purpose
fastapi/encoders.py is a single module exposing jsonable_encoder — the function that converts arbitrary Python values into JSON-compatible primitives. It is used internally for response bodies (when going through a Starlette JSONResponse), error payloads, and any time the framework needs to hand a rich Python object to a stdlib JSON encoder.
Key abstractions
| Symbol | Purpose |
|---|---|
jsonable_encoder(obj, *, include, exclude, by_alias, exclude_unset, exclude_defaults, exclude_none, custom_encoder, sqlalchemy_safe) |
Recursively converts the input to a tree of primitives. |
ENCODERS_BY_TYPE |
A dict mapping type → encoder function. Pre-populated for bytes, Color, datetime, Decimal, Enum, IPv4*, IPv6*, Path, Pattern, set, UUID, etc. |
decimal_encoder |
Returns int if the Decimal exponent is non-negative, otherwise float — preserves round-tripping. |
isoformat |
Wrapper around datetime.date.isoformat / datetime.time.isoformat. |
is_pydantic_v1_model_instance (re-exported from _compat) |
Used to gate Pydantic v1 detection — raising PydanticV1NotSupportedError if encountered. |
How it works
The function is a recursive walk:
- If
objis a Pydantic v2BaseModel, call its.model_dump(...)with the requested include/exclude/alias flags and recurse. - If
objis a Pydantic v1 model, raisePydanticV1NotSupportedError. - If
objis adataclass(and not yet handled), convert viadataclasses.asdictand recurse. - If
objisNone,str,int,float,bool— return as-is. - If
objis adict, recurse on key/value pairs (keys go through the encoder too). - If
objis a list/tuple/set/GeneratorType/deque/frozenset, return a list of encoded items. - If
obj's type is inENCODERS_BY_TYPEorcustom_encoder, apply the encoder. - Otherwise try a few fallback paths (
vars(obj)→ recurse) and finally raise.
The flags mirror Pydantic's model_dump:
by_alias— emit field aliases (matches what OpenAPI declares).exclude_unset— only include fields explicitly set on the model.exclude_defaults— drop fields equal to their default.exclude_none— dropNonevalues.include/exclude— set/dict of field paths to keep or drop.custom_encoder— caller-supplied per-type overrides.sqlalchemy_safe— whenTrue, drop SQLAlchemy internal attributes from__dict__-walked objects.
Usage in the framework
- Default exception handlers (
fastapi/exception_handlers.py) calljsonable_encoder(exc.errors())so validation errors render cleanly. fastapi/openapi/utils.py:get_openapi_security_definitionsencodes theSecurityBase.modelfor the OpenAPI document.serialize_response(fastapi/routing.py) prefers a Pydantic-direct fast path but falls back tojsonable_encoderfor arbitrary return values when noresponse_modelis set and the response is JSON.
Integration points
- Down to Pydantic —
BaseModel.model_dumpdoes the per-field work for models. The encoder mediates between Pydantic and stdlib JSON. - Down to
_compat.is_pydantic_v1_model_instance— raises a clear error rather than silently producing the wrong shape. - Up to user code —
jsonable_encoderis part of the public API:from fastapi.encoders import jsonable_encoder.
Entry points for modification
- Adding support for a new type: register in
ENCODERS_BY_TYPE(or documentcustom_encoder). - Adding a new flag: thread through the recursive function and through Pydantic's
model_dumpcall. - Changing handling for SQLAlchemy attributes: edit the
sqlalchemy_safebranch.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.
Previous
OpenAPI
Next
Exceptions