[QUESTION] Response model with pydantic's arbitrary_types_allowed not working
See original GitHub issueI 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()}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:14 (4 by maintainers)
Top Results From Across the Web
Invalid args for response field! Hint: check that <class 'typing ...
I have no idea what's going on! class ParameterSchema(BaseModel): expiryDate = Optional[datetime] class Config: arbitrary_types_allowed = ...
Read more >fastapi.exceptions.fastapierror: invalid args for response field ...
The error says your type you pass to predict must be pydantic BaseModel (or ... Response model with pydantic's arbitrary_types_allowed not working#1186.
Read more >tiangolo/fastapi - Gitter
In this case, what I'd really like to do is return a pydantic model with a derived field for the count of the...
Read more >Getting Started with MongoDB and FastAPI
So, it's not unusual to create models when working with a MongoDB database. Our application has two models, the StudentModel and the ...
Read more >Extra Models - FastAPI
Pydantic models have a .dict() method that returns a dict with the model's data. ... issues (when you update in one place but...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Same problem here:
will report error:
I ended up using a custom Pydantic field validator, instead of using
arbitrary_types_allowed
: