Overview
FastAPI provides a modern, high-performance framework for building ML model APIs with automatic validation, documentation, and type safety.Implementation
API Structure
The FastAPI server (serving/fast_api.py) implements two endpoints:
serving/fast_api.py
Request/Response Models
text: List of strings to classify- Validated by Pydantic at runtime
- Automatic error messages for invalid input
probs: List of probability distributions- Each inner list sums to 1.0
- Length matches number of input texts
API Endpoints
Health Check
Predict
422 Unprocessable Entity: Invalid input format500 Internal Server Error: Model prediction failure
Testing
Tests use FastAPI’sTestClient for integration testing:
tests/test_fast_api.py
- Health check endpoint
- Prediction endpoint with validation
- Probability distribution validation
Local Development
Using Make
- Builds Docker image with
app-fastapitarget - Runs container on port 8081
- Mounts W&B API key from environment
Using Docker Directly
Manual Testing
Kubernetes Deployment
Manifest Structure
k8s/app-fastapi.yaml
- Replicas: 2 pods for high availability
- Image: Pulled from GitHub Container Registry
- Secrets: W&B API key from Kubernetes secret
- Service: ClusterIP exposes port 8080
Deployment Steps
Testing in Kubernetes
Production Considerations
Performance
Optimization strategies:- Use
uvicornworkers for concurrency - Enable model batching for throughput
- Add Redis for response caching
- Implement request queuing
Monitoring
Add observability with middleware:- Request latency (p50, p95, p99)
- Throughput (requests/second)
- Error rate (4xx, 5xx)
- Model inference time
Error Handling
Enhance error responses:API Documentation
FastAPI automatically generates docs:- Swagger UI:
http://localhost:8080/docs - ReDoc:
http://localhost:8080/redoc - OpenAPI spec:
http://localhost:8080/openapi.json
Best Practices
Validation
Use Pydantic models for all inputs/outputs
Versioning
Version APIs with path prefixes (
/v1/predict)Rate Limiting
Add
slowapi for request throttlingAuthentication
Implement API keys or OAuth for security
Comparison with Alternatives
| Feature | FastAPI | Flask | Django |
|---|---|---|---|
| Performance | Excellent | Good | Moderate |
| Type Safety | Yes | No | Partial |
| Auto Docs | Yes | No | Partial |
| Async Support | Yes | Limited | Yes |
| Learning Curve | Low | Very Low | High |
Next Steps
Streamlit UI
Build interactive web interfaces with Streamlit