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%.
@@ -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:
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'
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.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).
- 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
- GitHub issue: #12364
- Fix PR: https://github.com/pydantic/pydantic/pull/11898
- First fixed release: 2.11.8
- 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.40
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…”
“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…”
“This is due to a refactor (see relevant the blog post section)”
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
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 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
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
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.
When NOT to Use This Fix
- This fix should not be applied if the application relies on the previous behavior of BeforeValidator.
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.8 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.