Handling different
input types in
FastAPI
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Matt Eckerle
Software and Data Engineering Leader
Restaurant vs API
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Validation flow
Incoming data via request
Input data validation happens using
Pydantic
Process different types of data as per
model requirements
Processed input sent to the model
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Comment moderation system
class CommentMetrics(BaseModel):
length: int
user_karma: int
report_count: int
class CommentText(BaseModel):
content: str
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Endpoint for floating point numbers
app = FastAPI()
@[Link]("/predict")
def predict_score(data: CommentMetrics):
features = [Link]([
[Link],
data.user_karma,
data.report_count
])
model = CommentScorer()
prediction = [Link](features)
return {"prediction": round(prediction, 2),
"input": [Link]()}
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Endpoint for textual input
Output for comment: Sign up for free
@[Link]("/analyze_text")
def analyze(comment: CommentText):
{
forbidden = ["spam", "hate", "free"
"issues": ["free", "sign up"],
"fake", "sign up"]
"needs_moderation": 2
text_lower = [Link]()
}
issues = [word for word in forbidden
if word in text_lower]
return {
"issues": issues,
"needs_moderation": len(issues)
}
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Let's practice!
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Input validation in
FastAPI
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Matt Eckerle
Software and Data Engineering Leader
Validating input data
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Why validate the input?
Validation for data integrity
Prevent errors in the application
Integrates with Pydantic
Provided powerful tools for data validation
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Pydantic for pre-defined function
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Custom validation with pydantic
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Graceful error reporting
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Pydantic field validators
User registration endpoint
Validating the username entered by users:
from pydantic import BaseModel, Field
class User(BaseModel):
username: str = Field(..., min_length=3, max_length=50)
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Adding custom validators
class User(BaseModel):
username: str = Field(...,
min_length=3,
max_length=50)
age: int
@field_validator('age')
def age_criteria(cls, age):
if age < 13:
raise ValueError('User must be at least 13')
return age
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Custom validators in action
Valid request:
{"username": "john_doe", "age": 25}
Valid user: username='john_doe' age=25
Invalid request:
{"username": "too_young", "age": 10}
Validation error for {'username': 'too_young', 'age': 10}: User must be at least 13
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Putting it all together
Field validator for username
Custom validator for age
Error message if failing validation
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Putting it all together
@[Link]("/users")
def create_user(user: User):
return {"message": "User created",
"user": user.model_dump()}
Output:
{
"message": "User created successfully",
"user": {
"username": "john_doe",
"age": 25
}
}
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Let's practice!
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Loading a pre-
trained model
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Matt Eckerle
Software and Data Engineering Leader
Current structure
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Challenge with loading models
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Load models before the request
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Loading the model
from fastapi import FastAPI
sentiment_model = None
def load_model():
global sentiment_model
sentiment_model = SentimentAnalyzer("trained_model.joblib")
print("Model loaded successfully")
load_model()
Model loaded successfully
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
FastAPI lifespan event
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
FastAPI lifespan event
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup: Load the ML model
load_model()
yield
app = FastAPI(lifespan=lifespan)
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Health checks
Curl command:
@[Link]("/health") curl -X GET \
def health_check(): "[Link] \
if sentiment_model is not None: -H "accept: application/json"
return {"status": "healthy",
"model_loaded": True} Output:
return {"status": "unhealthy",
"model_loaded": False} {
"status": "healthy",
"model_loaded": true
}
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Let's practice!
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Returning structured
prediction response
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I
Matt Eckerle
Software and Data Engineering Leader
Challenges with deploying models
1. Accept input data properly
2. Validate incoming data and handle errors
3. Make predictions
4. Return well-structured responses
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Defining request structure
from pydantic import BaseModel
class PredictionRequest(BaseModel):
text: str
class PredictionResponse(BaseModel):
text: str
sentiment: str
confidence: float
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Creating the prediction endpoint
@[Link]("/predict") Input JSON:
def predict_sentiment(request: PredictionRequest):
if sentiment_model is None: {"text": "This movie was fantastic!"}
raise HTTPException(
status_code=503,
detail="Model not loaded"
)
Response:
result = sentiment_model([Link])
return PredictionResponse(
{
text=[Link],
"text": "This movie was fantastic!",
sentiment=result[0]["label"],
"sentiment": "POSITIVE",
confidence=result[0]["score"]
"confidence": 0.95
)
}
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Error handling
Response when model fails to predict
try:
result = sentiment_model([Link])
{
return PredictionResponse(
"detail": "Prediction failed",
text=[Link],
"status_code": 500
sentiment=result[0]["label"],
}
confidence=result[0]["score"]
)
except Exception:
raise HTTPException(
status_code=500,
detail="Prediction failed"
)
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Testing the endpoint
# Example request
import requests
response = [Link](
"[Link]
json={"text": "Great product!"}
)
print([Link]())
{
"text": "Great product!",
"sentiment": "POSITIVE",
"confidence": 0.998
}
DEPLOYING AI INTO PRODUCTION WITH FASTAPI
Let's practice!
D E P L OY I N G A I I N T O P R O D U C T I O N W I T H FA S TA P I