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 · Tutorial · all subjects

request-bodies/field-validation

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

Field import source

Field is imported directly from pydantic, not from fastapi. This is different from Query, Path, and Body which are imported from fastapi.

Field works same as Query, Path, Body

Field works the same way as Query, Path and Body, and has all the same parameters for declaring validation and metadata in Pydantic model attributes.

Query, Path, Body are functions returning special classes

Query, Path, and others imported from fastapi are actually functions that return special classes. Query, Path and others create objects of subclasses of a common Param class, which is itself a subclass of Pydantic's FieldInfo class. Body returns objects of a subclass of FieldInfo directly.

Field returns FieldInfo instance

Pydantic's Field returns an instance of FieldInfo, the same base class used by Query, Path, Body and other parameter validators.

Extra information in Field adds to JSON Schema

You can declare extra information in Field, Query, Body, and other validators. This extra information will be included in the generated JSON Schema.

Extra keys in Field appear in OpenAPI schema

Extra keys passed to Field will be present in the resulting OpenAPI schema for your application. Keys that are not part of the OpenAPI specification may cause some OpenAPI tools, such as the OpenAPI validator, to fail validation.

Model attributes with Field have same structure as path operation parameters

Each model attribute with a type, default value and Field has the same structure as a path operation function parameter, with Field instead of Path, Query and Body.

List fields with type parameter

To declare a list with a specific element type in Python, use square brackets with the type inside, such as `list[str]` for a list of strings. This syntax works in Pydantic model attributes and tells FastAPI what type of elements the list will contain.

Set types for unique items

You can declare an attribute as a `set` type (for example, `tags: set[str]`) when you need unique, non-repeating items. FastAPI will convert any duplicate data in the request to a set of unique items, and output the data as a set of unique items.

Nested Pydantic models in request bodies

Each attribute of a Pydantic model can have a type that is itself another Pydantic model. This allows you to declare deeply nested JSON objects with specific attribute names, types, and validations. FastAPI will validate, convert, and document all nested levels automatically.

Pydantic HttpUrl type for URL validation

Instead of declaring a field as `str`, you can use Pydantic's `HttpUrl` type to ensure the string is a valid URL. FastAPI will validate that the value is a valid URL and document it as such in the JSON Schema and OpenAPI documentation.

Lists of submodels in request bodies

You can declare an attribute as a list of Pydantic models, such as `images: list[Image]`. FastAPI will expect, convert, validate, and document a JSON body containing an array of objects matching that model's schema.

Arbitrarily deeply nested models

FastAPI and Pydantic support arbitrarily deep nesting of models. For example, an `Offer` model can contain a list of `Item` models, and each `Item` can contain an optional list of `Image` models. All levels are validated, documented, and supported by editor autocomplete.

Pure list request bodies

If the top-level JSON body is a JSON array, you can declare the path operation function parameter as `list[Model]` (for example, `images: list[Image]`). FastAPI will treat the entire request body as a list of that model type.

Dictionary request bodies with typed keys and values

You can declare a request body as a `dict` with specific key and value types, such as `dict[int, float]`. You don't need to know the valid field names beforehand. FastAPI will accept any dict as long as the keys and values match the declared types. JSON only supports string keys, but Pydantic automatically converts string representations of integers to actual integers.

JSON only supports string keys

JSON objects only support string keys natively. However, Pydantic has automatic data conversion, so if you declare a dict with integer keys, Pydantic will accept strings from JSON clients that contain pure integers and convert them to actual integers.

Nested model editor support

When using Pydantic models instead of plain dicts, FastAPI provides editor autocomplete and type checking everywhere, including for items inside lists and nested objects. This works automatically without additional configuration.

Give your agent this brain