How do you implement dependency injection in FastAPI for database connections?
Use FastAPI's Depends() system. Create a get_db() function that yields a database session, then inject it: async def endpoint(db: Session = Depends(get_db)). For AI apps, use this pattern for LLM clients, vector stores, and connection pools — ensures resources are properly initialized and cleaned up per request, and makes testing easy by overriding dependencies in tests.
How do you stream LLM responses with FastAPI?
Use StreamingResponse with an async generator. The generator yields tokens as they arrive from the LLM API: async def stream(): async for chunk in openai_client.stream(): yield chunk.text. Return StreamingResponse(stream(), media_type="text/event-stream"). On the frontend, read with EventSource or fetch with streaming. This reduces time-to-first-token from 5s+ to under 500ms perceived latency.
What is Pydantic V2 and how does it change FastAPI development?
Pydantic V2 rewrites the core in Rust, making validation 5–50x faster. Key changes: model_config instead of class Config, @model_validator replaces @validator, field_validator for field-level validation, and model_dump() replaces dict(). For FastAPI, this means faster request validation and serialization — critical for high-traffic AI APIs processing thousands of requests per minute.
How do you handle background tasks in FastAPI for AI processing?
Use FastAPI BackgroundTasks for lightweight tasks (email notifications after AI processing). For heavy AI workloads (embedding generation, model inference), use a proper task queue: Celery + Redis or ARQ. The pattern: receive request → validate → enqueue task → return task_id immediately → client polls /tasks/{task_id} for results. This prevents request timeouts on long-running AI operations.
How do you implement rate limiting in a FastAPI AI API?
Use slowapi (FastAPI wrapper for limits library) for per-IP rate limiting, or implement custom middleware using Redis for per-user limits. For AI APIs specifically: limit by user tier (free: 10 req/min, paid: 100 req/min), by model (expensive GPT-4: stricter limits), and use token counting for OpenAI costs. Decorate with @limiter.limit("10/minute") and return 429 with Retry-After header.
Explain middleware in FastAPI and give an AI use case.
Middleware intercepts every request and response. Add with app.add_middleware(). In AI APIs, use middleware for: (1) Request ID injection for tracing LLM calls, (2) Latency logging to identify slow embeddings, (3) API key authentication, (4) Cost tracking by counting OpenAI tokens per request, (5) CORS for frontend AI tools. Keep middleware lightweight — heavy processing should happen in background tasks.
How do you write tests for a FastAPI endpoint that calls OpenAI?
Use TestClient (sync) or AsyncClient from httpx (async). Mock external AI calls with unittest.mock.AsyncMock or pytest-mock. Pattern: override the get_llm_client dependency in app.dependency_overrides, return a mock that returns predictable responses. Test: (1) happy path, (2) OpenAI timeout/error handling, (3) rate limiting, (4) input validation. Never call real APIs in unit tests — use fixtures with example responses.
What is the difference between response_model and model_validate in FastAPI?
response_model tells FastAPI to validate and serialize the response through a Pydantic model — it filters out extra fields and ensures type safety. model_validate is a Pydantic method to create a model instance from a dict. In AI APIs, use response_model to strip internal data (embeddings, raw LLM outputs) before returning to clients. This also generates correct OpenAPI documentation.
How do you deploy FastAPI with Docker for production?
Use multi-stage Docker build: Stage 1 (builder) installs dependencies, Stage 2 (production) copies only necessary files. Run with Uvicorn + Gunicorn: CMD ["gunicorn", "main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker"]. Use --workers = (2 × CPU cores) + 1. For AI APIs with heavy model loading, use 1 worker per GPU and load models at startup in lifespan events, not per-request.
Know your weak spots before the interview
10-question AI readiness assessment → personalized study plan.
Take Free Assessment →Unlock 3,000+ Interview Questions
Get full access to all interview questions with detailed answers, explanations, and real company context
Unlock Full Access →