The Fix
pip install fastapi==0.128.4
Based on closed fastapi/fastapi issue #14508 · PR/commit linked
Production note: This usually shows up under retries/timeouts. Treat it as a side-effect risk until you can verify behavior with a canary + real traffic.
@@ -18,7 +18,7 @@
from fastapi.openapi.constants import REF_TEMPLATE
from fastapi.types import IncEx, ModelNameMap, UnionType
-from pydantic import BaseModel, ConfigDict, TypeAdapter, create_model
+from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, create_model
from pydantic import PydanticSchemaGenerationError as PydanticSchemaGenerationError
from typing import Annotated, Union
from pydantic import BaseModel, Tag, Discriminator
from fastapi import FastAPI, Body
class Cat(BaseModel):
pet_type: str = "cat"
meows: int
class Dog(BaseModel):
pet_type: str = "dog"
barks: float
def get_pet_type(v):
if isinstance(v, dict):
return v.get("pet_type", "")
return getattr(v, "pet_type", "")
# Define discriminated union
Pet = Annotated[
Union[
Annotated[Cat, Tag("cat")],
Annotated[Dog, Tag("dog")]
],
Discriminator(get_pet_type)
]
app = FastAPI()
# ✅ THIS WORKS in 0.124.1+
@app.post("/pet/works")
async def create_pet_works(pet: Pet = Body(...)):
return pet
# ❌ THIS BREAKS in 0.124.1+ (worked in 0.124.0)
@app.post("/pet/broken")
async def create_pet_broken(pet: Annotated[Pet, Body(...)]):
return pet
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install fastapi==0.128.4\nWhen NOT to use: Do not use if it changes public behavior or if the failure cannot be reproduced.\n\nOption B — Safe version pin\npip install fastapi==0.124.0\nWhen NOT to use: Do not use if you need features or security fixes in newer releases.\n\nOption C — Workaround\nUse pet: Pet = Body(...) instead of pet: Annotated[Pet,\nWhen NOT to use: Do not use if it changes public behavior or if the failure cannot be reproduced.\n\n
Why This Fix Works in Production
- Trigger: pydantic.errors.PydanticUserError: `Tag` not provided for choice { 'type': 'tagged-union', 'choices': { 'cat': {'type':…
- Mechanism: Fix support for tagged union with discriminator inside of `Annotated` with `Body()` in FastAPI.
- Why the fix works: Fix support for tagged union with discriminator inside of `Annotated` with `Body()` in FastAPI. (first fixed release: 0.128.4).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Triggered by an upgrade/regression window: 0.124.1 breaks; 0.128.4 is the first fixed release.
- Surfaces as: pydantic.errors.PydanticUserError: `Tag` not provided for choice {\n 'type': 'tagged-union',\n 'choices': {\n 'cat': {'type': 'definition-ref', 'metadata':\n…
Proof / Evidence
- GitHub issue: #14508
- Fix PR: https://github.com/fastapi/fastapi/pull/14512
- First fixed release: 0.128.4
- Affected versions: 0.124.1
- Reproduced locally: No (not executed)
- Last verified: 2026-02-08
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.40
Verified Execution
We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.
- Status: PASS
- Ran: 2026-02-11T16:52:29Z
- Package: fastapi
- Fixed: 0.128.4
- Mode: fixed_only
- Outcome: ok
Logs
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“This is fixed in https://github.com/fastapi/fastapi/pull/14512, it is now available in FastAPI 0.124.3 :tada:”
Failure Signature (Search String)
- pydantic.errors.PydanticUserError: `Tag` not provided for choice {\n 'type': 'tagged-union',\n 'choices': {\n 'cat': {'type': 'definition-ref', 'metadata':\n
Error Message
Stack trace
Error Message
-------------
pydantic.errors.PydanticUserError: `Tag` not provided for choice {\n 'type': 'tagged-union',\n 'choices': {\n 'cat': {'type': 'definition-ref', 'metadata':\n {'pydantic_internal_union_tag_key': 'cat'}},\n 'dog': {'type': 'definition-ref', 'metadata':\n {'pydantic_internal_union_tag_key': 'dog'}}\n },\n 'discriminator': <function get_pet_type at 0x...>\n } used with `Discriminator`
Minimal Reproduction
from typing import Annotated, Union
from pydantic import BaseModel, Tag, Discriminator
from fastapi import FastAPI, Body
class Cat(BaseModel):
pet_type: str = "cat"
meows: int
class Dog(BaseModel):
pet_type: str = "dog"
barks: float
def get_pet_type(v):
if isinstance(v, dict):
return v.get("pet_type", "")
return getattr(v, "pet_type", "")
# Define discriminated union
Pet = Annotated[
Union[
Annotated[Cat, Tag("cat")],
Annotated[Dog, Tag("dog")]
],
Discriminator(get_pet_type)
]
app = FastAPI()
# ✅ THIS WORKS in 0.124.1+
@app.post("/pet/works")
async def create_pet_works(pet: Pet = Body(...)):
return pet
# ❌ THIS BREAKS in 0.124.1+ (worked in 0.124.0)
@app.post("/pet/broken")
async def create_pet_broken(pet: Annotated[Pet, Body(...)]):
return pet
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install fastapi==0.128.4
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option B — Safe version pin Backward-compatible pin
pip install fastapi==0.124.0
Use when you can’t upgrade immediately. Plan a follow-up to upgrade (pins can accumulate security/compat debt).
Option C — Workaround Temporary workaround
Use pet: Pet = Body(...) instead of pet: Annotated[Pet,
Use only if you cannot change versions today. Treat this as a stopgap and remove once upgraded.
Fix reference: https://github.com/fastapi/fastapi/pull/14512
First fixed release: 0.128.4
Last verified: 2026-02-08. Validate in your environment.
When NOT to Use This Fix
- Do not use if it changes public behavior or if the failure cannot be reproduced.
- Do not use if you need features or security fixes in newer releases.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
- Pin production dependencies and upgrade only with a reproducible test that hits the failing path.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.124.0 | Working |
| 0.124.1 | Working |
| 0.124.1 | Broken |
| 0.128.4 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.