Factory.ai

Open-Source Wikis

/

FastAPI

/

Reference

/

Data models

fastapi/fastapi

Data models

FastAPI defines surprisingly few Pydantic models of its own — most data shapes belong to user code or to Starlette/Pydantic. The framework's models cluster into three groups.

OpenAPI spec models (fastapi/openapi/models.py)

Pydantic models that mirror OpenAPI 3.1. Each is configured with extra = "allow" so spec extensions (x-*) flow through. The headline classes:

Model Notable fields
OpenAPI openapi, info, jsonSchemaDialect, servers, paths, webhooks, components, security, tags, externalDocs.
Info title, summary, description, termsOfService, contact, license, version.
Contact, License Spec-defined sub-objects of info.
Server, Tag, ExternalDocumentation Top-level structural objects.
PathItem One per path; holds summary, description, and one operation per HTTP method.
Operation tags, summary, description, externalDocs, operationId, parameters, requestBody, responses, callbacks, deprecated, security, servers.
Parameter, RequestBody, Response, MediaType, Encoding, Example, Header, Reference, Schema Operation building blocks.
SecurityBase, APIKey, HTTPBase, HTTPBearer, OAuth2, OAuthFlows, OAuthFlowImplicit, OAuthFlowPassword, OAuthFlowClientCredentials, OAuthFlowAuthorizationCode, OpenIdConnect Security-scheme variants.

Each security class subclassed by FastAPI's runtime security schemes (under fastapi/security/) carries a model: instance of the matching OpenAPI model — that is how a runtime scheme contributes to the schema.

Server-Sent Events (fastapi/sse.py)

Model Description
ServerSentEvent Pydantic model for a single SSE: data (any JSON-serialisable value), raw_data (raw string, mutually exclusive with data), event (event type), id, retry, comment.
_SSE_EVENT_SCHEMA A bare dict[str, Any] (not a Pydantic model) matching the OpenAPI 3.2 SSE schema fragment. Injected into the OpenAPI document for SSE routes.

Exception payloads (fastapi/exceptions.py)

The validation-error classes carry sequences of error dicts (the shape Pydantic produces for ValidationError.errors()). They are not themselves Pydantic models, but they are paired with two OpenAPI fragments declared in fastapi/openapi/utils.py:

  • validation_error_definition — the ValidationError schema.
  • validation_error_response_definition — the HTTPValidationError wrapper with {"detail": [...]}.

Two empty Pydantic models are created at module import to namespace error locations:

RequestErrorModel:   type[BaseModel] = create_model("Request")
WebSocketErrorModel: type[BaseModel] = create_model("WebSocket")

These show up in error messages so a validation error against the request body says body.field_name and against the websocket says websocket.field_name.

Internal value classes (not Pydantic)

These are dataclasses or plain classes used as carriers:

Class File Purpose
Dependant fastapi/dependencies/models.py The function-signature reflection used for DI.
EndpointContext fastapi/exceptions.py TypedDict(total=False)function, path, file, line.
DefaultPlaceholder fastapi/datastructures.py Sentinel for "framework default, may be overridden."
UploadFile fastapi/datastructures.py Subclass of Starlette's UploadFile adding __get_pydantic_*__ hooks.
OAuth2PasswordRequestForm, OAuth2PasswordRequestFormStrict fastapi/security/oauth2.py Form-data carriers for OAuth2 password flow.
HTTPBasicCredentials, HTTPAuthorizationCredentials fastapi/security/http.py Pydantic models that wrap the parsed Authorization header.

Re-exports

Most of fastapi.responses and fastapi.datastructures exists to re-export Starlette types (FormData, Headers, QueryParams, Address, URL, State, JSONResponse, HTMLResponse, RedirectResponse, StreamingResponse, FileResponse, PlainTextResponse). They are not redefined here — see Starlette docs for their fields.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Data models – FastAPI wiki | Factory