Jump to solution
Verify

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%.

Jump to Verify Open PR/Commit
@@ -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
repro.py
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' """
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==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).
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

  • 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

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…”
@Viicos · 2024-08-13 · source
“You're right that either would behave correctly since this may be in a category faulty model i may close the issue??”
@atanas-balevsky · 2024-08-13 · source
“The issue you raised is still valid. The patch can be found here: https://github.com/pydantic/pydantic/pull/10121”
@Viicos · 2024-08-13 · source

Failure Signature (Search String)

  • `PlainSerializer` not respecting `when_used` field during generation of json schema

Error Message

Stack trace
error.txt
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

repro.py
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

When NOT to use: This fix should not be used if the serializer needs to handle None values differently.

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.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be used if the serializer needs to handle None values differently.

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