new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Reference · all subjects

fastapi class fundamentals

20 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

FastAPI uses Swagger UI for documentation

FastAPI automatically generates interactive API documentation using Swagger UI at /docs endpoint.

FastAPI uses ReDoc for alternative documentation

FastAPI automatically generates alternative interactive API documentation using ReDoc at /redoc endpoint.

FastAPI is based on OpenAPI and JSON Schema standards

FastAPI is based on and fully compatible with the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

FastAPI standard dependencies

When installing FastAPI with `uv add "fastapi[standard]"`, it includes: email-validator (Pydantic), httpx (Starlette/testing), jinja2 (Starlette/templating), python-multipart (Starlette/forms), uvicorn (server), and fastapi-cli[standard].

FastAPI is built on Starlette and Pydantic

FastAPI stands on the shoulders of giants: Starlette for the web parts and Pydantic for the data parts.

FastAPI basic example with path and query parameters

Example showing FastAPI fundamentals: from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q}

FastAPI async example

FastAPI supports async functions using async def: from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") async def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q}

FastAPI example with request body

Example showing how to use Pydantic BaseModel for request body: from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float is_offer: bool | None = None @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q} @app.put("/items/{item_id}") def update_item(item_id: int, item: Item): return {"item_name": item.name, "item_id": item_id}

FastAPI decorator methods

FastAPI app instance supports HTTP method decorators: @app.get(), @app.put(), and others corresponding to HTTP methods.

FastAPI supports WebSockets

FastAPI supports WebSockets through Starlette.

fastapi dev command for development

The `fastapi dev` command automatically detects the FastAPI app in main.py and starts a server using Uvicorn with auto-reload enabled for local development.

FastAPI servers parameter

FastAPI accepts a servers parameter when creating an application instance. This parameter allows you to provide a custom list of alternative servers for the API. When a root_path is set and you provide custom servers, FastAPI automatically inserts a server with the root_path URL at the beginning of the servers list.

FastAPI root_path parameter

FastAPI accepts a root_path parameter when creating an application instance. This parameter sets the root path for the application, which is used when the application is behind a proxy with a stripped path prefix. The root_path is part of the ASGI specification and can be set by passing it to the FastAPI constructor or via the --root-path command-line option.

FastAPI root_path_in_servers parameter

FastAPI accepts a root_path_in_servers parameter when creating an application instance. When set to False, this parameter prevents FastAPI from automatically including a server with the root_path URL in the OpenAPI schema. By default, FastAPI automatically creates a server entry with the root_path URL.

Combining response_class with direct Response return

You can use the response_class parameter and also return a Response object. The response_class will be used only for documenting the OpenAPI path operation, while your Response will be used unchanged.

Automatic data conversion with response_class

When you declare a response_class with a JSON media type like JSONResponse, data returned from your path operation function is automatically converted with any Pydantic response_model declared in the path operation decorator, but is not serialized to JSON bytes with Pydantic. Instead, it is converted with jsonable_encoder and then passed to the JSONResponse class, which serializes it to bytes using Python's standard JSON library.

default_response_class parameter for FastAPI

When creating a FastAPI class instance or APIRouter, you can specify which Response class to use by default using the default_response_class parameter.

JSON response performance best practice

For maximum performance with JSON responses, use a response model and do not declare a response_class in the path operation decorator.

FastAPI default response is JSON

By default, FastAPI returns JSON responses. When you declare a response model, FastAPI uses Pydantic to serialize the data to JSON.

response_class parameter in path operation decorator

You can declare the Response class you want to use by specifying the response_class parameter in the path operation decorator. The content returned from the path operation function will be wrapped in this Response.

Give your agent this brain