Deploying AI into Production with FastAPI
Matt Eckerle
Software and Data Engineering Leader
http://localhost:8000/users/? [email protected]
:8000
: The port number. FastAPI typically uses 8000 by default./users/
: The path to the specific endpoint we're accessing.?
: Indicates the start of the query parameters.HTTP/1.1 200 OK
date: Fri, 18 Oct 2024 12:34:56 GMT
server: uvicorn
content-length: 76
content-type: application/json
{
"username": "johndoe",
"email": "[email protected]",
"age": 30
}
User
from pydantic import BaseModel
class User(BaseModel):
username: str
email: str
age: int
from pydantic import ValidationError
try:
invalid_user = User(username="john_doe", email="[email protected]",
age="thirty")
print("Invalid User:", invalid_user)
except ValidationError as e:
print("Validation Error:", e)
Validation Error: 1 validation error for User age
Input should be a valid integer, unable to parse string as an integer
[type=int_parsing, input_value='thirty', input_type=str]
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI()
class User(BaseModel): username: str email: str age: int
@app.post("/users", response_model=User)
async def create_user(user: User): return user
response_model
curl -X 'POST' \
'http://localhost:8000/users/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"username": "john_doe",
"email": "[email protected]",
"age": 30
}'
{
"username": "john_doe",
"email": "[email protected]",
"age": 30
}
Deploying AI into Production with FastAPI