pola-rs/polars · error · TypeError
Unrecognised connection type
Error message
Unrecognised connection type {qualified_type_name(conn)!r}; no 'execute' or 'cursor' method What it means
read_database accepts a connection object, SQLAlchemy engine/session, or cursor-like object. _normalise_cursor probes for an 'execute' or 'cursor' method to obtain a usable cursor; if the passed object has neither, it raises TypeError because polars cannot execute the query against it.
Solutions
- Pass a real DBAPI connection, SQLAlchemy Engine, or open cursor object instead of a string or arbitrary object
- If using a wrapper/proxy, implement and delegate 'execute' or 'cursor' methods to the underlying connection
- Establish the connection first (e.g. engine.connect() or driver.connect(dsn)) before handing it to polars
- Use connection_uri= to let polars open the connection itself if you only have a URL
Example fix
// before
pl.read_database(query, "postgresql://user:pw@host/db")
// after
import psycopg2
conn = psycopg2.connect("postgresql://user:pw@host/db")
pl.read_database(query, conn)
// or simply
pl.read_database(query, "postgresql://user:pw@host/db".replace(...), engine="connectorx") # via connection_uri param Defensive patterns
Strategy: type-guard
Validate before calling
if not (hasattr(conn, "execute") or hasattr(conn, "cursor")):
raise TypeError("connection must be a DBAPI connection/cursor or SQLAlchemy engine/session") Type guard
def is_supported_connection(conn) -> bool:
return callable(getattr(conn, "execute", None)) or callable(getattr(conn, "cursor", None)) Try / catch
try:
df = pl.read_database(query, conn)
except TypeError as e:
if "Unrecognised connection type" in str(e):
conn = engine.connect() # open a real connection
df = pl.read_database(query, conn) Prevention
- Never pass DSN strings as the connection object unless using connection_uri/engine params
- Wrap connections only with proxies that delegate execute/cursor
- Match sync/async driver APIs to the corresponding polars call
When it happens
Trigger: Passing an object to pl.read_database(..., connection=...) that is not a DBAPI connection/cursor or SQLAlchemy engine/session/connection — e.g. a raw connection string URL, a closed/pooled object without those methods, or a custom wrapper that forgets to expose execute/cursor.
Common situations: Passing a DSN string instead of an established connection; passing a connectorx/odbc handle; wrapping connections in a proxy class without delegating execute/cursor; mixing async drivers with the sync API.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- expected connection to be a URI string; found
- arr.to_struct() got a str instead of a list. hint: pass
- Array constructor is missing the required argument `shape`
- cannot call `map_groups` when filtering groups with `having`
- cannot call `map_groups` when grouping by an expression
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/92c784674dd94537.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/database/_executor.py:472
return conn
elif hasattr(conn, "cursor"):
# connection has a dedicated cursor; prefer over direct execute
cursor = (
cast("Cursor", cursor()) if callable(cursor := conn.cursor) else cursor
)
self.can_close_cursor = True
return cursor
elif hasattr(conn, "execute"):
# can execute directly (given cursor, sqlalchemy connection, etc)
return conn
msg = (
f"Unrecognised connection type {qualified_type_name(conn)!r}; no "
"'execute' or 'cursor' method"
)
raise TypeError(msg)
def _oracle_arrow_proxy(self) -> OracleCursorProxy | None:
"""Return an Arrow proxy for a SQLAlchemy "python-oracledb" connection."""
if not self._is_alchemy_async(self.cursor):
driver_name = getattr(getattr(self.cursor, "engine", None), "driver", None)
if driver_name == "oracledb" and OracleCursorProxy.supports_arrow(
# note: proxy the established DBAPI connection so the query
# is executed in the caller's own session/transaction
raw_conn := getattr(
getattr(self.cursor, "connection", None), "driver_connection", None
)
):
return OracleCursorProxy(raw_conn)
return None
async def _sqlalchemy_async_execute(self, query: TextClause, **options: Any) -> Any:
"""Execute a query using an async SQLAlchemy connection."""
is_session = self._is_alchemy_session(self.cursor)View on GitHub (pinned to fe841f959e)