Module 14 Lesson 1: API Design with FastAPI
Connecting to the Frontend. How to wrap your LangChain apps in a professional REST API using FastAPI.
Serving Your AI: The FastAPI Bridge
A Python script running in your terminal is not a "Product." To build a website or a mobile app, your AI must live behind a REST API. FastAPI is the industry-standard choice for AI backends because it is extremely fast and natively supports asynchronous operations (async), which is perfect for LLM calls.
1. Why FastAPI?
- Concurrency: It can handle multiple users at once without blocking.
- JSON Validation: It uses Pydantic (Module 11) to ensure the frontend sends the right data.
- Speed: It is one of the fastest Python frameworks available.
2. The Basic Endpoint
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class ChatRequest(BaseModel):
input: str
@app.post("/chat")
async def chat(request: ChatRequest):
# Here is where your LangChain logic lives
response = chain.invoke({"input": request.input})
return {"reply": response}
3. Streaming Over the Web
One of the best features of FastAPI + LangChain is Streaming. You can send tokens to the browser as they are generated. We use StreamingResponse.
from fastapi.responses import StreamingResponse
@app.get("/stream")
async def stream_chat(query: str):
async def generate():
async for chunk in model.astream(query):
yield chunk.content
return StreamingResponse(generate(), media_type="text/event-stream")
4. Visualizing the Web Stack
graph LR
U[User Browser] -->|Post /chat| F[FastAPI Server]
F -->|Invoke| LC[LangChain Object]
LC -->|API Request| O[OpenAI/Cloud]
O -->|Tokens| F
F -->|JSON| U
5. Engineering Tip: CORS
When you build your frontend (React/Next.js), it will try to talk to your FastAPI server. By default, browsers block this for security. You must enable CORS (Cross-Origin Resource Sharing) in your FastAPI code.
Key Takeaways
- FastAPI is the bridge between your AI logic and the world.
- POST requests are preferred for sending large prompt data.
StreamingResponseenables the "ChatGPT-style" typing effect in your UI.- Pydantic ensures your API stays stable and well-documented.