The Fix
pip install pydantic==2.10.4
Based on closed pydantic/pydantic issue #11092 · 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%.
@@ -239,6 +239,9 @@ def __ge__(self, other: Any) -> bool:
return hash(self._url)
+ def __len__(self) -> int:
+ return len(str(self._url))
+
from pydantic import AnyUrl, Field, BaseModel
class SomeClass(BaseModel):
url: AnyUrl = Field(max_length=100)
SomeClass(url="https://example.com")
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.10.4\nWhen NOT to use: Do not use this fix if the AnyUrl type is expected to remain unchanged.\n\n
Why This Fix Works in Production
- Trigger: if len(x) > max_length:
- Mechanism: The AnyUrl type does not implement a length method, causing TypeError when max_length is used
- Why the fix works: Added a magic method to _BaseUrl class to support length checks with AnyUrl and Field max_length. (first fixed release: 2.10.4).
- 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).
- The AnyUrl type does not implement a length method, causing TypeError when max_length is used
- Surfaces as: Traceback (most recent call last):
Proof / Evidence
- GitHub issue: #11092
- Fix PR: https://github.com/pydantic/pydantic/pull/11111
- First fixed release: 2.10.4
- 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.65
Verified Execution
We executed the runnable minimal repro in a temporary environment and captured exit codes + logs.
- Status: PASS
- Ran: 2026-02-11T16:52:29Z
- Package: pydantic
- Fixed: 2.10.4
- Mode: fixed_only
- Outcome: ok
Logs
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Yep, I think a __len__ addition would be great here. PRs welcome. We can include this in v2.10.4. Thanks!”
“@sydney-runkle I can make a PR later today or tomorrow morning :)”
Failure Signature (Search String)
- if len(x) > max_length:
Error Message
Stack trace
Error Message
-------------
Traceback (most recent call last):
File "/Users/XXXX/XXX/XXXXX/.venv/lib/python3.12/site-packages/pydantic/_internal/_validators.py", line 324, in max_length_validator
if len(x) > max_length:
^^^^^^
TypeError: object of type 'AnyUrl' has no len()
Minimal Reproduction
from pydantic import AnyUrl, Field, BaseModel
class SomeClass(BaseModel):
url: AnyUrl = Field(max_length=100)
SomeClass(url="https://example.com")
Environment
- Python: 3.12
- Pydantic: 2
What Broke
Using AnyUrl with max_length results in TypeError, breaking model validation.
Why It Broke
The AnyUrl type does not implement a length method, causing TypeError when max_length is used
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.10.4
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/11111
First fixed release: 2.10.4
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if the AnyUrl type is expected to remain unchanged.
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.10.4 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.