FastAPI Basics
FastAPI is a high-performance, asynchronous web framework for building APIs in Python. It relies on standard Python type hints and is built on top of Starlette ...
Definition
FastAPI is a high-performance, asynchronous web framework for building APIs in Python. It relies on standard Python type hints and is built on top of Starlette (for web routes) and Pydantic (for data validation).
Why It Exists
Traditional frameworks like Flask are synchronous and slow down when handling long-running, I/O-bound LLM requests. FastAPI supports async routes natively, provides automatic Swagger UI documentation, and parses and validates incoming payloads using Pydantic, making it the industry standard framework for AI backends.
How It Works
FASTAPI REQUEST CYCLE
┌────────┐ ───[ POST /api/chat ]───► ┌─────────────────────────┐
│ Client │ │ FastAPI Router │
└────────┘ ◄───[ 200 OK + JSON ]─── └───────────┬─────────────┘
│ Type checks payload
│ using Pydantic
▼
┌─────────────────────────┐
│ Async Handler Function │
└─────────────────────────┘
(Awaits LLM Generation)
1. **Payload Type Checking**: When a request lands, FastAPI uses Pydantic to validate the input data type definitions. If validation fails, it automatically returns a 422 Unprocessable Entity status code.
2. **Event Scheduling**: Async routes run concurrently on the Starlette event loop, allowing the server to handle thousands of concurrent requests while awaiting model generations.Real-World Example
AI startups use FastAPI to serve their models. It automatically creates interactive documentation pages at the /docs path, allowing developers to test their API endpoints directly from their web browser.
Python Example
Here is a production-style FastAPI endpoint demonstrating async generation, input validation, and Dependency Injection to verify API keys:
from fastapi import FastAPI, Depends, HTTPException, Security, status
from fastapi.security import APIKeyHeader
from pydantic import BaseModel, Field
app = FastAPI(title="AI Agent API Gateway")
# Configure authentication header check
API_KEY_NAME = "X-Agent-Key"
api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
def verify_api_key(api_key: str = Depends(api_key_header)):
"""Verifies that the correct API key is present in request headers."""
if not api_key or api_key != "secret-agent-pass":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Forbidden: Invalid or missing API credential key."
)
return api_key
# Define schema parameters using Pydantic
class GenerationRequest(BaseModel):
prompt: str = Field(..., min_length=5, description="Input query prompt")
temperature: float = Field(0.7, ge=0.0, le=1.0, description="Model sampling temperature")
@app.post("/v1/generate", tags=["AI Engine"])
async def generate_response(
request: GenerationRequest,
_auth: str = Depends(verify_api_key)
):
"""
Asynchronously processes a generation request.
FastAPI automatically validates input formatting using the GenerationRequest schema.
"""
# Represents mock async generation delay
# In production, this would be an async HTTP request to an LLM provider
import asyncio
await asyncio.sleep(0.5)
return {
"status": "success",
"output": f"Generated output for prompt: '{request.prompt}'",
"parameters": {"temperature": request.temperature}
}
if __name__ == "__main__":
import uvicorn
# In production, run this command using the terminal: uvicorn main:app --reload
print("Start the server using: uvicorn <filename>:app --reload")Interview Questions
- Beginner: Why is FastAPI highly recommended for building AI backends?
- Intermediate: What role does Pydantic play in a FastAPI application?
- Advanced: Explain how Dependency Injection works in FastAPI, using a real-world example like user authentication.
Interview Answers
- Beginner: FastAPI supports async/await natively, allowing it to handle concurrent, long-running I/O calls (like LLM API requests) efficiently. It also uses standard Python type hints to generate interactive API documentation automatically.
- Intermediate: Pydantic validates the structure and data types of incoming request payloads. If a client sends invalid data (e.g., text instead of an integer), Pydantic catches it and returns a detailed validation error, ensuring the backend function only processes clean data.
- Advanced: Dependency Injection (
Depends) allows you to inject reusable components (like database connections or authentication logic) directly into your API route handlers. For example, averify_api_keyfunction can be injected into multiple routes. FastAPI will run this function first, extract and validate the headers, and pass the returned credentials to the route handler, keeping the routing code clean and modular.
Common Mistakes
- Using blocking libraries in async routes: Calling synchronous functions (like
time.sleep()orrequests.get()) inside an async route handler. This blocks the event loop, freezing the server and negating the performance benefits of using FastAPI. Always use asynchronous client libraries likehttpxinstead.
Assignment
Create a FastAPI app with a GET route /status that returns a health check JSON payload, and a POST route /summarize that validates a text field input parameter and returns a mock summary.