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%.
@@ -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'):
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
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 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).
- 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
- GitHub issue: #10122
- Fix PR: https://github.com/pydantic/pydantic/pull/10137
- First fixed release: 1.10.18
- 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.32
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“@dmder, Good find! I'll root around for a fix.”
“I should be able to submit a PR tomorrow with a fix for this 👍”
“Thank you @sydney-runkle for such a quick turnaround on this 👍”
Failure Signature (Search String)
- from pydantic import BaseModel, ImportString
Error Message
Stack trace
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
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
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.
When NOT to Use This Fix
- This fix should not be used if the application relies on other non-standard stream objects.
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.