The Fix
pip install pydantic==2.11.0
Based on closed pydantic/pydantic issue #9904 · PR/commit linked
@@ -297,7 +297,8 @@ jobs:
- name: Run ODMantic tests
- run: pytest tests
+ # Disabled tests, as per https://github.com/art049/odmantic/issues/512:
+ run: pytest tests -k 'not test_custom_bson_serializable and not test_sync_custom_bson_serializable and not test_with_bson_serializer_override_builtin_bson'
from typing import Final
from typing_extensions import Annotated
from pydantic import BaseModel, ConfigDict
import pydantic
class TestModel1(BaseModel):
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True)
normal_field: Annotated[str, pydantic.Field(alias="normalField")]
SOME_CONST: Annotated[
Final[int],
pydantic.Field(alias="someConst"),
] = 9007199254740991
class TestModel2(BaseModel):
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True)
normal_field: Annotated[str, pydantic.Field(alias="normalField")]
SOME_CONST: Final[
Annotated[
int,
pydantic.Field(alias="someConst"),
]
] = 9007199254740991
t1 = TestModel1(normal_field="test")
t2 = TestModel2(normal_field="test")
d1 = t1.model_dump(by_alias=True)
d2 = t2.model_dump(by_alias=True)
print(d1)
print(d2)
assert d1["someConst"] == 9007199254740991
assert d2["someConst"] == 9007199254740991
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install pydantic==2.11.0\nWhen NOT to use: Do not use this fix if you require the `Final` annotation to enforce immutability.\n\n
Why This Fix Works in Production
- Trigger: Order of `Final` annotation changes behaviour of field
- Mechanism: The order of the `Final` annotation affects the serialization of fields in Pydantic models
- Why the fix works: Addresses a known issue with the order of the `Final` annotation affecting the behavior of fields in Pydantic models. (first fixed release: 2.11.0).
- If left unfixed, this can cause silent data inconsistencies that propagate (bad cache entries, incorrect downstream decisions).
Why This Breaks in Prod
- Shows up under Python 3.8 in real deployments (not just unit tests).
- The order of the `Final` annotation affects the serialization of fields in Pydantic models
- Production symptom (often without a traceback): Order of `Final` annotation changes behaviour of field
Proof / Evidence
- GitHub issue: #9904
- Fix PR: https://github.com/pydantic/pydantic/pull/11479
- First fixed release: 2.11.0
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.47
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“This is a known mypy bug: https://github.com/python/mypy/issues/12061. I think the best path to take here is to keep using the first example an use a…”
“So actually I went too fast when reading this”
Failure Signature (Search String)
- Order of `Final` annotation changes behaviour of field
- `error: Final can be only used as an outermost qualifier in a variable annotation [valid-type]`
Copy-friendly signature
Failure Signature
-----------------
Order of `Final` annotation changes behaviour of field
`error: Final can be only used as an outermost qualifier in a variable annotation [valid-type]`
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Order of `Final` annotation changes behaviour of field
`error: Final can be only used as an outermost qualifier in a variable annotation [valid-type]`
Minimal Reproduction
from typing import Final
from typing_extensions import Annotated
from pydantic import BaseModel, ConfigDict
import pydantic
class TestModel1(BaseModel):
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True)
normal_field: Annotated[str, pydantic.Field(alias="normalField")]
SOME_CONST: Annotated[
Final[int],
pydantic.Field(alias="someConst"),
] = 9007199254740991
class TestModel2(BaseModel):
model_config = ConfigDict(populate_by_name=True, arbitrary_types_allowed=True)
normal_field: Annotated[str, pydantic.Field(alias="normalField")]
SOME_CONST: Final[
Annotated[
int,
pydantic.Field(alias="someConst"),
]
] = 9007199254740991
t1 = TestModel1(normal_field="test")
t2 = TestModel2(normal_field="test")
d1 = t1.model_dump(by_alias=True)
d2 = t2.model_dump(by_alias=True)
print(d1)
print(d2)
assert d1["someConst"] == 9007199254740991
assert d2["someConst"] == 9007199254740991
Environment
- Python: 3.8
- Pydantic: 2
What Broke
The `someConst` field is missing from the model dump, leading to incorrect data representation.
Why It Broke
The order of the `Final` annotation affects the serialization of fields in Pydantic models
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.11.0
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/pydantic/pydantic/pull/11479
First fixed release: 2.11.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if you require the `Final` annotation to enforce immutability.
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
- Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
- Upgrade behind a canary and run integration tests against the canary before 100% rollout.
Version Compatibility Table
| Version | Status |
|---|---|
| 2.11.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.