-
First Check
Commit to Help
Example Codefrom typing import Annotated, Literal
from fastapi import FastAPI
from pydantic import BaseModel, Field
class ItemV1(BaseModel):
version: Literal[1] = 1
class ItemV2(BaseModel):
version: Literal[2] = 2
Item = Annotated[
ItemV1 | ItemV2,
Field(discriminator='version'),
]
app = FastAPI()
@app.post('/')
async def create(body: Item) -> None:
passDescription
It raises the following backtrace: The same code used to work well with FastAPI v0.94.1 (and priors). Please let me know if there is any additional inputs I can provide or whether this is now a desired behavior. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.95.0 Python VersionPython 3.11.2 Additional ContextPip freeze: |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 9 replies
-
|
Same issue here, certainly seems like a bug in the recent 0.95.0. |
Beta Was this translation helpful? Give feedback.
-
|
I also experienced this issue, updating to 0.95.0. |
Beta Was this translation helpful? Give feedback.
-
|
I got bitten today as well. |
Beta Was this translation helpful? Give feedback.
-
|
So uh, when does a 'discussion' become an 'issue'? Or are we just yelling into the void |
Beta Was this translation helpful? Give feedback.
-
|
This was indeed caused by the changes in #4871. Apologies for the inconvenience. The quick solution to this problem is to replace Item = Annotated[
ItemV1 | ItemV2,
Field(discriminator='version'),
]with Item = Annotated[
ItemV1 | ItemV2,
Body(discriminator='version'),
]I'm not really sure whether the original code was supposed to work before, or if it just worked incidentally. @tiangolo what do you think? If you think it should work, I can try and figure out how to make it work. |
Beta Was this translation helpful? Give feedback.
-
|
Hi, first of all thanks to FastAPI developers for such a great project! Just wanted to know if there's any progress on this, because it seems that it's a showstopper for fully migrating to pydantic V2. I also agree with a solution that allows the use of For instance, in our projects we are using these Discriminated Unions throughout the application logic, where FastAPI it's only the final API part in order to interact with outside world. Having to mantain 2 identical sets of Discriminated Unions, only by changing Field() into Body() if these are going to be used in FastAPI is not efficient at all. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
This was indeed caused by the changes in #4871. Apologies for the inconvenience.
The quick solution to this problem is to replace
with
I'm not really sure whether the original code was supposed to work before, or if it just worked incidentally.
@tiangolo what do you think? If you think it should work, I can try and figure out how to make it work.