Unregistered Pydantic Type Error
Description
This error is thrown during schema construction when a field uses a Pydantic
model that is neither a Strawberry type nor registered in the schemaโs
scalar_map . It applies to regular Strawberry fields as well as fields of
strawberry.pydantic types (with Pydantic 2.11 or newer):
import pydanticimport strawberry
class Address(pydantic.BaseModel): street: str
@strawberry.typeclass Query: @strawberry.field def address(self) -> Address: return Address(street="Main Street")
schema = strawberry.Schema(query=Query) This happens because Strawberry needs to know the GraphQL type of every field,
and Address hasnโt been converted to a GraphQL type.
How to fix this error
You can fix this error by decorating the model used by the field, with
@strawberry.pydantic.type for output types and @strawberry.pydantic.input
for inputs:
import pydanticimport strawberry
@strawberry.pydantic.typeclass Address(pydantic.BaseModel): street: str
@strawberry.pydantic.typeclass User(pydantic.BaseModel): name: str address: Address Subclasses of decorated models need their own decorator too, as they can have fields of their own. Otherwise the field would silently use the base modelโs GraphQL type, without the subclassโs fields:
@strawberry.pydantic.typeclass Animal(pydantic.BaseModel): name: str
@strawberry.pydantic.typeclass Dog(Animal): breed: str
@strawberry.pydantic.typeclass Owner(pydantic.BaseModel): dog: Dog A parametrized generic model, like Page[User] , is a GraphQL type when its
generic model is decorated, so decorate the generic model:
from typing import Generic, TypeVar
T = TypeVar("T")
@strawberry.pydantic.typeclass Page(pydantic.BaseModel, Generic[T]): items: list[T]
@strawberry.pydantic.typeclass Directory(pydantic.BaseModel): users: Page[User] If the model has a type from the experimental integration,
strawberry.experimental.pydantic , the error says it only has a
strawberry.experimental.pydantic type: the two integrations canโt use each
otherโs types. Decorate the model with @strawberry.pydantic.type too, with a
different name if its experimental type is also in the schema:
import pydanticimport strawberry
class AddressModel(pydantic.BaseModel): street: str
@strawberry.experimental.pydantic.type(model=AddressModel, all_fields=True)class Address: pass
strawberry.pydantic.type(AddressModel, name="NewAddress")
@strawberry.pydantic.typeclass User(pydantic.BaseModel): name: str address: AddressModel