Jump to solution
Verify

The Fix

pip install pydantic==1.10.18

Based on closed pydantic/pydantic issue #10122 · 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
@@ -1013,6 +1013,15 @@ def _serialize(v: Any) -> str: elif hasattr(v, '__module__') and hasattr(v, '__name__'): return f'{v.__module__}.{v.__name__}' + # Handle special cases for sys.XXX streams + # if we see more of these, we should consider a more general solution + elif hasattr(v, 'name'):
repro.py
from pydantic import BaseModel, ImportString class ImportThings(BaseModel): obj: ImportString ImportThings(obj='math.cos').model_dump_json() # ok ImportThings(obj='sys.stdout').model_dump_json() Traceback (most recent call last): File "...", line 364, in runcode coro = func() ^^^^^^ File "<input>", line 1, in <module> File ".../python3.12/site-packages/pydantic/main.py", line 415, in model_dump_json return self.__pydantic_serializer__.to_json( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: UnsupportedOperation: not readable
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 application relies on other non-standard stream objects.\n\n

Why This Fix Works in Production

  • Trigger: from pydantic import BaseModel, ImportString
  • Mechanism: Fixes serialization issues for `ImportString` when using `sys.stdout`.
  • Why the fix works: Fixes serialization issues for `ImportString` when using `sys.stdout`. (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.12 in real deployments (not just unit tests).
  • Surfaces as: from pydantic import BaseModel, ImportString

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“@dmder, Good find! I'll root around for a fix.”
@sydney-runkle · 2024-08-14 · source
“I should be able to submit a PR tomorrow with a fix for this 👍”
@sydney-runkle · 2024-08-14 · source
“Thank you @sydney-runkle for such a quick turnaround on this 👍”
@dmder · 2024-08-22 · source

Failure Signature (Search String)

  • from pydantic import BaseModel, ImportString

Error Message

Stack trace
error.txt
Error Message ------------- from pydantic import BaseModel, ImportString class ImportThings(BaseModel): obj: ImportString ImportThings(obj='math.cos').model_dump_json() # ok ImportThings(obj='sys.stdout').model_dump_json() Traceback (most recent call last): File "...", line 364, in runcode coro = func() ^^^^^^ File "<input>", line 1, in <module> File ".../python3.12/site-packages/pydantic/main.py", line 415, in model_dump_json return self.__pydantic_serializer__.to_json( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: UnsupportedOperation: not readable

Minimal Reproduction

repro.py
from pydantic import BaseModel, ImportString class ImportThings(BaseModel): obj: ImportString ImportThings(obj='math.cos').model_dump_json() # ok ImportThings(obj='sys.stdout').model_dump_json() Traceback (most recent call last): File "...", line 364, in runcode coro = func() ^^^^^^ File "<input>", line 1, in <module> File ".../python3.12/site-packages/pydantic/main.py", line 415, in model_dump_json return self.__pydantic_serializer__.to_json( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: UnsupportedOperation: not readable

Environment

  • Python: 3.12
  • Pydantic: 2

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 application relies on other non-standard stream objects.

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/10137

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 application relies on other non-standard stream objects.

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.