Learning Outcome
5
Connect FastAPI to serving GenAI safely
4
Apply Pydantic models to validate requests
3
Recognize path vs query parameters.
2
Identify the three parts of an endpoint
1
Understand what FastAPI is and why it’s used
Imagine you stop ordering from other people’s kitchens and open your own kitchen.
Open Your Own Kitchen
You now control what can be ordered, just like creating your own API.
Set and Check Order Rules
Every order is checked, and invalid requests like “Biryani, size: banana” are rejected with a clear message.
Keep the Menu Current
When you add or change a dish, the menu updates, just like API documentation stays up to date.
Your app needs an AI feature. Putting your AI key in the app is like handing every customer your supplier
account card. What do you do instead?
Customers order at your counter, and your kitchen calls the supplier, with the card
kept safely inside.
So what do we use to build this kitchen?
What is FastAPI?
A modern Python framework for building APIs.
Type hints (student_id: int) drive validation, conversion, and docs
Built on Starlette (web) and Pydantic (validation).
Strengths:
auto validation, auto docs, high speed (async), less code
Flask needs manual validation, and Django is built for full websites. FastAPI has API features built in.
Setting Up and Running FastAPI
Install
pip install "fastapi[standard]"
Create main.py
app = FastAPI()
Run
fastapi dev main.py (or uvicorn main:app --reload)
Open
127.0.0.1:8000, /docs (Swagger, Try it out), /redoc (read-only)
Your First Endpoint
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Welcome to Itvedant API"}
The decorator sets the method and path.
The function runs on each request.
The return value becomes JSON automatically, with a 200 status.
Path vs Query Parameters
/students/abc is rejected automatically. Use int | None = None for a skippable query.
Receiving Data and Returning Errors
class Enquiry(BaseModel):
name: str
phone: str
course: str
@app.post("/enquiries")
def create_enquiry(enquiry: Enquiry):
A missing field or wrong type gets a 422, which names the failed field.
A valid request that can’t be completed: raise HTTPException(status_code=404, detail="Student not found")
It’s the same 404 you handled as a client. Now you’re the one sending it
A Simple Way to Remember
The decorator sets the route, the function does the work, and the return becomes JSON. Path says which, query says how, Pydantic says what.
A relaxed API could guess that “five” means 5. Why is rejecting it with a clear 422 better design?
Real-World Applications: Path or Query?
2
3
4
1
Profile of student 1042 → Path
Courses at the Thane branch
→ Query
Limit search to 5 results → Query
Details of the “data-science” course → Path
Why FastAPI Matters for Generative AI
02.
03.
01.
App → your FastAPI endpoint → AI provider, with the key kept safely in env vars.
Async (async def) lets one server serve many users during slow LLM calls.
Pydantic validates user input and the model’s JSON output.
FastAPI is a natural home for AI features
Summary
5
FastAPI keeps AI keys safely on your server
4
Pydantic validates POST data; bad requests get a 422
3
Path says which item; query says how to filter
2
An endpoint is a decorator, a function, and a return value
1
FastAPI builds APIs with automatic validation and docs
Quiz
A POST request is missing a required field. What does FastAPI return?
A. 200
B. 401
C. 404
D. 422
A POST request is missing a required field. What does FastAPI return?
A. 200
B. 401
C. 404
D. 422
Quiz-Answer