Jump to solution
Verify

The Fix

Resolves a warning that occurs when using built-in functions for JSON serialization in psycopg. It ensures that no warnings are raised when these functions are used.

Based on closed psycopg/psycopg issue #1165 · 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
@@ -43,6 +43,13 @@ Psycopg 3.3.0 (unreleased) +Psycopg 3.2.11 (unreleased) +^^^^^^^^^^^^^^^^^^^^^^^^^^^ +
repro.py
In [4]: import psycopg In [5]: import orjson # this causes no warning In [6]: psycopg.types.json.set_json_dumps(orjson.dumps) In [7]: conn = psycopg.connect() # This causes a warning In [8]: psycopg.types.json.set_json_dumps(orjson.dumps, conn) /home/piro/dev/psycopg3/psycopg/build/__editable__.psycopg-3.3.0.dev1-py3-none-any/psycopg/types/json.py:165: RuntimeWarning: function <built-in function dumps> has no __code__ or __closure__. warn(f"function {f} has no __code__ or __closure__.", RuntimeWarning)
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Apply the official fix\nResolves a warning that occurs when using built-in functions for JSON serialization in psycopg. It ensures that no warnings are raised when these functions are used.\nWhen NOT to use: This fix should not be used if the built-in functions are not intended for JSON serialization.\n\n

Why This Fix Works in Production

  • Trigger: [json] new warning emited by a recent change in types/json.py with orjson
  • Mechanism: The warning is caused by using built-in functions for JSON serialization without proper handling in psycopg
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

  • The warning is caused by using built-in functions for JSON serialization without proper handling in psycopg
  • Production symptom (often without a traceback): [json] new warning emited by a recent change in types/json.py with orjson

Proof / Evidence

Discussion

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

“Thank you very much for the follow-up. I think that the fix in #1166 will solve the problem. In the meantime you can work around…”
@dvarrazzo · 2025-09-10 · source
“Hi, Thank you for the quick response ! My real case is slighltly more intricated and involve SQLAlchemy as well”
@yoch · 2025-09-10 · source
“Hello, thank you for the report! Yes, you are correct and this comes from #1108 My understanding from the behaviour I have tested and the…”
@dvarrazzo · 2025-09-10 · source

Failure Signature (Search String)

  • [json] new warning emited by a recent change in types/json.py with orjson
Copy-friendly signature
signature.txt
Failure Signature ----------------- [json] new warning emited by a recent change in types/json.py with orjson

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- [json] new warning emited by a recent change in types/json.py with orjson

Minimal Reproduction

repro.py
In [4]: import psycopg In [5]: import orjson # this causes no warning In [6]: psycopg.types.json.set_json_dumps(orjson.dumps) In [7]: conn = psycopg.connect() # This causes a warning In [8]: psycopg.types.json.set_json_dumps(orjson.dumps, conn) /home/piro/dev/psycopg3/psycopg/build/__editable__.psycopg-3.3.0.dev1-py3-none-any/psycopg/types/json.py:165: RuntimeWarning: function <built-in function dumps> has no __code__ or __closure__. warn(f"function {f} has no __code__ or __closure__.", RuntimeWarning)

What Broke

Users experience warnings when using orjson with psycopg for JSON serialization.

Why It Broke

The warning is caused by using built-in functions for JSON serialization without proper handling in psycopg

Fix Options (Details)

Option A — Apply the official fix

Resolves a warning that occurs when using built-in functions for JSON serialization in psycopg. It ensures that no warnings are raised when these functions are used.

When NOT to use: This fix should not be used if the built-in functions are not intended for JSON serialization.

Fix reference: https://github.com/psycopg/psycopg/pull/1166

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 built-in functions are not intended for JSON serialization.

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

  • Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
  • Pin production dependencies and upgrade only with a reproducible test that hits the failing path.

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.