The Fix
pip install pydantic==1.10.19
Based on closed pydantic/pydantic issue #10459 · 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%.
@@ -279,7 +279,7 @@ def _apply_constraint_with_incompatibility_info(
schema = cs.no_info_after_validator_function(
- partial(get_constraint_validator(constraint), **{'constraint_value': value}), schema
+ partial(get_constraint_validator(constraint), **{constraint: value}), schema
)
from datetime import timedelta
from typing import Annotated
from annotated_types import Le
from pydantic import BaseModel, BeforeValidator
def validator(v):
if isinstance(v, (int, float)):
return timedelta(days=v)
return v
class BeforeValidatorAfterLe(BaseModel):
v: Annotated[timedelta, Le(timedelta(days=365)), BeforeValidator(validator)]
class BeforeValidatorBeforeLe(BaseModel):
v: Annotated[timedelta, BeforeValidator(validator), Le(timedelta(days=365))]
try:
BeforeValidatorAfterLe(v=366) # raises ValueError, as expected
except ValueError as ex:
print(1, ex)
try:
BeforeValidatorBeforeLe(v=366) # raises TypeError in pydantic>=2.9.0
except ValueError as ex: # ValueError (as expected) in 2.8.2
print(2, "ValueError:", ex)
except TypeError as ex: # UNEXPECTED TypeError in pydantic>=2.9.0
print(2, "TypeError:", ex)
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==1.10.19\nWhen NOT to use: Do not use this fix if the application relies on the previous behavior of BeforeValidator.\n\n
Why This Fix Works in Production
- Trigger: TypeError when using BeforeValidator
- Mechanism: TypeError occurs when using BeforeValidator with constraints from annotated_types in Pydantic V2
- Why the fix works: Fixes a TypeError when using BeforeValidator with constraints from annotated_types in Pydantic V2. (first fixed release: 1.10.19).
- 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
- Shows up under Python 3.12 in real deployments (not just unit tests).
- TypeError occurs when using BeforeValidator with constraints from annotated_types in Pydantic V2
- Surfaces as: TypeError when using BeforeValidator
Proof / Evidence
- GitHub issue: #10459
- Fix PR: https://github.com/pydantic/pydantic/pull/10490
- First fixed release: 1.10.19
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- 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).
“Looks like an issue with annotation application, will look into this today!”
Failure Signature (Search String)
- TypeError when using BeforeValidator
Error Message
Stack trace
Error Message
-------------
TypeError when using BeforeValidator
Minimal Reproduction
from datetime import timedelta
from typing import Annotated
from annotated_types import Le
from pydantic import BaseModel, BeforeValidator
def validator(v):
if isinstance(v, (int, float)):
return timedelta(days=v)
return v
class BeforeValidatorAfterLe(BaseModel):
v: Annotated[timedelta, Le(timedelta(days=365)), BeforeValidator(validator)]
class BeforeValidatorBeforeLe(BaseModel):
v: Annotated[timedelta, BeforeValidator(validator), Le(timedelta(days=365))]
try:
BeforeValidatorAfterLe(v=366) # raises ValueError, as expected
except ValueError as ex:
print(1, ex)
try:
BeforeValidatorBeforeLe(v=366) # raises TypeError in pydantic>=2.9.0
except ValueError as ex: # ValueError (as expected) in 2.8.2
print(2, "ValueError:", ex)
except TypeError as ex: # UNEXPECTED TypeError in pydantic>=2.9.0
print(2, "TypeError:", ex)
Environment
- Python: 3.12
- Pydantic: 2
What Broke
Unexpected TypeError raised during model validation in production, causing application errors.
Why It Broke
TypeError occurs when using BeforeValidator with constraints from annotated_types in Pydantic V2
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==1.10.19
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/10490
First fixed release: 1.10.19
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix 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 |
|---|---|
| 1.10.19 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.