Working with Pydantic v1 while having v2 installed #9966
Replies: 6 comments 4 replies
-
Well, I expect v1 models to work out of the box based on the comment on https://fastapi.tiangolo.com/release-notes/?h=v1#01000:
On my use case, I'd like to be able to migrate models one by one while my system is still running. |
Beta Was this translation helpful? Give feedback.
-
I came across this issue in the context of collerek/ormar#1115. The behaviour in 0.100, with the The logic around the various references to (Would anyone need a |
Beta Was this translation helpful? Give feedback.
-
I gave it a try at #10223 to support v1 models in request and response body. |
Beta Was this translation helpful? Give feedback.
-
I would really expect this to work, we are running into it here chroma-core/chroma#893 (comment) |
Beta Was this translation helpful? Give feedback.
-
Hi. For a response model based on File ".../lib/python3.10/site-packages/fastapi/_compat.py", line 125, in validate
self._type_adapter.validate_python(value, from_attributes=True),
File ".../lib/python3.10/site-packages/pydantic/type_adapter.py", line 283, in validate_python
return self.validator.validate_python(__object, strict=strict, from_attributes=from_attributes, context=context)
TypeError: BaseModel.validate() takes 2 positional arguments but 3 were given
|
Beta Was this translation helpful? Give feedback.
-
Let's follow it on #10360. No confirmation needed. |
Beta Was this translation helpful? Give feedback.
-
This is a continuation of a topic introduced in #9709 (comment) as requested by @Kludex. I've chosen the "Show and tell" category as the (lack of) template seems better for this discussion.
In the original thread I've asked if there's a way to work with pydantic v1 while having v2 installed:
Pydantic v2 ships the latest version of v1 for easier migration, so the pattern is to work with v1 via things like
from pydantic.v1 import BaseModel
etc. while having v2 installed.Currently, this approach doesn't seem to work with FastAPI 0.100.0 and the following snippet:
produces the following error:
Switching from
pydantic.v1
to justpydantic
(v2) obviously works.I'm seeing there's
fastapi._compat
module andPYDANTIC_V2
var, but I'm not sure if that's the right direction.For reference, installed versions:
Now... that being said, I'm not even sure if this would be helpful to anyone even if FastAPI did support it. My initial thought was that this would ease the migration, but the reality seems more complicated. E.g. models are used by other models and even Pydantic itself doesn't support mixing v1 and v2 together. So given that the cost of figuring out the plan for the gradual migration seems comparable with doing the whole migration at once (at least for us).
Maybe it would be useful if people commented on their intended use case and see if that's actually a feature that's needed/wanted.
Beta Was this translation helpful? Give feedback.
All reactions