0
0
LangChainframework~10 mins

FastAPI integration patterns in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import FastAPI and create an app instance.

LangChain
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
AResponse
BRequest
CFastAPI
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI
Not creating an instance of FastAPI
2fill in blank
medium

Complete the code to define a GET endpoint that returns a greeting.

LangChain
@app.[1]("/")
async def read_root():
    return {"message": "Hello World"}
Drag options to blanks, or click blank then click option'
Apost
Bdelete
Cput
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or PUT instead of GET for a read endpoint
Forgetting the decorator
3fill in blank
hard

Fix the error in the code to correctly inject a dependency using Depends.

LangChain
from fastapi import Depends

def get_token():
    return "token123"

@app.get("/items")
async def read_items(token: str = [1](get_token)):
    return {"token": token}
Drag options to blanks, or click blank then click option'
ADependsWith
BDepends
CDependsOn
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect names like DependsOn or DependsWith
Not importing Depends
4fill in blank
hard

Fill both blanks to create a POST endpoint that accepts a Pydantic model and returns it.

LangChain
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float

@app.[1]("/items")
async def create_item(item: [2]):
    return item
Drag options to blanks, or click blank then click option'
Apost
BItem
Cget
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for creation
Using wrong parameter type like str
5fill in blank
hard

Fill all three blanks to create a FastAPI app with a startup event and a simple GET endpoint.

LangChain
from fastapi import [1]

app = [2]()

@app.[3]("/status")
async def status():
    return {"status": "ok"}

@app.on_event("startup")
async def startup_event():
    print("App is starting")
Drag options to blanks, or click blank then click option'
AFastAPI
Cget
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong import or instance name
Using wrong decorator for the endpoint