Examples

Configurations

While the Configuration documentation contains all available options in detail, this page shows them in conjunction to provide different examples on how to display pydantic models and settings.

Default

This example shows the default out-of-the-box configuration of autodoc_pydantic. In contrast, it also shows how standard sphinx autodoc displays the same example code.

pydantic settings ExampleSettings[source]

Document your project settings very conveniently. Applies like wise to pydantic models.

Show JSON schema
{
   "title": "ExampleSettings",
   "description": "Document your project settings very conveniently. Applies like wise\nto pydantic models.",
   "type": "object",
   "properties": {
      "field_without_default": {
         "title": "Field Without Default",
         "type": "string"
      },
      "field_plain_with_validator": {
         "default": 100,
         "title": "Field Plain With Validator",
         "type": "integer"
      },
      "BarFoo": {
         "default": "FooBar",
         "title": "Barfoo",
         "type": "string"
      },
      "field_with_constraints_and_description": {
         "default": 5,
         "description": "Shows constraints within doc string.",
         "maximum": 100,
         "minimum": 0,
         "title": "Field With Constraints And Description",
         "type": "integer"
      }
   },
   "additionalProperties": false,
   "required": [
      "field_without_default"
   ]
}

Config:
  • env_prefix: str = foo_

  • frozen: bool = False

Fields:
Validators:
field field_plain_with_validator: int = 100

Show standard field with type annotation.

Validated by:
field field_with_constraints_and_description: int = 5

Shows constraints within doc string.

Constraints:
  • ge = 0

  • le = 100

field field_with_validator_and_alias: str = 'FooBar' (alias 'BarFoo')

Shows corresponding validator with link/anchor.

Validated by:
field field_without_default: str [Required]

Shows the [Required] marker in the signature.

validator check_max_length_ten  Β»  field_with_validator_and_alias, field_plain_with_validator[source]

Show corresponding field with link/anchor.

Entity-Relationship Diagram

This example shows the rendered output of a pydantic model including an Entity-Relationship Diagram.

pydantic model Order[source]

Order representation.

digraph "Entity Relationship Diagram created by erdantic" { graph [fontcolor=gray66, fontname="Times New Roman,Times,Liberation Serif,serif", fontsize=9, nodesep=0.5, rankdir=LR, ranksep=1.5 ]; node [fontname="Times New Roman,Times,Liberation Serif,serif", fontsize=14, label="\N", shape=plain ]; edge [dir=both]; "target.example_erdantic.Customer" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>Customer</b></td></tr><tr><td>id</td><td port="id">int</td></tr><tr><td>name</td><td port="name">str</td></tr></table>>, tooltip="target.example_erdantic.Customer&#xA;&#xA;Customer representation.&#xA;"]; "target.example_erdantic.Order" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>Order</b></td></tr><tr><td>id</td><td port="id">int</td></tr><tr><td>customer</td><td port="customer">Customer</td></tr><tr><td>products</td><td port="products">list[Product]</td></tr><tr><td>total</td><td port="total">float</td></tr></table>>, tooltip="target.example_erdantic.Order&#xA;&#xA;Order representation.&#xA;"]; "target.example_erdantic.Order":customer:e -> "target.example_erdantic.Customer":_root:w [arrowhead=noneteetee, arrowtail=nonenone]; "target.example_erdantic.Product" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>Product</b></td></tr><tr><td>id</td><td port="id">int</td></tr><tr><td>name</td><td port="name">str</td></tr><tr><td>category</td><td port="category">ProductCategory</td></tr></table>>, tooltip="target.example_erdantic.Product&#xA;&#xA;Product representation.&#xA;"]; "target.example_erdantic.Order":products:e -> "target.example_erdantic.Product":_root:w [arrowhead=crownone, arrowtail=nonenone]; "target.example_erdantic.ProductCategory" [label=<<table border="0" cellborder="1" cellspacing="0"><tr><td port="_root" colspan="2"><b>ProductCategory</b></td></tr><tr><td>id</td><td port="id">int</td></tr><tr><td>name</td><td port="name">str</td></tr></table>>, tooltip="target.example_erdantic.ProductCategory&#xA;&#xA;Product category representation.&#xA;"]; "target.example_erdantic.Product":category:e -> "target.example_erdantic.ProductCategory":_root:w [arrowhead=noneteetee, arrowtail=nonenone]; }

Show JSON schema
{
   "title": "Order",
   "description": "Order representation.",
   "type": "object",
   "properties": {
      "id": {
         "title": "Id",
         "type": "integer"
      },
      "customer": {
         "$ref": "#/$defs/Customer"
      },
      "products": {
         "items": {
            "$ref": "#/$defs/Product"
         },
         "title": "Products",
         "type": "array"
      },
      "total": {
         "title": "Total",
         "type": "number"
      }
   },
   "$defs": {
      "Customer": {
         "description": "Customer representation.",
         "properties": {
            "id": {
               "title": "Id",
               "type": "integer"
            },
            "name": {
               "title": "Name",
               "type": "string"
            }
         },
         "required": [
            "id",
            "name"
         ],
         "title": "Customer",
         "type": "object"
      },
      "Product": {
         "description": "Product representation.",
         "properties": {
            "id": {
               "title": "Id",
               "type": "integer"
            },
            "name": {
               "title": "Name",
               "type": "string"
            },
            "category": {
               "$ref": "#/$defs/ProductCategory"
            }
         },
         "required": [
            "id",
            "name",
            "category"
         ],
         "title": "Product",
         "type": "object"
      },
      "ProductCategory": {
         "description": "Product category representation.",
         "properties": {
            "id": {
               "title": "Id",
               "type": "integer"
            },
            "name": {
               "title": "Name",
               "type": "string"
            }
         },
         "required": [
            "id",
            "name"
         ],
         "title": "ProductCategory",
         "type": "object"
      }
   },
   "required": [
      "id",
      "customer",
      "products",
      "total"
   ]
}

Fields:
  • customer (target.example_erdantic.Customer)

  • id (int)

  • products (list[target.example_erdantic.Product])

  • total (float)

field customer: Customer [Required]
field id: int [Required]
field products: list[Product] [Required]
field total: float [Required]

Fields only

In this scenario everything is hidden except actual pydantic fields. Validators and model/setting config is hidden.

pydantic settings ExampleSettings[source]

Document your project settings very conveniently. Applies like wise to pydantic models.

Fields:
field field_plain_with_validator: int = 100

Show standard field with type annotation.

field field_with_constraints_and_description: int = 5

Shows constraints within doc string.

Constraints:
  • ge = 0

  • le = 100

field field_with_validator_and_alias: str = 'FooBar' (alias 'BarFoo')

Shows corresponding validator with link/anchor.

field field_without_default: str [Required]

Shows the [Required] marker in the signature.

Specifics

This section focuses rendered documentation examples of pydantic specific concepts such as model validators, required/optional fields or generic models.

Model validators

This example highlights how model validators (@model_validator or @field_validator('*')) are represented. Since they validate all fields, their corresponding field reference is replaced with a simple all fields marker which hyperlinks to the related model itself.

pydantic model ExampleValidators[source]

Show usage of asterisk and root validators.

Show JSON schema
{
   "title": "ExampleValidators",
   "description": "Show usage of asterisk and root validators.",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      },
      "email": {
         "title": "Email",
         "type": "string"
      }
   },
   "required": [
      "name",
      "email"
   ]
}

Fields:
Validators:
field email: str [Required]
Validated by:
field name: str [Required]
Validated by:
validator check_contains_letters  Β»  all fields[source]

Confirm that string contains at least one letter.

validator check_non_whitespaces  Β»  all fields[source]

Confirm that string contains non whitespace characters.

Note

By default the function signature of validators is replaced with hyperlinks to validated fields by autodoc_pydantic. You can disable this behaviour via validator-replace-signature.

Required/Optional fields

Pydantic has different ways to represent required or optional fields as described in the official documentation . The following example outlines all available combinations with the default autodoc_pydantic settings:

pydantic model RequiredOptionalField[source]

Outlines different representations of required/optional fields.

Show JSON schema
{
   "title": "RequiredOptionalField",
   "description": "Outlines different representations of required/optional fields.",
   "type": "object",
   "properties": {
      "required_standard": {
         "title": "Required Standard",
         "type": "integer"
      },
      "required_optional_with_ellipsis": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "title": "Required Optional With Ellipsis"
      },
      "required_optional_with_field": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "title": "Required Optional With Field"
      },
      "optional_standard": {
         "default": 1,
         "title": "Optional Standard",
         "type": "integer"
      },
      "optional_with_optional": {
         "anyOf": [
            {
               "type": "integer"
            },
            {
               "type": "null"
            }
         ],
         "title": "Optional With Optional"
      },
      "optional_with_default_factory": {
         "title": "Optional With Default Factory",
         "type": "integer"
      }
   },
   "required": [
      "required_standard",
      "required_optional_with_ellipsis",
      "required_optional_with_field",
      "optional_with_optional"
   ]
}

Fields:
field required_standard: int [Required]

No default value given:

required_standard: int

field required_optional_with_ellipsis: int | None [Required]

Requires either integer or None:

required_optional_with_ellipsis: Optional[int] = ...

field required_optional_with_field: int | None [Required]

Requires either integer or None:

required_optional_with_field: Optional[int] = Field(...)

field optional_standard: int = 1

Optional value with default value 1:

optional_standard: int = 1

field optional_with_optional: int | None [Required]

Optional value with default value None:

optional_with_optional: Optional[int]

field optional_with_default_factory: int [Optional]

Optional value with default factory:

optional_with_default_factory: int = Field(default_factory=lambda: 1)

Swap field name with alias

It is possible to completely replace the field name with the provided field alias when field-swap-name-and-alias is enabled:

pydantic model SwapFieldWithAlias[source]

SwapFieldWithAlias.

Show JSON schema
{
   "title": "SwapFieldWithAlias",
   "description": "SwapFieldWithAlias.",
   "type": "object",
   "properties": {
      "aliased_field": {
         "default": 5,
         "title": "Aliased Field",
         "type": "integer"
      },
      "field_without_alias": {
         "default": 5,
         "title": "Field Without Alias",
         "type": "integer"
      }
   }
}

Fields:
Validators:
field aliased_field: int = 5 (name 'field_with_alias')

Field with alias.

Validated by:
field field_without_alias: int = 5

Field without alias.

Validated by:
validator check  Β»  field_without_alias, aliased_field[source]

Check.

Validates:

Generic Models

Generic pydantic models can be documented just as normal models, too. The following example is borrowed from the official pydantic documentation for generic models :

pydantic model DataModel[source]

Payload representation.

Show JSON schema
{
   "title": "DataModel",
   "description": "Payload representation.",
   "type": "object",
   "properties": {
      "numbers": {
         "items": {
            "type": "integer"
         },
         "title": "Numbers",
         "type": "array"
      },
      "people": {
         "items": {
            "type": "string"
         },
         "title": "People",
         "type": "array"
      }
   },
   "required": [
      "numbers",
      "people"
   ]
}

Fields:
field numbers: List[int] [Required]
field people: List[str] [Required]
pydantic model Error[source]

HTTP error representation.

Show JSON schema
{
   "title": "Error",
   "description": "HTTP error representation.",
   "type": "object",
   "properties": {
      "code": {
         "title": "Code",
         "type": "integer"
      },
      "message": {
         "title": "Message",
         "type": "string"
      }
   },
   "required": [
      "code",
      "message"
   ]
}

Fields:
field code: int [Required]
field message: str [Required]
pydantic model Response[source]

HTTP Response representation.

Show JSON schema
{
   "title": "Response",
   "description": "HTTP Response representation.",
   "type": "object",
   "properties": {
      "data": {
         "anyOf": [
            {},
            {
               "type": "null"
            }
         ],
         "title": "Data"
      },
      "error": {
         "anyOf": [
            {
               "$ref": "#/$defs/Error"
            },
            {
               "type": "null"
            }
         ]
      }
   },
   "$defs": {
      "Error": {
         "description": "HTTP error representation.",
         "properties": {
            "code": {
               "title": "Code",
               "type": "integer"
            },
            "message": {
               "title": "Message",
               "type": "string"
            }
         },
         "required": [
            "code",
            "message"
         ],
         "title": "Error",
         "type": "object"
      }
   },
   "required": [
      "data",
      "error"
   ]
}

Fields:
Validators:
field data: DataT | None [Required]
field error: Error | None [Required]
Validated by:
validator check_consistency  Β»  error[source]

Reused Validators

Functions can be declared as reusable validators for pydantic models. Unlike normal validators which are bound methods, a reusable validator is an actual function. Therefore, the function should be referenced and linked with corresponding pydantic fields in the generated documentation.

While declaring a reusable validator, a class method is automatically created for the pydantic model that conveys no meaningful information. Hence it can be hidden in the documentation via model-hide-resued-validator.

The following example is borrowed from the official pydantic documentation for reused validators which shows how the reused function is correctly linked within the model’s validator summary and the fields validator’s list:

normalize(name: str) str[source]

Normalize docstring.

pydantic model Consumer[source]

Show JSON schema
{
   "title": "Consumer",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      }
   },
   "required": [
      "name"
   ]
}

Fields:
Validators:
field name: str [Required]
Validated by:
pydantic model Producer[source]

Show JSON schema
{
   "title": "Producer",
   "type": "object",
   "properties": {
      "name": {
         "title": "Name",
         "type": "string"
      }
   },
   "required": [
      "name"
   ]
}

Fields:
Validators:
field name: str [Required]
Validated by: