Introducing a new way to review your PRs.

Read the announcement

strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓

View on GitHub

21 documents · Updated Nov 18, 2026

docs / pydantic-types-and-conversion

Pydantic types and conversion

The Pydantic integration lets one BaseModel define both the data contract and its GraphQL view. Decorators expose models as GraphQL object types, input types, or interfaces. The integration maps fields at schema build time and validates GraphQL input at request time.

See the Pydantic integration page for setup and the wider set of supported features.

Model roles

GraphQL object types

@strawberry.pydantic.type exposes a Pydantic model as a GraphQL object type. The decorated class stays the source of the values returned by resolvers, so GraphQL reads its attributes instead of creating a second output model. An undecorated Pydantic model used as a field has no GraphQL definition and fails schema construction.

Object types can also implement Pydantic or regular Strawberry interfaces. A model must be the first base when it is combined with a regular Strawberry interface so that model construction remains the owning behavior.

References: strawberry/…/object_type.py• L109-231 strawberry/…/fields.py• L350-419

GraphQL input types

@strawberry.pydantic.input exposes a model as a GraphQL input object. Strawberry first turns the client input into Python data, then Pydantic builds and validates the model. The resolver therefore receives a validated Pydantic instance rather than an unvalidated field container.

Input types do not expose computed fields or resolver fields. They represent values supplied by the client. A model can be used for both output and input roles through separate model subclasses because one model cannot have two different GraphQL definitions.

References: strawberry/…/object_type.py• L312-381 strawberry/…/fields.py• L941-1059

GraphQL interfaces

@strawberry.pydantic.interface turns shared model fields into a GraphQL interface. Pydantic model subclasses can implement that interface and add their own fields. The interface links the common GraphQL contract to the model hierarchy while concrete types provide the values.

References: strawberry/…/object_type.py• L384-448 strawberry/…/fields.py• L185-198

Field mapping

Model fields

The integration treats Pydantic's declared fields as the main field set. It carries field types, descriptions, defaults, deprecation data, and nested model references into Strawberry's GraphQL definition. Strawberry metadata can refine that view with Annotated[..., strawberry.field(...)], including a GraphQL name, description, directive, permission, or explicit GraphQL type.

Pydantic validation constraints remain part of model validation. They do not by themselves create a different GraphQL constraint. A Pydantic type that narrows a normal scalar is exposed as the matching GraphQL scalar unless the field supplies another GraphQL type.

References: strawberry/…/fields.py• L484-570 strawberry/…/fields.py• L941-1059

GraphQL names and Pydantic aliases

GraphQL names normally come from Python field names. A Pydantic alias controls Pydantic's own data boundary, not the GraphQL schema. Use strawberry.field(name=...) when the GraphQL name must differ from the Python name.

This separation also applies to input conversion. Strawberry maps the client-facing GraphQL name back to the Python field name, then asks Pydantic to validate by field name. A mode="before" validator therefore sees Python field names rather than Pydantic aliases.

References: strawberry/…/fields.py• L586-590 strawberry/…/conversion.py• L123-153 strawberry/…/conversion.py• L266-279

Hidden and computed fields

strawberry.Private keeps a model value available to Python code but removes it from the GraphQL schema. On output types, Field(exclude=True) also removes a field because it is absent from the model's serialized view. That setting does not prevent an input client from sending the field.

Pydantic computed fields are output fields by default. They can be omitted for a type with include_computed=False, and input types omit them because clients cannot provide their values. Their descriptions and Strawberry field metadata follow the same mapping rules as regular fields.

References: strawberry/…/fields.py• L526-553 strawberry/…/fields.py• L941-1059 strawberry/…/object_type.py• L109-184

Optional input fields

strawberry.Maybe[T] is an input-only field type that keeps client omission separate from an explicit value. An omitted field is None and is not part of Pydantic's set-field data. A supplied value is wrapped as Some(value), including Some(None) when the inner type permits null.

Pydantic validates the value inside Some. Put constraints and value validators inside the Maybe type so they apply to T, not to the wrapper. A Maybe field does not need a default, and it cannot be used on output types.

References: strawberry/…/maybe.py• L18-58 strawberry/…/fields.py• L330-398 strawberry/…/exceptions.py• L375-435

Resolver fields

Resolver field contract

strawberry.pydantic.field adds a GraphQL field whose value comes from a method or callable rather than a stored Pydantic field. It provides the normal Strawberry field options, so a resolver can add derived data, access request context, or apply permissions while still living on the model.

Resolver fields are output-only. Defining one directly on an input model is an error, and inherited resolver fields are ignored when they come from a shared base. To replace an inherited resolver or computed field, the replacement must use the matching supported field form.

References: strawberry/…/fields.py• L790-938 strawberry/…/exceptions.py• L451-557

Model conversion

GraphQL input conversion

Input conversion is the boundary between GraphQL's transport values and Pydantic's model validation. Strawberry applies GraphQL scalar conversion and converts nested Pydantic input objects into data. It then calls Pydantic validation with Python field names, which applies field validators, model validators, defaults, and nested validation.

The conversion also adapts GraphQL lists to tuple-shaped Pydantic fields. This keeps strict tuple validation consistent with the list shape that GraphQL exposes.

The flow below shows the ownership boundary between Strawberry and Pydantic.

flowchart LR
    A[GraphQL input] --> B[GraphQL name and scalar conversion]
    B --> C[Nested Python data]
    C --> D[Pydantic model validation]
    D --> E[Validated model in resolver]
    D --> F[Input validation error]

References: strawberry/…/conversion.py• L113-202 strawberry/…/conversion.py• L266-300

GraphQL output conversion

Output resolution does not call model_dump() to build the GraphQL response. GraphQL reads the model attributes that correspond to the exposed fields. This keeps resolver behavior direct, but it means Pydantic serializers cannot silently change or redact an output value.

Nested output models use their own registered GraphQL definitions. A model inside a list, union, or another model must have the GraphQL role required by that position. The schema reports an unregistered Pydantic type before execution when that contract is missing.

References: strawberry/…/fields.py• L401-490 strawberry/…/exceptions.py• L80-153

Input default conversion

When an input model is used as a GraphQL default, conversion includes only fields that were set on the Pydantic instance. GraphQL then applies its own default coercion before rebuilding the model, so the converted default does not promise to preserve the original model_fields_set exactly.

Maybe uses the same boundary to preserve omission. Its Pydantic default is None, while a supplied value remains part of the set-field data and is serialized as its inner value.

References: strawberry/…/conversion.py• L250-263 strawberry/…/maybe.py• L35-45

Experimental wrapper conversion

The separate strawberry.experimental.pydantic integration creates a Strawberry wrapper class instead of decorating the Pydantic model itself. Its from_pydantic operation copies model data into the wrapper without running a new Pydantic validation pass. Its to_pydantic operation turns wrapper data back into the model and triggers Pydantic validation.

This conversion model is distinct from the direct strawberry.pydantic integration. Do not mix the two integrations for related fields unless each model has the GraphQL definition required by the integration that uses it.

References: strawberry/…/object_type.py• L118-291 strawberry/…/conversion.py• L20-136 strawberry/…/fields.py• L22-68

Validation and serialization errors

Input validation errors

Invalid Pydantic input becomes InputValidationError, a GraphQL input coercion error. The response carries a validationErrors extension with the client-visible location, the Pydantic error message, and the Pydantic error type. Locations use GraphQL field names and represent list indexes as strings.

The conversion removes raw input values from the Pydantic error details and does not chain the original validation error. The default behavior returns the failure as a GraphQL error. A field that can return strawberry.pydantic.ValidationError can use PydanticValidationErrorHandler to return the same details as typed data instead. A Pydantic validation error raised later by application resolver code is not treated as invalid GraphQL input.

References: strawberry/…/conversion.py• L205-247 strawberry/…/conversion.py• L280-300 strawberry/…/error.py• L17-95

Resolver validation errors

Validation performed explicitly inside a resolver, such as through a TypeAdapter or RootModel, is a normal application error. PydanticErrorExtension can add a validation_errors extension for such errors, with a simpler field-and-message shape. It does not change input coercion errors produced by the Pydantic input boundary.

References: strawberry/…/pydantic_error_extension.py• L7-41 strawberry/…/error.py• L41-95

Ignored serializer errors

Output types reject Pydantic serializers that would change, hide, or reshape exposed values. GraphQL resolves fields from attributes, so applying field_serializer, PlainSerializer, WrapSerializer, exclude_if, or a model serializer would otherwise give a schema that does not honor the model's serialization rules.

Use a resolver field to produce a GraphQL-specific value. Use strawberry.Private or Field(exclude=True) to keep a value out of the output schema.

References: strawberry/…/fields.py• L718-773 strawberry/…/exceptions.py• L560-604

Model definition errors

The decorators reject models that cannot provide a GraphQL object shape. This includes RootModel, which holds one value instead of named fields, Pydantic v1 models, models that inherit an undecorated GraphQL definition, and models with unsupported field or base ordering.

These errors occur during schema construction. Their messages point to the model or field and identify the required decorator, field form, or model shape.

References: strawberry/…/object_type.py• L136-159 strawberry/…/exceptions.py• L277-365 strawberry/…/exceptions.py• L451-557

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to GraphQL name conversion must stay aligned with input error locations and Pydantic's field-name validation.
  • Changes to exposed output fields must stay aligned with serializer checks and the rules for Private and Field(exclude=True).
  • Changes to Maybe handling must keep omission, explicit null, Pydantic validation, and set-field serialization consistent.
  • Changes to nested model handling must preserve the distinction between an input model that needs validation and an output model that needs a registered GraphQL definition.
  • Changes to validation error handling must preserve the separate contracts for validationErrors on input coercion and validation_errors on resolver-raised Pydantic errors.
  • Changes to either Pydantic integration must keep direct model types separate from experimental wrapper types.

Important failure modes

TriggerConsequenceGuard
A Pydantic model is used as a GraphQL field without the needed decorator or scalar registration.Schema construction fails with an unregistered Pydantic type error.Decorate every model at the GraphQL boundary or register an intentional custom scalar.
A Pydantic alias is expected to become a GraphQL name.The schema uses the Python field name and client input does not match the assumed alias.Set the GraphQL name with strawberry.field(name=...).
An output model uses a Pydantic serializer on an exposed field.Schema construction fails because GraphQL would ignore that serializer.Move the output rule to a resolver field or hide the source field.
A resolver field is declared on an input model.Model decoration fails because input values cannot be resolved from a method.Keep resolver fields on output types.
A Maybe field has an unsupported default, outer constraint, or after/plain field validator.Decoration fails or Pydantic validates the Some wrapper instead of the client value.Remove the default and put constraints or validators inside Maybe[T].
A RootModel is decorated or used as a GraphQL field.Schema construction fails because the model has no named GraphQL fields.Expose the type of its value, such as list[str], instead.
Pydantic validation fails while GraphQL builds an input model.The field returns a GraphQL input error unless a typed validation handler handles it.Read validationErrors, or include the typed ValidationError result when that API contract is required.

Key files

FileWhy to read it
strawberry/…/object_type.pyDefines how Pydantic models become GraphQL types, inputs, and interfaces.
strawberry/…/fields.pyDefines field mapping, names, hidden fields, computed fields, resolver fields, and serializer checks.
strawberry/…/resolver_field.pyDefines the output-only resolver field contract.
strawberry/…/conversion.pyDefines the GraphQL-to-Pydantic input boundary, tuple adaptation, and error location mapping.
strawberry/…/maybe.pyDefines how omitted and supplied Maybe values are validated and serialized.
strawberry/…/exceptions.pyDefines schema-build errors for unsupported models, fields, inheritance, Maybe usage, and serializers.
strawberry/…/error.pyDefines input validation error data and typed error handling.
strawberry/…/pydantic_error_extension.pyDefines formatting for Pydantic errors raised during resolver execution.
strawberry/…/object_type.pyShows the separate wrapper-based integration and its conversion methods.
strawberry/…/conversion.pyShows recursive conversion between experimental wrappers and Pydantic models.