Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Pydantic with typing.Protocol

I'm attempting to use typing protocols and pydantic together and am running into some issues. I expect probably my understanding of how to properly use the two of them together is lacking.

This pydantic code is producing an error:

from typing import TypeVar, Protocol, Generic, runtime_checkable
from pydantic import BaseModel

D = TypeVar('D')


@runtime_checkable
class SupportsDimensionality(Protocol[D]):
    dimensionality: D


class ValueModel(BaseModel, Generic[D]):
    value: SupportsDimensionality[D]

Error text:

pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for main.SupportsDimensionality[~D]. Set arbitrary_types_allowed=True in the model_config to ignore this error or implement __get_pydantic_core_schema__ on your type to fully support it.

I've been trying to figure out why this happens and how to fix it but need some help.

like image 837
Rick supports Monica Avatar asked May 22 '26 06:05

Rick supports Monica


1 Answers

I figured it out. The docs say here you can use the arbitrary_types_allowed=True configuration option in a case like this. The problem seems to be that pydantic doesn't support validation of an arbitrary Protocol.

What this does is circumvent the detailed/thorough model checking for fields in cases they are just arbitrary types that are not directly supported by pydantic. Instead pydantic just does a simple instance check. Since it is doing an instance check of a Protocol at runtime, you do have to use the runtime_checkable decorator.

Here is the fix I went with:

from typing import TypeVar, Protocol, Generic, runtime_checkable
from pydantic import BaseModel, ConfigDict

D = TypeVar('D')


@runtime_checkable
class SupportsDimensionality(Protocol[D]):
    dimensionality: D


class Model(BaseModel):
    """A base model that allows protocols to be used for fields."""

    model_config = ConfigDict(arbitrary_types_allowed=True)


class ValueModel(Model, Generic[D]):
    value: SupportsDimensionality[D]

Also see this issue at GH with a short discussion of the problem.

like image 122
Rick supports Monica Avatar answered May 24 '26 19:05

Rick supports Monica