Body - Fields
Part of: FastAPI Basics
Learn to add validation and metadata to Pydantic model fields using Field(). Master field-level constraints, descriptions, and advanced validation.
What You'll Learn
- Import and use Field from pydantic for model attributes
- Add validation constraints to model fields (gt, max_length, etc.)
- Include metadata like titles and descriptions for better documentation
- Understand the relationship between Field, Query, Path, and Body
Theory and Concepts
Body - Fields
šÆ What You'll Learn
This lesson follows the official FastAPI "Body - Fields" tutorial exactly. You'll learn how to use Pydantic's Field() function to add validation and metadata to model attributes, making your APIs more robust and well-documented.
By the end of this lesson, you'll understand how to:
- Import and use Field from pydantic for model attributes
- Add validation constraints to model fields (gt, max_length, etc.)
- Include metadata like titles and descriptions for better documentation
- Understand the relationship between Field, Query, Path, and Body
š Import Field
First, you need to import Field from pydantic:
[Code Example]
ā ļø Important: Notice that Field is imported directly from pydantic, not from fastapi like Query, Path, and Body.
š§ Declare Model Attributes with Field
You can use Field with model attributes to add validation and metadata:
[Code Example]
Field Parameters
Field works the same way as Query, Path, and Body - it accepts the same validation parameters:
- Validation constraints: gt, ge, lt, le, min_length, max_length, etc.
- Metadata: title, description, example, etc.
- Default values: default, default_factory
šÆ Complete Example
Following the official FastAPI tutorial:
[Code Example]
Request Body Structure
With Body(embed=True), the request expects:
[Code Example]
š Key Concepts
Field vs Function Parameters
Notice the similarity between model attributes with Field and function parameters:
Model attribute with Field:
[Code Example]
Function parameter with Path:
[Code Example]
Both follow the same pattern: parameter: type = ValidationFunction(constraints)
Validation Benefits
Field validation provides:
- Automatic validation: Invalid data returns HTTP 422 with detailed error messages
- Type conversion: Automatic conversion between compatible types
- Documentation: Metadata appears in OpenAPI schema and interactive docs
- IDE support: Better autocomplete and type checking
Technical Details
- Query, Path, Body create objects of subclasses of Param class
- Param is a subclass of Pydantic's FieldInfo class
- Field returns an instance of FieldInfo directly
- All use the same underlying validation system
š” Best Practices
Validation Constraints
[Code Example]
Descriptive Metadata
[Code Example]
Optional Fields with Defaults
[Code Example]
š Add Extra Information
You can include additional metadata in Field that will appear in the generated JSON Schema:
[Code Example]
ā ļø Warning: Extra keys passed to Field will be present in the OpenAPI schema. Some OpenAPI tools may not work with non-standard keys.
šÆ Validation Examples
Valid Request
[Code Example]
ā Result: Success - all validations pass
Invalid Requests
Negative price:
[Code Example]
ā Result: HTTP 422 - "ensure this value is greater than 0"
Description too long:
[Code Example]
ā Result: HTTP 422 - "ensure this value has at most 300 characters"
š What's Next?
In the next lesson, you'll learn about Body - Nested Models, where you'll discover how to create complex nested Pydantic models with lists, sets, and deeply nested structures.
š Additional Resources
- FastAPI Official Tutorial - Body - Fields
- Pydantic Field Documentation
- Pydantic Validators
- OpenAPI Schema Documentation
Helpful Hint
Import Field from pydantic (not fastapi). Use Field() to add validation like gt=0 for positive numbers and max_length for strings.