The Fix
pip install pydantic==1.10.18
Based on closed pydantic/pydantic issue #10118 · 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%.
@@ -1043,17 +1043,30 @@ def default_schema(self, schema: core_schema.WithDefaultSchema) -> JsonSchemaVal
# we reflect the application of custom plain, no-info serializers to defaults for
- # json schemas viewed in serialization mode
+ # JSON Schemas viewed in serialization mode:
# TODO: improvements along with https://github.com/pydantic/pydantic/issues/8208
from pydantic import BaseModel, Field, PlainSerializer
from decimal import Decimal as _Decimal
from typing_extensions import Annotated
Decimal = Annotated[
_Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used='json-unless-none')
]
class MyModel(BaseModel):
my_decimal: Decimal = Field(default=None)
print(f"schema {MyModel.model_json_schema(mode='serialization')}")
"""
File "/Users/programming/pydantic_work/pydantic/test.py", line 6, in <lambda>
_Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used='json-unless-none')
TypeError: float() argument must be a string or a real number, not 'NoneType'
"""
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.18\nWhen NOT to use: This fix should not be used if the serializer needs to handle None values differently.\n\n
Why This Fix Works in Production
- Trigger: `PlainSerializer` not respecting `when_used` field during generation of json schema
- Mechanism: The PlainSerializer was not respecting the when_used field during JSON schema generation when the default value is None
- Why the fix works: Addresses an issue where the `PlainSerializer` was not respecting the `when_used` field during JSON schema generation, specifically when the default value is `None`. (first fixed release: 1.10.18).
- 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.11 in real deployments (not just unit tests).
- The PlainSerializer was not respecting the when_used field during JSON schema generation when the default value is None
- Surfaces as: File "/Users/programming/pydantic_work/pydantic/test.py", line 6, in <lambda>\n _Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used='json-unless-none')
Proof / Evidence
- GitHub issue: #10118
- Fix PR: https://github.com/pydantic/pydantic/pull/10121
- First fixed release: 1.10.18
- 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.56
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I raised a patch. Note that you should include None in the annotation in some way, i.e. Decimal = Annotated[_Decimal | None] or my_decimal: Decimal…”
“You're right that either would behave correctly since this may be in a category faulty model i may close the issue??”
“The issue you raised is still valid. The patch can be found here: https://github.com/pydantic/pydantic/pull/10121”
Failure Signature (Search String)
- `PlainSerializer` not respecting `when_used` field during generation of json schema
Error Message
Stack trace
Error Message
-------------
File "/Users/programming/pydantic_work/pydantic/test.py", line 6, in <lambda>\n _Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used='json-unless-none')
Minimal Reproduction
from pydantic import BaseModel, Field, PlainSerializer
from decimal import Decimal as _Decimal
from typing_extensions import Annotated
Decimal = Annotated[
_Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used='json-unless-none')
]
class MyModel(BaseModel):
my_decimal: Decimal = Field(default=None)
print(f"schema {MyModel.model_json_schema(mode='serialization')}")
"""
File "/Users/programming/pydantic_work/pydantic/test.py", line 6, in <lambda>
_Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used='json-unless-none')
TypeError: float() argument must be a string or a real number, not 'NoneType'
"""
Environment
- Python: 3.11
- Pydantic: 2
What Broke
Schema generation fails due to serialization lambda being called unnecessarily, causing TypeError.
Why It Broke
The PlainSerializer was not respecting the when_used field during JSON schema generation when the default value is None
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==1.10.18
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/10121
First fixed release: 1.10.18
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix should not be used if the serializer needs to handle None values differently.
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.18 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.