How loseall/openai-http works
1. Top-level shape
- This is a Python 3.12+ service package, not just a library: it exposes an OpenAI v1-compatible HTTP API and can also be embedded as a server via run_server().
- The main runtime is FastAPI/Uvicorn-style ASGI, with the package centered under openai_http/.
2. Core request path
- Requests enter FastAPI, pass through middleware for request IDs and structured logging, then land in route handlers under openai_http/routers/.
- The handlers translate OpenAI-style HTTP payloads into backend calls, then map backend output back into OpenAI v1 response shapes defined in openai_http/schemas/.
3. API surface and routing
- Implemented endpoints are the core OpenAI compatibility set: /v1/models, /v1/chat/completions, /v1/completions, /v1/embeddings, and /health.
- Audio and image routes are already registered, but intentionally return 501 stubs.
- The router layout mirrors the product surface, so an architecture diagram should show separate endpoint groups rather than a single monolithic API layer.
4. Backend abstraction boundary
- The most important architectural seam is openai_http/backends/base.py, which defines BackendBase.
- The server is backend-agnostic: it expects a backend object that can generate completions, stream tokens, list/get models, and optionally implement embeddings and tool/function calls.
- Default behavior for unsupported capabilities is 501, which makes backend support explicit rather than implicit.
5. Pluggable inference implementations
- The repository ships with a mock_backend for local testing and compatibility validation.
- The example backend in examples/transformers-backend/ demonstrates a real model-backed implementation using Hugging Face Transformers, including tool calling support for chat.
- This means the architecture is “HTTP facade + pluggable inference engine,” not a fixed model-serving stack.
6. Streaming and SSE
- Chat/completions support both normal and streaming responses.
- Streaming is implemented as a first-class path, likely using Server-Sent Events semantics from the router layer down through backend generate_stream() yielding chunks.
- In a diagram, streaming should be shown as a separate response mode from standard request/response.
7. Queueing and GPU serialization
- openai_http/queue.py provides request throttling/serialization, described as a semaphore-based queue that prevents concurrent GPU access.
- This is a key operational boundary: route handlers acquire queue capacity before invoking backend inference.
- When the queue depth is exceeded, the service returns 429, so the queue acts both as protection and backpressure.
8. Configuration and runtime control
- openai_http/config.py loads TOML configuration with environment variable overrides using the OPENAI_HTTP__... convention.
- Server host/port, auth, queue depth, and observability settings are all runtime-configurable, so deployment concerns are externalized from code.
- For architecture purposes, config sits beside the server bootstrap layer, not inside request handlers.
9. Authentication and error normalization
- openai_http/auth.py handles API-key style authorization when enabled.
- openai_http/errors.py centralizes OpenAI-shaped error responses, so failures are normalized to the API contract rather than leaking raw framework errors.
- This creates a boundary between internal exceptions and externally visible API semantics.
10. Schemas and contract stability
- openai_http/schemas/ contains the Pydantic models for chat, completions, embeddings, common objects, and model metadata.
- These schemas are the contract layer between routers and backends, and they are what keep the service aligned with the OpenAI v1 format.
- If drawing the architecture, put schemas as the shared “data model” block spanning handlers and backend adapters.
11. Observability
- Logging is structured JSON/text with request IDs, and there is an observability package for metrics integration.
- README indicates OpenTelemetry metrics exposed via a Prometheus endpoint, so there are two notable observability outputs: logs and metrics.
- This is operationally important because it means request tracing and service health are designed into the server, not added externally.
12. Health and lifecycle
- There is a dedicated health route and a server lifecycle path in app.py / _server.py.
- The server likely performs backend setup/teardown during lifespan events, which is important for model loading or resource cleanup in real backends.
- This is the place to show startup/shutdown behavior in a diagram.
13. Testing and compatibility validation
- Testing is stratified into unit, integration, contract, and SDK compatibility tests.
- The SDK suite is especially important: it runs the official openai Python client against the server, validating real-world API compatibility.
- This repo is therefore organized around contract conformance, not only internal correctness.
14. Specs and examples as architecture documentation
- specs/ contains explicit feature specs, API contracts, plans, and data models, which act as design-time documentation for implemented and planned capabilities.
- examples/transformers-backend/ is the canonical real-world extension example and should be treated as a reference integration, not part of the core runtime.
- For an architecture diagram, these belong on the side as “reference implementations / design artifacts,” not in the request path.
15. Key boundaries to show
- External client → FastAPI routes → auth/logging/queue → backend adapter → model/inference engine.
- Cross-cutting services: config, error formatting, observability.
- Non-core but visible surfaces: OpenAI SDK contract tests, mock backend, and example Transformers backend.
Main entry points: app.py App · base.py Backend base · schemas Schemas · chat.py Chat API
Components 20
Runtime
ASGI service, request handling, and cross-cutting concerns
- App · Builds the FastAPI service and wires routers, middleware, and lifespan
openai_http/app.py - Server · Process entry and run_server lifecycle control
openai_http/_server.py - Config · Loads TOML and environment overrides for deployment settings
openai_http/config.py - Logging · Structured request logging and request ID plumbing
openai_http/_logging.py - Auth · Optional authorization gate for incoming requests
openai_http/auth.py - Queue · Serializes GPU-bound inference and returns backpressure when saturated
openai_http/queue.py - Errors · Normalizes failures into OpenAI-shaped error responses
openai_http/errors.py - Chat API · OpenAI chat completions endpoint with streaming support
openai_http/routers/chat.py - Completions API · Legacy completions endpoint and response translation
openai_http/routers/completions.py - Embeddings API · Embeddings endpoint and request/response mapping
openai_http/routers/embeddings.py - Models API · Lists and retrieves model metadata
openai_http/routers/models.py - Health · Service health endpoint
openai_http/routers/health.py - Audio/Image · Registered compatibility routes that intentionally return 501
openai_http/routers/audio.py
Backend
Pluggable inference boundary and implementations
- Backend base · Defines the server-side inference contract and optional capabilities
openai_http/backends/base.py - Mock backend · Local compatibility backend for tests and validation
openai_http/backends/mock_backend.py
Contracts
Schemas, API surface, and validation
- Schemas · Shared OpenAI v1 data models for chat, completions, embeddings, and models
openai_http/schemas
Specs & examples
Reference implementations and design artifacts
- Transformers backend · Reference Hugging Face backend with model and tool-calling support
examples/transformers-backend/transformers_backend.py - Specs · Feature specs, contracts, plans, and data models
specs
Tests
Compatibility and correctness validation
- SDK tests · Official OpenAI Python client compatibility validation
tests/sdk - Unit & integration · Backend, router, validation, and library-level correctness checks
tests/unit
Connections 27
- Server → App: creates · Bootstrap constructs and runs the ASGI app
- App → Config: loads · Runtime settings are sourced at startup
- App → Logging: installs · Middleware adds request IDs and structured logs
- App → Auth: guards · Requests pass through optional API-key authorization
- App → Queue: throttles · Inference requests acquire queue capacity before backend calls
- App → Errors: normalizes · Unhandled failures are mapped to API-shaped responses
- App → Chat API: routes · Chat completions endpoint is mounted on the app
- App → Completions API: routes · Completions endpoint is mounted on the app
- App → Embeddings API: routes · Embeddings endpoint is mounted on the app
- App → Models API: routes · Models endpoint is mounted on the app
- App → Health: routes · Health endpoint is mounted on the app
- App → Audio/Image: stubs · Audio and image routes are registered as 501-compatible stubs
- Chat API → Schemas: parses/returns · OpenAI payloads are converted to and from shared schemas
- Completions API → Schemas: parses/returns · Completions requests and responses use shared schemas
- Embeddings API → Schemas: parses/returns · Embeddings requests and responses use shared schemas
- Models API → Schemas: returns · Model metadata is serialized via shared schema objects
- Chat API → Backend base: calls · Router delegates generation and streaming to backend adapter
- Completions API → Backend base: calls · Completions handler delegates to backend adapter
- Embeddings API → Backend base: calls · Embeddings handler delegates when supported
- Models API → Backend base: calls · Model listing and lookup come from backend capabilities
- Backend base → Mock backend: implemented by · Mock backend provides a default local implementation
- Backend base → Transformers backend: reference impl · Example backend demonstrates a real model-serving integration
- Backend base → Errors: raises 501 · Unsupported capabilities are surfaced as explicit not-implemented responses
- Specs → Schemas: documents · Specs describe the API and data model contracts
- Specs → Backend base: documents · Design artifacts define the extension boundary
- SDK tests → App: validates · OpenAI SDK compatibility is exercised against the service
- Unit & integration → Backend base: verifies · Lower-level tests cover adapter contract and library behavior