Pydantic integration
Strawberry's Pydantic integration lets a Pydantic model serve as the source for a GraphQL type. It connects model fields and validation with GraphQL schema rules so applications can use one model across their API boundary and Python domain code.
Integration model
The integration adds GraphQL meaning to Pydantic v2 BaseModel classes without requiring a second wrapper class. The decorators keep the decorated class as the Pydantic model while adding the metadata Strawberry needs to build a schema. strawberry/…/__init__.py• L1-22
Model roles
The integration supports three GraphQL roles. An object type exposes model data in query results, an input type accepts data from clients, and an interface defines shared fields for Pydantic-backed object types. The same model is not decorated more than once, so applications use subclasses or shared undecorated base models when they need separate output and input roles. strawberry/…/object_type.py• L109-230 strawberry/…/exceptions.py• L653-739
The main decorators are:
strawberry.pydantic.typefor GraphQL object types.strawberry.pydantic.inputfor GraphQL input types.strawberry.pydantic.interfacefor GraphQL interfaces.
These roles follow normal GraphQL direction: output types describe values returned by resolvers, while input types describe values accepted by fields. Input types cannot contain resolver fields. strawberry/…/object_type.py• L257-451 strawberry/…/fields.py• L790-828
References: strawberry/…/object_type.py• L109-230 strawberry/…/object_type.py• L257-451 strawberry/…/exceptions.py• L653-739
Data flow
The integration turns GraphQL input into validated Pydantic models and reads Pydantic model values when it resolves output fields. Strawberry first applies GraphQL name and scalar conversion, then Pydantic validates the resulting Python data. This keeps GraphQL coercion and model validation as related but distinct steps.
The following diagram shows the main request and response paths.
flowchart LR
Client[GraphQL client] --> Input[GraphQL input]
Input --> Map[GraphQL-to-model mapping]
Map --> Validate[Pydantic validation]
Validate --> Model[Pydantic model]
Model --> Resolver[Resolver]
Resolver --> Output[GraphQL result]
Nested Pydantic inputs are validated with the outer model. The conversion keeps GraphQL names and list positions in the error path, so callers can connect a model error to the submitted value. It also preserves the difference between an omitted input field and an explicit null through strawberry.Maybe. strawberry/…/conversion.py• L111-202 strawberry/…/conversion.py• L205-300 strawberry/…/maybe.py• L35-58
References: strawberry/…/conversion.py• L111-202 strawberry/…/conversion.py• L205-300 strawberry/…/maybe.py• L35-58
Field boundary
Pydantic supplies the model field set. Strawberry then maps those fields to GraphQL names and adds GraphQL-only behavior such as directives, permissions, descriptions, deprecation, custom GraphQL types, and resolver fields. Python field names are the normal GraphQL source; Pydantic serialization aliases do not silently rename the schema.
Output types can include computed fields and can hide model data with strawberry.Private or output serialization exclusion. GraphQL reads model attributes rather than Pydantic's serialized output, so serializers that reshape or hide a GraphQL field are rejected instead of being silently ignored. A resolver field adds a GraphQL field backed by a method rather than a stored model value. Input types focus on client data and therefore do not expose resolver fields. strawberry/…/fields.py• L718-773 strawberry/…/fields.py• L790-938 strawberry/…/fields.py• L941-1059
References: strawberry/…/fields.py• L718-773 strawberry/…/fields.py• L790-938 strawberry/…/fields.py• L941-1059
Optional input state
strawberry.Maybe gives an input field three useful states: the client omitted it, sent null, or sent a value. Pydantic validates a sent value and wraps it in Some; an omitted field remains unset, which lets a resolver distinguish a partial update from an explicit null. strawberry/…/maybe.py• L18-58 strawberry/…/fields.py• L330-398
This state is an input concern. Maybe is not valid on Pydantic output types, and its constraints or validators must apply to the value inside Maybe. The integration reports unsupported defaults and validator placement during schema setup. strawberry/…/fields.py• L330-398 strawberry/…/exceptions.py• L375-449
References: strawberry/…/maybe.py• L18-58 strawberry/…/fields.py• L330-398 strawberry/…/exceptions.py• L375-449
Validation and error boundary
Pydantic validation is part of GraphQL input handling when a model uses strawberry.pydantic.input. A failed model, field, or functional validator becomes an input coercion error instead of an unexpected server failure. strawberry/…/error.py• L41-76
GraphQL error behavior
By default, invalid input returns a GraphQL error with a validationErrors extension. Each detail contains a location, message, and Pydantic error type. Applications can instead declare strawberry.pydantic.ValidationError in a field's return type and register PydanticValidationErrorHandler to return those details as typed GraphQL data. strawberry/…/error.py• L17-35 strawberry/…/error.py• L79-95
Validation that an application performs later inside a resolver is a different path. A Pydantic ValidationError raised there is a normal resolver error; PydanticErrorExtension can add its details to the response, but it does not turn the error into typed input data. strawberry/…/pydantic_error_extension.py• L7-41
References: strawberry/…/error.py• L17-35 strawberry/…/error.py• L41-95 strawberry/…/pydantic_error_extension.py• L7-41
Setup errors
Schema setup rejects model shapes that GraphQL cannot represent safely. The errors guide callers toward a decorated model type, a supported field declaration, or a direct GraphQL scalar instead of allowing a missing nested type, unsupported root model, ignored serializer, or field conflict to fail later. strawberry/…/exceptions.py• L80-153 strawberry/…/exceptions.py• L160-233 strawberry/…/exceptions.py• L318-365 strawberry/…/exceptions.py• L502-603
References: strawberry/…/exceptions.py• L80-153 strawberry/…/exceptions.py• L160-233 strawberry/…/exceptions.py• L318-365 strawberry/…/exceptions.py• L502-603
Conversion boundary
The first-class integration uses the Pydantic model itself at the resolver boundary. It does not use the older pattern of creating a separate Strawberry wrapper and calling from_pydantic or to_pydantic. The separate strawberry.experimental.pydantic integration remains the path for that wrapper-and-conversion model and for Pydantic v1 support. tests/…/test_pydantic.py• L83-105 docs/…/pydantic.md• L1391-1421
For the current conversion and validation rules, see Pydantic types and conversion.
References: strawberry/…/object_type.py• L211-227 strawberry/…/conversion.py• L250-300 docs/…/pydantic.md• L1391-1421
Dependency boundary
Pydantic is an optional dependency. Importing Strawberry does not import Pydantic, so applications that do not use this integration can run without the extra package. Accessing strawberry.pydantic requires the optional dependency and a supported Pydantic version. tests/test_optional_pydantic_dependency.py• L36-77 strawberry/…/_requirements.py• L1-36
The current integration requires Pydantic 2.11 or newer. Install the extra with pip install 'strawberry-graphql[pydantic]'; use the experimental integration for Pydantic v1 models. strawberry/…/_requirements.py• L9-33 docs/…/pydantic.md• L11-20
References: strawberry/…/_requirements.py• L1-36 tests/test_optional_pydantic_dependency.py• L36-77 docs/…/pydantic.md• L11-20
Related guidance
Read Pydantic types and conversion for the detailed rules for creating Pydantic-backed types, mapping fields, converting values, and handling validation or serialization errors.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the decorator roles, resolver boundary, and input error contract aligned across the type decorators and conversion code.
- Check GraphQL field-name mapping when Pydantic alias behavior or the schema name converter changes.
- Check both default
validationErrorsresponses and typedValidationErrorresponses when input validation behavior changes. - Check omitted, null, and present values when
strawberry.Maybeconversion or Pydantic field validation changes. - Check serializer rejection and output field visibility when model serialization or exclusion rules change.
- Keep optional-import behavior intact so core Strawberry imports do not load Pydantic.
- Check the separate experimental integration when changing shared Pydantic terminology or documenting version support.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An undecorated Pydantic model is used as a GraphQL field type. | Schema construction cannot determine the model's GraphQL type. | Decorate it for its output or input role, or register it as a deliberate scalar. |
A Pydantic version older than 2.11 is used with strawberry.pydantic. | The integration fails at import time because its input validation contract is unavailable. | Install the Pydantic extra with a supported version. |
A resolver raises a Pydantic ValidationError after GraphQL input handling. | The error is reported as a resolver error rather than as typed input validation data. | Validate through a Pydantic input when possible, or use PydanticErrorExtension for response details. |
Pydantic is absent but application code accesses strawberry.pydantic. | The integration import fails while unrelated Strawberry features remain usable. | Install strawberry-graphql[pydantic] before using the integration. |
A Maybe field is used on an output type or its validation metadata is placed outside Maybe. | Schema setup fails or Pydantic validates the wrapper instead of the client value. | Use Maybe only for inputs and put value constraints inside its type. |
| A model serializer changes or hides a field that GraphQL exposes. | The GraphQL result can differ from the model's Pydantic serialization. | Use a resolver or custom scalar for a changed value, or hide the field with Strawberry or Pydantic field exclusion. |
A nested model or RootModel has no supported GraphQL definition. | Schema construction fails because the field has no object type or because a single-value root model is not a GraphQL object. | Decorate nested object models and expose the root value's GraphQL type directly. |
Key files
| File | Why to read it |
|---|---|
| strawberry/…/object_type.py | Defines the three decorator roles and attaches GraphQL behavior to Pydantic models. |
| strawberry/…/fields.py | Shows how model fields, computed fields, hidden fields, and GraphQL field metadata meet. |
| strawberry/…/conversion.py | Explains the boundary from GraphQL input values to Pydantic validation and model instances. |
| strawberry/…/maybe.py | Defines how omitted, null, and present Maybe input values are validated and serialized. |
| strawberry/…/exceptions.py | Describes setup errors for unsupported models, fields, serializers, inheritance, and Maybe usage. |
| strawberry/…/error.py | Defines input validation errors and the typed validation-error handler. |
| strawberry/…/_requirements.py | Defines optional dependency and supported-version behavior. |
| strawberry/…/pydantic_error_extension.py | Shows how resolver-raised Pydantic errors can be added to GraphQL error extensions. |
| docs/…/pydantic.md | Provides user-facing examples and the detailed feature and migration rules. |