A Python dict sent to PostgreSQL stops with can't adapt type 'dict'
Your error
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'dict'2 more lines it can showFix it fast
Most common cause, 1 of 3A raw driver call or text() query passes the dict as one parameter value
Wrap the value in the driver's JSON wrapper, or with psycopg2 register a global adapter for dict.
# psycopg2
from psycopg2.extras import Json
cur.execute("INSERT INTO events (payload) VALUES (%s)", [Json(payload)])
# or once at startup, for every dict:
# psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
# psycopg 3
from psycopg.types.json import Jsonb
conn.execute("INSERT INTO events (payload) VALUES (%s)", [Jsonb(payload)])Short answer
(psycopg2.ProgrammingError) can't adapt type 'dict' means a Python dict reached the driver as a single query value, and the driver has no rule for turning it into SQL. psycopg 3 says cannot adapt type 'dict' using placeholder '%s'. Wrap the value in Json or Jsonb, or declare the column as JSONB in SQLAlchemy or in pandas to_sql(dtype=...).
Which cause do you have?
Find which value is the dict and which call sends it. Print the Python type of every parameter you pass to execute() or text(), or of the first row of the DataFrame you pass to to_sql(); the dict-typed entry names the column the causes below are about.
print({k: type(v).__name__ for k, v in params.items()}) # execute() / text() parameters
print(df.iloc[0].map(type)) # DataFrame given to to_sql()Then find the line you see in this list and open its fix:
- 1A raw driver call or text() query passes the dict as one parameter value
The traceback ends in cursor.execute() or session.execute(text(...)), and one of the bound values is a dict - 2pandas to_sql() writes a column of dicts without a JSON type
The traceback goes through DataFrame.to_sql, and the first step shows dict in one column - 3The SQLAlchemy model maps a JSON column as String or Text
The error comes from session.commit() or an insert() on a mapped table whose column type is String or Text while the attribute holds a dict
What does the error look like?
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'dict'psycopg2.ProgrammingError: can't adapt type 'dict'psycopg.ProgrammingError: cannot adapt type 'dict' using placeholder '%s' (format: AUTO)
Cause 1 of 3
A raw driver call or text() query passes the dict as one parameter value
You seeThe traceback ends in cursor.execute() or session.execute(text(...)), and one of the bound values is a dict
Check
can't adapt type 'dict' from a hand-written query means the dict is used as a column value rather than as the mapping of parameter names. Neither psycopg2 nor psycopg 3 dumps a dict to json by default: psycopg 3 says outright that the object has to be wrapped, and psycopg2 only does it after an adapter is registered.
print(type(payload)) # <class 'dict'> is the value the driver refusesFix
Wrap the value in the driver's JSON wrapper, or with psycopg2 register a global adapter for dict. Use Jsonb for a jsonb column and Json for json.
# psycopg2
from psycopg2.extras import Json
cur.execute("INSERT INTO events (payload) VALUES (%s)", [Json(payload)])
# or once at startup, for every dict:
# psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json)
# psycopg 3
from psycopg.types.json import Jsonb
conn.execute("INSERT INTO events (payload) VALUES (%s)", [Jsonb(payload)])Verify
The insert succeeds and the stored value reads back as a JSON object.
SELECT jsonb_typeof(payload) FROM events ORDER BY id DESC LIMIT 1; -- objectConfirmed by psycopg.org
Cause 2 of 3
pandas to_sql() writes a column of dicts without a JSON type
You seeThe traceback goes through DataFrame.to_sql, and the first step shows dict in one column
Check
This version appears when a DataFrame column holds dicts, for example after loading an API response. Without a dtype for that column, the values go to the driver as plain dicts, which it cannot adapt. The dtype argument of to_sql() maps column names to SQLAlchemy types, and with JSONB SQLAlchemy serializes each dict before the driver sees it.
print(df.dtypes)Fix
Pass the column type explicitly, so SQLAlchemy handles the JSON conversion.
from sqlalchemy.dialects.postgresql import JSONB
df.to_sql("events", engine, if_exists="append", index=False,
dtype={"payload": JSONB})Verify
to_sql() finishes, and the column in PostgreSQL holds JSON objects.
SELECT payload FROM events LIMIT 3;Confirmed by pandas.pydata.org
Cause 3 of 3
The SQLAlchemy model maps a JSON column as String or Text
You seeThe error comes from session.commit() or an insert() on a mapped table whose column type is String or Text while the attribute holds a dict
Check
When the ORM raises can't adapt type 'dict', the mapped column type tells SQLAlchemy to pass the value through untouched. A JSON or JSONB column type is what makes SQLAlchemy convert a dict on the way in, as in its own JSONB example that inserts a dict directly.
print(Event.__table__.c.payload.type)Fix
Declare the attribute with the PostgreSQL JSONB type (and make the database column jsonb to match).
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
class Event(Base):
__tablename__ = "events"
id: Mapped[int] = mapped_column(primary_key=True)
payload: Mapped[dict] = mapped_column(JSONB)Verify
Adding an Event with a dict payload commits without the adapt error.
session.add(Event(payload={"key1": "value1"}))
session.commit()Confirmed by docs.sqlalchemy.org
How do I stop it coming back?
- Type every JSON column as JSONB in the SQLAlchemy model and in the migration, never as Text.
- Pass an explicit dtype mapping whenever to_sql() writes a DataFrame that came from JSON.
- In raw queries, wrap JSON values with Json or Jsonb at the call site instead of relying on a global adapter that a later refactor may drop.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.