oag-fastapi-server
Python FastAPI server generator for OpenAPI 3.x specs.
Takes an IrSpec from oag-core and produces FastAPI route stubs with Pydantic v2 models.
Layout
This generator currently supports modular layout only. It produces separate files per concern:
| File | Description |
|---|---|
models.py |
Pydantic v2 models for all schemas (request/response bodies) |
routes.py |
FastAPI route stubs with proper type annotations |
sse.py |
Server-Sent Events utilities using StreamingResponse |
main.py |
FastAPI app entry point |
When scaffold generation is enabled (default), these are also created:
| File | Description |
|---|---|
pyproject.toml |
uv-compatible project config with FastAPI and uvicorn dependencies |
conftest.py |
pytest fixture with async httpx test client (optional, scaffold.tests) |
test_routes.py |
Per-operation pytest tests (optional, scaffold.tests) |
When scaffold.tests is enabled (default), pyproject.toml includes a [dependency-groups] section (PEP 735) with pytest, pytest-asyncio, and httpx as dev dependencies. The generated tests cover:
- Route existence (not 404)
- Stub returns 500 (NotImplementedError)
- Input validation returns 422 (for operations with request body)
- Unknown path returns 404
Key features
- Pydantic v2 models — All OpenAPI schemas are converted to Pydantic models with proper field types, descriptions, and validation
- Type-safe routes — Every FastAPI route is fully annotated with request/response types
- SSE streaming — Server-Sent Events endpoints use
StreamingResponsewith async generators (no external dependencies) - uv-compatible —
pyproject.tomluses PEP 735 dependency groups; run withuv sync && uv run pytest - Absolute imports — Generated code uses absolute imports (
from models import ...) so tests work without package installation - Stub implementation — Routes raise
NotImplementedError; you fill in the business logic
Generated route structure
For a GET /pets operation, the generator produces:
"""
List all pets
"""
# TODO: Implement this endpoint
For SSE endpoints (detected by text/event-stream content type), the generator produces:
"""
Stream chat responses via SSE
"""
# TODO: Implement your SSE logic here
yield
return
Depends on
oag-core— parser, IR, andCodeGeneratortrait