{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"openapi_first","text":""},{"location":"#openapi_first","title":"openapi_first","text":"
FastAPI OpenAPI First \u2014 strict OpenAPI-first application bootstrap for FastAPI.
FastAPI OpenAPI First is a contract-first infrastructure library that enforces OpenAPI as the single source of truth for FastAPI services.
The library removes decorator-driven routing and replaces it with deterministic, spec-driven application assembly. Every HTTP route, method, and operation is defined in OpenAPI first and bound to Python handlers explicitly via operationId.
The package is intentionally minimal and layered. Each module has a single responsibility and exposes explicit contracts rather than convenience facades.
"},{"location":"#openapi_first--architecture-overview","title":"Architecture Overview","text":"The library is structured around four core responsibilities:
The package root acts as a namespace, not a facade. Consumers are expected to import functionality explicitly from the appropriate module.
"},{"location":"#openapi_first--installation","title":"Installation","text":"Install using pip:
pip install openapi-first\n Or with Poetry:
poetry add openapi-first\n Runtime dependencies are intentionally minimal: - fastapi (server-side) - httpx (client-side) - openapi-spec-validator - pyyaml (optional, for YAML specs)
The ASGI server (e.g., uvicorn) is an application-level dependency and is not bundled with this library.
"},{"location":"#openapi_first--command-line-interface-scaffolding-templates","title":"Command-Line Interface (Scaffolding, Templates)","text":"FastAPI OpenAPI First ships with a small CLI for bootstrapping OpenAPI-first FastAPI applications from bundled templates.
List available application templates:
openapi-first --list\n Create a new application using the default template:
openapi-first\n Create a new application using a specific template:
openapi-first health_app\n Create a new application in a custom directory:
openapi-first health_app my-service\n The CLI copies template files verbatim into the target directory. No code is generated or modified beyond the copied scaffold.
"},{"location":"#openapi_first--server-side-usage-openapi-fastapi","title":"Server-Side Usage (OpenAPI \u2192 FastAPI)","text":"Minimal OpenAPI-first FastAPI application:
from openapi_first import app\nimport my_service.routes as routes\n\napi = app.OpenAPIFirstApp(\n openapi_path=\"openapi.yaml\",\n routes_module=routes,\n title=\"My Service\",\n version=\"1.0.0\",\n)\n\n# Run with:\n# uvicorn my_service.main:api\n Handler definitions (no decorators):
def get_health():\n return {\"status\": \"ok\"}\n OpenAPI snippet:
paths:\n /health:\n get:\n operationId: get_health\n responses:\n \"200\":\n description: OK\n The binder guarantees: - Every OpenAPI operationId has exactly one handler - No undocumented routes exist - All mismatches fail at application startup
"},{"location":"#openapi_first--client-side-usage-openapi-http-client","title":"Client-Side Usage (OpenAPI \u2192 HTTP Client)","text":"The same OpenAPI specification can be used to construct a strict, operationId-driven HTTP client.
Client construction:
from openapi_first.loader import load_openapi\nfrom openapi_first.client import OpenAPIClient\n\nspec = load_openapi(\"openapi.yaml\")\n\nclient = OpenAPIClient(spec)\n Calling operations (operationId is the API):
response = client.get_health()\nassert response.status_code == 200\nassert response.json() == {\"status\": \"ok\"}\n Path parameters must match the OpenAPI specification exactly:
response = client.get_item(\n path_params={\"item_id\": 1}\n)\n Request bodies are passed explicitly:
response = client.create_item(\n body={\"name\": \"Orange\", \"price\": 0.8}\n)\n Client guarantees: - One callable per OpenAPI operationId - No hardcoded URLs or HTTP methods in user code - Path and body parameters must match the spec exactly - Invalid or incomplete OpenAPI specs fail at client construction time - No schema inference or mutation is performed
The client is transport-level only and returns httpx.Response objects directly. Response interpretation and validation are left to the consumer or higher-level layers.
FastAPI OpenAPI First is designed to be extended via explicit contracts:
loader.load_openapibinder.bind_routesUsers SHOULD NOT rely on FastAPI decorators for routing when using this library. Mixing decorator-driven routes with OpenAPI-first routing defeats the contract guarantees and is explicitly unsupported.
"},{"location":"#openapi_first--public-api-surface","title":"Public API Surface","text":"The supported public API consists of the following top-level modules:
Classes and functions should be imported explicitly from these modules. No individual symbols are re-exported at the package root.
"},{"location":"#openapi_first--design-guarantees","title":"Design Guarantees","text":"FastAPI OpenAPI First favors correctness, explicitness, and contract enforcement over convenience shortcuts.
"},{"location":"#openapi_first--core-philosophy","title":"Core Philosophy","text":"FastAPI OpenAPI First operates on the Contract-as-Code principle:
httpx-based client, ensuring type-safe communication.Follow these \"AI-Native\" docstring principles to maximize developer and agent productivity:
"},{"location":"#openapi_first--for-humans","title":"For Humans","text":"openapi_first.loader.load_openapi)..pyi stubs for all public interfaces to provide an optimized machine-context.: description pairs in Raises blocks to allow agents to accurately map errors to spec violations.OpenAPI-first application bootstrap for FastAPI.
This module provides OpenAPIFirstApp, a thin but strict abstraction that enforces OpenAPI as the single source of truth for a FastAPI service.
This module is intended for teams that want:
OpenAPIFirstApp(*, openapi_path: str, routes_module: Any, **fastapi_kwargs: Any)\n Bases: FastAPI
FastAPI application enforcing OpenAPI-first design.
OpenAPIFirstApp subclasses FastAPI and replaces manual route registration with OpenAPI-driven binding. All routes are derived from the provided OpenAPI specification, and each operationId is mapped to a Python function in the supplied routes module.
openapi_path : str Filesystem path to the OpenAPI 3.x specification file. This specification is treated as the authoritative API contract.
modulePython module containing handler functions whose names correspond exactly to OpenAPI operationId values.
**fastapi_kwargs Additional keyword arguments passed directly to fastapi.FastAPI (e.g., title, version, middleware, lifespan handlers).
OpenAPIFirstError If the OpenAPI specification is invalid, or if any declared operationId does not have a corresponding handler function.
"},{"location":"app/#openapi_first.app.OpenAPIFirstApp--behavior-guarantees","title":"Behavior guarantees","text":"/openapi.json always reflect the provided spec.from openapi_first import OpenAPIFirstApp import app.routes as routes
app = OpenAPIFirstApp( ... openapi_path=\"app/openapi.json\", ... routes_module=routes, ... title=\"Example Service\" ... )
"},{"location":"binder/","title":"Binder","text":""},{"location":"binder/#openapi_first.binder","title":"openapi_first.binder","text":""},{"location":"binder/#openapi_first.binder--openapi_firstbinder","title":"openapi_first.binder","text":"OpenAPI-driven route binding for FastAPI.
This module is responsible for translating an OpenAPI 3.x specification into concrete FastAPI routes. It enforces a strict one-to-one mapping between OpenAPI operations and Python handler functions using operationId.
"},{"location":"binder/#openapi_first.binder--core-responsibility","title":"Core responsibility","text":"Those concerns belong to other layers or tooling.
"},{"location":"binder/#openapi_first.binder.bind_routes","title":"bind_routes","text":"bind_routes(app: FastAPI, spec: Dict[str, Any], routes_module: Any) -> None\n Bind OpenAPI operations to FastAPI routes.
Iterates through the OpenAPI specification paths and methods, resolves each operationId to a handler function, and registers a corresponding APIRoute on the FastAPI application.
"},{"location":"binder/#openapi_first.binder.bind_routes--parameters","title":"Parameters","text":"app : fastapi.FastAPI The FastAPI application instance to which routes will be added.
dictParsed OpenAPI 3.x specification dictionary.
modulePython module containing handler functions. Each handler's name MUST exactly match an OpenAPI operationId.
"},{"location":"binder/#openapi_first.binder.bind_routes--raises","title":"Raises","text":"MissingOperationHandler If an operationId is missing from the spec or if no corresponding handler function exists in the routes module.
"},{"location":"binder/#openapi_first.binder.bind_routes--behavior-guarantees","title":"Behavior guarantees","text":"Command-line interface for FastAPI OpenAPI-first scaffolding utilities.
This CLI bootstraps OpenAPI-first FastAPI applications from versioned, bundled templates packaged with the library.
"},{"location":"cli/#openapi_first.cli.available_templates","title":"available_templates","text":"available_templates() -> list[str]\n Return a list of available application templates.
"},{"location":"cli/#openapi_first.cli.copy_template","title":"copy_template","text":"copy_template(template: str, target_dir: Path) -> None\n Copy a bundled OpenAPI-first application template into a directory.
"},{"location":"client/","title":"Client","text":""},{"location":"client/#openapi_first.client","title":"openapi_first.client","text":""},{"location":"client/#openapi_first.client.OpenAPIClient","title":"OpenAPIClient","text":"OpenAPIClient(spec: Dict[str, Any], base_url: Optional[str] = None, client: Optional[httpx.Client] = None)\n OpenAPI-first HTTP client (httpx-based).
Bases: OpenAPIFirstError
Raised when an OpenAPI client operation fails.
"},{"location":"errors/","title":"Errors","text":""},{"location":"errors/#openapi_first.errors","title":"openapi_first.errors","text":""},{"location":"errors/#openapi_first.errors--openapi_firsterrors","title":"openapi_first.errors","text":"Custom exceptions for OpenAPI-first FastAPI applications.
This module defines a small hierarchy of explicit, intention-revealing exceptions used to signal contract violations between an OpenAPI specification and its Python implementation.
"},{"location":"errors/#openapi_first.errors--design-principles","title":"Design principles","text":"These errors should normally cause immediate application failure.
"},{"location":"errors/#openapi_first.errors.MissingOperationHandler","title":"MissingOperationHandler","text":"MissingOperationHandler(*, path: str, method: str, operation_id: Optional[str] = None)\n Bases: OpenAPIFirstError
Raised when an OpenAPI operation cannot be resolved to a handler.
This error occurs when: - An OpenAPI operation does not define an operationId, or - An operationId is defined but no matching function exists in the provided routes module.
This represents a violation of the OpenAPI-first contract and indicates that the specification and implementation are out of sync.
"},{"location":"errors/#openapi_first.errors.MissingOperationHandler--parameters","title":"Parameters","text":"path : str The HTTP path declared in the OpenAPI specification.
strThe HTTP method (as declared in the OpenAPI spec).
str, optionalThe operationId declared in the OpenAPI spec, if present.
"},{"location":"errors/#openapi_first.errors.OpenAPIFirstError","title":"OpenAPIFirstError","text":" Bases: Exception
Base exception for all OpenAPI-first enforcement errors.
This exception exists to allow callers, test suites, and CI pipelines to catch and distinguish OpenAPI contract violations from unrelated runtime errors.
All exceptions raised by the OpenAPI-first core should inherit from this type.
"},{"location":"loader/","title":"Loader","text":""},{"location":"loader/#openapi_first.loader","title":"openapi_first.loader","text":""},{"location":"loader/#openapi_first.loader--openapi_firstloaders","title":"openapi_first.loaders","text":"OpenAPI specification loading and validation utilities.
This module is responsible for loading an OpenAPI 3.x specification from disk and validating it before it is used by the application.
It enforces the principle that an invalid or malformed OpenAPI document must never reach the routing or runtime layers.
"},{"location":"loader/#openapi_first.loader--design-principles","title":"Design principles","text":" Bases: OpenAPIFirstError
Raised when an OpenAPI specification cannot be loaded or validated.
This error indicates that the OpenAPI document is unreadable, malformed, or violates the OpenAPI 3.x specification.
"},{"location":"loader/#openapi_first.loader.load_openapi","title":"load_openapi","text":"load_openapi(path: Union[str, Path]) -> Dict[str, Any]\n Load and validate an OpenAPI 3.x specification from disk.
The specification is parsed based on file extension and validated using a strict OpenAPI schema validator. Any error results in an immediate exception, preventing application startup.
"},{"location":"loader/#openapi_first.loader.load_openapi--parameters","title":"Parameters","text":"path : str or pathlib.Path Filesystem path to an OpenAPI specification file. Supported extensions: - .json - .yaml - .yml
dict Parsed and validated OpenAPI specification.
"},{"location":"loader/#openapi_first.loader.load_openapi--raises","title":"Raises","text":"OpenAPISpecLoadError If the file does not exist, cannot be parsed, or fails OpenAPI schema validation.
"}]}