Type annotations and resolution
Strawberry turns Python annotations into the type information used by fields, arguments, and resolver results. It evaluates delayed names in the correct module scope, preserves Strawberry metadata, and builds Strawberry types that the schema layer can later expose as GraphQL types. See Schema and type system, Defining GraphQL types, and Schema conversion and printing for the surrounding type lifecycle.
Resolution model
Annotation sources
Annotations come from fields, Annotated declarations, and resolver signatures. Strawberry keeps the raw annotation until it has the context needed to evaluate it. That context includes the module where the field or resolver was defined.
The same resolver signature can define both sides of a field contract. Its return annotation describes the field result. Its client-facing parameter annotations describe GraphQL arguments after Strawberry removes parameters used for resolver context.
References: strawberry/…/type_resolver.py• L64-101 strawberry/…/type_resolver.py• L192-231 strawberry/…/resolver.py• L282-305
Resolution stages
Resolution changes a Python annotation into a Strawberry type. It first evaluates names, unwraps supported async result containers, and collects Annotated metadata. It then recognizes wrappers such as lists, optional values, unions, lazy references, and generic types. Streamable results become list types. Field annotations are also checked for valid Strawberry field metadata before they are resolved. The result remains a Strawberry-level type until schema conversion.
Strawberry caches evaluation and resolution separately. A field can replace its namespace when its owning class is known. That change clears both results so a forward name can be evaluated again in the correct scope.
This diagram shows how an annotation moves from Python source to GraphQL schema conversion.
flowchart LR
A[Python annotation] --> B[Namespace evaluation]
B --> C[Async and metadata normalization]
C --> D[Wrapper recognition]
D --> E[Strawberry type]
E --> F[Generic specialization]
F --> G[GraphQL schema conversion]
References: strawberry/annotation.py• L101-145 strawberry/annotation.py• L147-205 strawberry/annotation.py• L212-257 strawberry/annotation.py• L259-264 strawberry/…/field.py• L342-377 strawberry/…/field.py• L379-388
Python annotation forms
Forward references
A forward reference is a type name written as a string or produced by from __future__ import annotations. Strawberry evaluates it with the defining module’s namespace. Nested forms such as list["User"], Optional["User"], and "list[User]" use the same rule.
If a name is not available when evaluation starts, Strawberry keeps the raw annotation until the relevant type scope is available. If the name is still missing when the schema needs the field type, schema construction reports the unresolved annotation instead of guessing a type.
References: strawberry/annotation.py• L101-132 strawberry/…/typing.py• L264-306 strawberry/…/field.py• L350-365 tests/…/test_deferred_annotations.py• L6-36 tests/…/test_string_annotations.py• L12-39
Lists, unions, and optional values
Strawberry maps Python container and choice syntax to Strawberry wrappers. A list-like annotation becomes a list of its resolved item type. A union becomes a choice of member types. A union that includes None or UNSET becomes an optional value around the remaining type.
Resolution is recursive. For example, list[User | None] resolves both the list and its item choice. Annotated metadata remains attached when Strawberry rebuilds an optional child union.
| Python form | Strawberry meaning |
|---|---|
list[T], tuple[T], or sequence forms | List of resolved T |
| `T | UorUnion[T, U]` |
| `T | NoneorOptional[T]` |
Maybe[T] | Input value that tracks absence separately from explicit null |
For an output union, the runtime value must identify one allowed member. strawberry.cast can select a matching member when normal Python instance checks are not enough. A cast that matches no member is ignored. A cast that matches more than one member is an error.
References: strawberry/annotation.py• L134-145 strawberry/annotation.py• L212-257 strawberry/annotation.py• L287-339 strawberry/annotation.py• L344-382 strawberry/…/base.py• L93-174 strawberry/…/union.py• L170-235
Lazy references
A lazy reference names a type and its owning module without importing the type at declaration time. This supports circular links and types available only under TYPE_CHECKING. The reference resolves when a later schema or value operation needs the concrete type.
Lazy references can appear inside lists, optional values, and other nested annotations. Their purpose is to delay the dependency without changing the type relationship exposed by the schema.
References: strawberry/…/lazy_type.py• L20-65 strawberry/…/lazy_type.py• L72-136 strawberry/…/typing.py• L229-327 tests/…/test_lazy_types_future_annotations.py• L77-242
Annotated metadata
Annotated combines a base Python type with Strawberry metadata. The base type defines the shape. Metadata can control field or argument settings, mark a lazy link, mark a private value, request automatic inference, or define a union.
Strawberry reads metadata at the boundary where it applies. Field metadata is used while fields are found. Argument metadata is used while resolver arguments are built. The base type still resolves recursively through lists, unions, and optional values.
A strawberry.field() marker is valid only in the outermost Annotated metadata for a field. Strawberry preserves the other outer metadata, but rejects a field marker nested inside a list, union, or another annotation. It also rejects more than one field marker at the outer level so that one Python field has one field contract.
References: strawberry/…/type_resolver.py• L21-61 strawberry/…/arguments.py• L82-121 strawberry/…/typing.py• L308-357 strawberry/annotation.py• L134-145 strawberry/annotation.py• L287-328 strawberry/…/field.py• L430-481
Generic types
Type variables
A type variable is a placeholder that a generic Strawberry type will fill later. Strawberry keeps it as a generic type marker so the placeholder remains visible while the generic definition is built.
The marker can appear by itself or inside a list, optional value, union, object, field, or resolver result. Such a type is not a final GraphQL type until its type arguments are known. Container wrappers preserve this marker so a later specialization can replace it at any nesting level.
References: strawberry/annotation.py• L229-239 strawberry/annotation.py• L249-250 strawberry/…/base.py• L122-159 strawberry/…/base.py• L177-206 tests/…/test_generics.py• L21-92
Concrete specializations
When code uses a generic type with concrete arguments, Strawberry resolves each supplied argument and maps it to the matching type variable. It applies that map to fields and resolver arguments and results. Nested containers and object definitions receive the same map, so a field such as list[T] becomes a list of the concrete type.
The mapping may come from the generic class that declares the field or from the concrete type that asks for it. This lets one generic definition produce different field and result types for different uses. A bare generic has no map and cannot become a final GraphQL type.
Strawberry does not treat an undecorated subclass as a new schema type merely because it inherits its parent’s definition. It rejects that class when it is used as a schema type, while generic aliases and runtime resolver values keep their distinct roles.
References: strawberry/annotation.py• L147-206 strawberry/annotation.py• L229-239 strawberry/…/base.py• L219-234 strawberry/…/base.py• L272-294 strawberry/…/base.py• L371-420 strawberry/…/base.py• L447-472 strawberry/…/resolver.py• L399-452 strawberry/…/schema_converter.py• L1215-1254
Automatic type inference
Auto markers
strawberry.auto asks an integration to derive a field type from an external model. It is not a general runtime guess from a Python value. The annotation layer preserves the marker so the integration can apply its own model rules.
For Pydantic types, an explicit Strawberry annotation takes priority. An auto field receives the model type only when the model contains that field. When an integration adds all model fields, explicit fields keep their declared types and only the remaining fields use automatic inference.
References: strawberry/…/auto.py• L69-94 strawberry/…/typing.py• L308-335 strawberry/…/object_type.py• L135-185
Resolver signatures
Return annotations
A resolver return annotation describes the value exposed by its field. Strawberry uses an explicit field annotation first. This includes a type supplied through the field's explicit GraphQL type option when the resolver's Python type differs from the schema type. If no explicit field type exists, Strawberry uses the resolver return annotation. A resolver field without either source raises a focused error rather than inferring a schema type.
Execution mode is separate from the declared result type. Synchronous and asynchronous resolvers can expose the same GraphQL type. Supported async iterable forms resolve to the contained result type, and streamable results expose a list of that type.
When a field is inherited, Strawberry binds its resolver to the class that owns the field. This keeps resolver execution context separate from the annotation used for the result type.
References: strawberry/…/field.py• L342-377 strawberry/…/field.py• L580-659 strawberry/…/resolver.py• L230-274 strawberry/…/resolver.py• L390-452 strawberry/…/object_type.py• L429-452 strawberry/…/missing_return_annotation.py• L15-37 tests/…/test_resolver_types.py• L41-107 tests/…/test_fields_resolver_async.py• L8-27
GraphQL arguments
Resolver parameters that belong to the client call become GraphQL arguments. Each needs an annotation because a default value or function body does not safely define an input type. The parameter default remains argument metadata while its annotation follows the same resolution rules as a field type.
Strawberry reports missing annotations together so one error identifies all client-facing parameters that need attention. A Strawberry argument marker can also provide the GraphQL name and other argument metadata.
Resolver arguments must resolve to GraphQL input types. Field registration rejects private types, output-only unions, and interfaces used as client inputs. A still-unresolved string annotation is deferred so that a forward reference can be resolved later.
References: strawberry/…/resolver.py• L301-348 strawberry/…/arguments.py• L35-121 strawberry/…/field.py• L204-229 strawberry/…/missing_arguments_annotations.py• L15-42 tests/…/test_resolvers.py• L271-327
Reserved parameters
Some resolver parameters come from Strawberry rather than from the GraphQL client. self and cls identify bound methods. root receives the parent value. An Info annotation receives request and schema context. A Parent annotation receives the parent object for a field resolver.
Strawberry detects these parameters by name or reserved type. Qualified string annotations remain detectable when future annotations or TYPE_CHECKING prevents runtime evaluation. One resolver cannot declare two parameters for the same reserved role. Reserved parameters stay out of the public argument list.
References: strawberry/…/resolver.py• L61-200 strawberry/…/resolver.py• L301-348 tests/…/test_resolvers.py• L106-204 tests/…/test_resolvers.py• L595-663 tests/…/test_resolvers.py• L698-737
Change checklist
When these files or sections are changed, remember to consider these:
- Changes to annotation evaluation or its caches must preserve the defining module scope for inherited fields and nested classes. Namespace changes must clear prior evaluation results.
- Changes to async or streamable annotation handling must keep the contained result type aligned with resolver execution and schema output.
- Changes to
Annotatedhandling must keep lazy, optional, private, auto, field, argument, and union metadata attached to the correct base type. - Changes to generic container copying or generic maps must update nested field types and resolver argument and return types together for every concrete specialization. Undecorated subclasses must not pass as concrete schema types through inherited metadata.
- Changes to reserved-parameter matching must keep qualified forward-reference strings aligned with the public
InfoandParentforms. - Changes to
Maybeinterpretation must preserve the difference between an omitted value and an explicit null through input conversion and validation. - Changes to automatic inference must keep explicit field types ahead of integration-derived model types.
- Changes to resolver binding must keep inherited classmethod context tied to the concrete class while preserving the original descriptor for later subclass binding.
- Changes to union value resolution must keep explicit casts aligned with concrete union members and reject ambiguous generic matches.
- Changes to field annotation extraction must keep
strawberry.field()at the outerAnnotatedboundary and preserve the other metadata on that annotation. - Changes to explicit GraphQL field type overrides must keep their precedence over resolver return annotations without changing resolver execution behavior.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A forward name is absent from the defining module namespace | The field stays unresolved and schema construction reports a missing type | Import the type, define it before schema use, or use a lazy reference |
| A resolver has no return annotation and no explicit field type | Strawberry raises MissingReturnAnnotationError | Add a return annotation or field type |
| A client-facing resolver parameter has no annotation | Strawberry raises MissingArgumentsAnnotationsError | Annotate every GraphQL argument |
| Two parameters match one reserved resolver role | Strawberry raises ConflictingArgumentsError | Keep one root, Info, or Parent parameter per resolver |
| A resolver argument resolves to a private type, output union, or interface | Field registration raises InvalidArgumentTypeError | Use a GraphQL input type for the client-facing argument |
A field annotation nests strawberry.field() or declares it more than once | Field discovery raises an invalid or multiple-field annotation error | Put one field marker in the outermost Annotated metadata |
| A bare generic reaches schema conversion | Strawberry raises MissingTypesForGenericError | Use a concrete generic specialization |
| A parameterized annotation uses an undecorated subclass of a Strawberry type | Strawberry raises UndecoratedSubclassError | Decorate the subclass or use a valid concrete parent specialization |
| An auto field is not present in the source model | The integration rejects the field instead of producing an inferred type | Keep auto fields aligned with the model or give the field an explicit type |
Maybe[T] receives explicit null while T is not nullable | Input validation rejects the value | Use `Maybe[T |
| A cast matches more than one member of a generic union | Union value resolution cannot choose one GraphQL member | Cast to one concrete union member or return a value that identifies one member |
Key files
| File | Why to read it |
|---|---|
| strawberry/annotation.py | Defines the central annotation wrapper and the order used to evaluate and recognize async forms, metadata, lists, optional values, unions, lazy types, enums, Strawberry types, and generic types. |
| strawberry/…/typing.py | Defines evaluation of strings, forward references, nested arguments, and special Annotated metadata. |
| strawberry/…/type_resolver.py | Shows how class fields obtain annotations and the namespace used to resolve them. |
| strawberry/…/resolver.py | Shows how resolver signatures become result types, GraphQL arguments, reserved parameters, async metadata, and class-bound callables. |
| strawberry/…/field.py | Shows how explicit field types and resolver signatures become field types, how Annotated field markers are validated, and which resolver arguments are valid inputs. |
| strawberry/…/object_type.py | Shows how fields bind resolver descriptors while a Strawberry type is created. |
| strawberry/…/union.py | Shows how resolved union members support generic specialization and runtime value selection. |
| strawberry/…/base.py | Defines the Strawberry wrappers, type-variable markers, generic specialization and copying rules, object-definition lookup, and undecorated-subclass checks used after annotation resolution. |
| strawberry/…/lazy_type.py | Defines cross-module lazy references used to break circular type dependencies. |
| strawberry/…/auto.py | Defines the marker used by integrations for model-based type inference. |
| strawberry/…/object_type.py | Shows the integration boundary where explicit annotations and model-derived types are combined. |
| strawberry/…/arguments.py | Shows how resolved argument types are used for input conversion, including Maybe and lazy values. |