Define the API Contract
Put a stable boundary around inference
FastAPI is a Python framework for building HTTP APIs. It uses Python type declarations to validate data and automatically creates an OpenAPI document, a machine-readable description of the endpoints. Define required request fields, maximum lengths, allowed model names, and a response containing the answer, model, latency, and optional source citations.
Return client errors for invalid input and service errors when inference is unavailable. Never convert every failure into HTTP 200 with an error string.
Analogy: A restaurant menu is a stable promise to customers even when the kitchen changes equipment. Your API response is that menu; Ollama is one piece of kitchen equipment.
Tip: Keep the endpoint contract independent of Ollama's raw response so the backend can change without breaking callers.