Skip to content

Response model with pydantic's arbitrary_types_allowed not working #1186

Description

@datasatanic

I use pydantic model with custom type. Set in config arbitrary_types_allowed=True.
Without setting a parameter "response_model" it's work. But when I set this model in param:

fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'Foo'> is a valid pydantic field type 

Pydantic class definition example:

import Foo

class Model(BaseModel):
    id: str
    custom: Optional[Foo] = Field(None)
    status: str = None
    fail_message: Optional[str] = None
    params: Dict[str, Any] = None

    @validator('custom')
    def deserialize_model(cls, v):
        return Foo.loads(v)

    class Config:
        arbitrary_types_allowed = True
        json_encoders = {Foo: lambda v: v.dumps()}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions