Deploying AI into Production with FastAPI
Matt Eckerle
Software and Data Engineering Leader
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)
class User(BaseModel): username: str = Field(..., min_length=3, max_length=50) age: int
@validator('age') def age_criteria(cls, age): if age < 13: raise ValueError('User must be at least 13') return age
{"username": "john_doe", "age": 25}
Valid user: username='john_doe' age=25
{"username": "too_young", "age": 10}
Validation error for {'username': 'too_young', 'age': 10}: User must be at least 13
@app.post("/users")
def create_user(user: User):
return {"message": "User created",
"user": user.dict()}
Output:
{
"message": "User created successfully",
"user": {
"username": "john_doe",
"age": 25
}
}
Deploying AI into Production with FastAPI