Jump to solution
Verify

The Fix

pip install pydantic==2.11.8

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

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -326,18 +326,15 @@ def collect_model_fields( # noqa: C901 # extend this to any descriptor in the future (by simply checking for # `hasattr(assigned_value.default, '__get__')`). - assigned_value.default = assigned_value.default.__get__(None, cls) - - # The `from_annotated_attribute()` call below mutates the assigned `Field()`, so make a copy:
repro.py
from datetime import date, datetime import pytz from pydantic import ( AwareDatetime, BeforeValidator, ) from pydantic_xml import BaseXmlModel, element from typing_extensions import Annotated def make_datetime_timezone_aware(v): if isinstance(v, str): d = datetime.fromisoformat(v) elif isinstance(v, date) or isinstance(v, datetime): d = datetime.fromisoformat(v.isoformat(sep="T")) # type:ignore [call-arg] else: return v if d.tzinfo is None or d.tzinfo.utcoffset is None: d = pytz.timezone("Europe/Paris").localize(d, is_dst=None) # type:ignore [call-arg] if isinstance(v, str): return d.isoformat(sep="T") else: return d PDPAwareDatetime = Annotated[ AwareDatetime, BeforeValidator(make_datetime_timezone_aware), ] class test(BaseXmlModel, tag="index", search_mode="unordered"): date: datetime = element(tag="date") class test_xml(BaseXmlModel, tag="index", search_mode="unordered"): date: PDPAwareDatetime = element(tag="date") if __name__ == "__main__": test.from_xml(b'<index><date>2025-10-08</date></index>') test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function test_xml.from_xml(b'<index>THISISATEST<date>2025-10-08</date></index>') # Fails with v = 'THISISATEST'
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.8\nWhen NOT to use: This fix should not be applied if the application relies on the previous behavior of BeforeValidator.\n\n

Why This Fix Works in Production

  • Trigger: test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function
  • Mechanism: The BeforeValidator fails to receive the expected value during XML serialization due to internal changes in Pydantic
  • Why the fix works: Refactor `FieldInfo` creation implementation to centralize merging logic and avoid repetition, addressing issues related to subclassing `FieldInfo`. (first fixed release: 2.11.8).
Production impact:
  • 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

  • The BeforeValidator fails to receive the expected value during XML serialization due to internal changes in Pydantic
  • Production symptom (often without a traceback): test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“Thanks for your quick response, even if it's the kind of answer I feared ;-). I will relay the answer to pydantic-xml projetc. Keep up…”
@canardoFR · 2025-10-09 · source
“Dear pydantic team, the external library pydantic-xml has been fixed as of version 2.18 Our latest tests are all OK with pydantic 2.12 and pydantic-xml…”
@canardoFR · 2025-10-13 · source
“This is due to a refactor (see relevant the blog post section)”
@Viicos · 2025-10-09 · source

Failure Signature (Search String)

  • test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function
  • test_xml.from_xml(b'<index>THISISATEST<date>2025-10-08</date></index>') # Fails with v = 'THISISATEST'
Copy-friendly signature
signature.txt
Failure Signature ----------------- test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function test_xml.from_xml(b'<index>THISISATEST<date>2025-10-08</date></index>') # Fails with v = 'THISISATEST'

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function test_xml.from_xml(b'<index>THISISATEST<date>2025-10-08</date></index>') # Fails with v = 'THISISATEST'

Minimal Reproduction

repro.py
from datetime import date, datetime import pytz from pydantic import ( AwareDatetime, BeforeValidator, ) from pydantic_xml import BaseXmlModel, element from typing_extensions import Annotated def make_datetime_timezone_aware(v): if isinstance(v, str): d = datetime.fromisoformat(v) elif isinstance(v, date) or isinstance(v, datetime): d = datetime.fromisoformat(v.isoformat(sep="T")) # type:ignore [call-arg] else: return v if d.tzinfo is None or d.tzinfo.utcoffset is None: d = pytz.timezone("Europe/Paris").localize(d, is_dst=None) # type:ignore [call-arg] if isinstance(v, str): return d.isoformat(sep="T") else: return d PDPAwareDatetime = Annotated[ AwareDatetime, BeforeValidator(make_datetime_timezone_aware), ] class test(BaseXmlModel, tag="index", search_mode="unordered"): date: datetime = element(tag="date") class test_xml(BaseXmlModel, tag="index", search_mode="unordered"): date: PDPAwareDatetime = element(tag="date") if __name__ == "__main__": test.from_xml(b'<index><date>2025-10-08</date></index>') test_xml.from_xml(b'<index><date>2025-10-08</date></index>') # Fails with no call to BeforeValidator function test_xml.from_xml(b'<index>THISISATEST<date>2025-10-08</date></index>') # Fails with v = 'THISISATEST'

Environment

  • Pydantic: 2.12

What Broke

Serialization of XML fails, leading to application errors and potential data processing issues.

Why It Broke

The BeforeValidator fails to receive the expected value during XML serialization due to internal changes in Pydantic

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==2.11.8

When NOT to use: This fix should not be applied if the application relies on the previous behavior of BeforeValidator.

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/11898

First fixed release: 2.11.8

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

  • This fix should not be applied if the application relies on the previous behavior of BeforeValidator.

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.8 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.