Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI
Not creating an instance of FastAPI
✗ Incorrect
You need to import FastAPI and create an instance of it to start your app.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST or PUT instead of GET for a read endpoint
Forgetting the decorator
✗ Incorrect
The decorator @app.get defines a GET HTTP method endpoint.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect names like DependsOn or DependsWith
Not importing Depends
✗ Incorrect
Use Depends to declare dependencies in FastAPI endpoints.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of POST for creation
Using wrong parameter type like str
✗ Incorrect
Use @app.post to define a POST endpoint and use the Pydantic model Item as the parameter type.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong import or instance name
Using wrong decorator for the endpoint
✗ Incorrect
Import FastAPI, create an app instance, and define a GET endpoint with @app.get.