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

parameter classes

12 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 supports form data, files, headers, and cookies

FastAPI can declare parameters from different places: headers, cookies, form fields, and files.

Union vs Optional for type hints

Both Union and Optional are equivalent and identical under the hood. Union[SomeType, None] is recommended over Optional[SomeType] because the word 'optional' can incorrectly imply that a value is optional, when it actually only means the value can be None. A parameter with Optional[str] or Union[str, None] annotation without a default value is still required, not optional—it just accepts None as a valid value.

Modern Python pipe operator for unions

In Python type annotations, you can use the pipe operator (|) to define unions of types, for example str | None instead of Union[str, None] or Optional[str]. This is the modern preferred syntax and requires no import from typing.

Cookie parameter models with Pydantic

You can create a Pydantic model to declare a group of related cookies together. This allows you to reuse the model in multiple places and declare validations and metadata for all parameters at once. FastAPI will extract data for each field from the cookies received in the request and provide you with the Pydantic model you defined. This feature is supported since FastAPI version 0.115.0.

Cookie parameter models apply to Query, Header too

The same technique for declaring parameter models as cookies also applies to Query and Header parameter classes.

Forbid extra cookies with Pydantic model configuration

You can use Pydantic model configuration to forbid extra cookie fields by setting `extra` to `forbid`. When a client sends additional cookies not defined in the model, they receive an error response with validation error details indicating the extra cookie is not allowed.

Cookie parameter declaration syntax

Cookie parameters are declared using the same structure as Path and Query parameters. You can define default values and additional validation or annotation parameters for Cookie parameters.

Cookie is a Param subclass

Cookie is a sister class of Path and Query. It inherits from the same common Param class.

Cookie must be imported explicitly

To declare cookies, you must use Cookie imported from fastapi. If you do not use Cookie, parameters will be interpreted as Query parameters instead.

Query, Path, and Cookie are functions returning special classes

When you import Query, Path, Cookie and other items from fastapi, they are actually functions that return special classes, not the classes themselves.

Cookie parameters work like Query and Path parameters

Cookie parameters follow the same general pattern and usage as Query and Path parameters.

Browser cookies limitation in API documentation UI

The API documentation UI at /docs uses JavaScript and cannot send cookies when executing requests, even if you fill in the data and click Execute. This results in an error message as if no values were entered.

Give your agent this brain