Jump to solution
Verify

The Fix

pip install pydantic==2.11.0

Based on closed pydantic/pydantic issue #9904 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -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'
repro.py
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
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
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).
Production impact:
  • 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

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…”
@Viicos · 2024-07-16 · source
“So actually I went too fast when reading this”
@Viicos · 2025-02-25 · source

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
signature.txt
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.txt
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

repro.py
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

When NOT to use: Do not use this fix if you require the `Final` annotation to enforce immutability.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not use this fix if you require the `Final` annotation to enforce immutability.

Verify Fix

verify
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

VersionStatus
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.