pola-rs/polars · error · TypeError
expected connection to be a URI string; found
Error message
expected connection to be a URI string; found {qualified_type_name(uri)!r} What it means
`read_database_uri` requires `uri` to be a plain string containing one or more connection URIs. Passing any other type (list, bytes, connection object, None) raises TypeError, since only connectorx-style URI strings are accepted.
Solutions
- Pass a single URI string; connectorx accepts multiple URIs inside one string separated by `|` if supported.
- If you have a connection object, use `read_database` instead.
- Convert Path/URL objects to str before calling: `str(uri)`.
Example fix
// before pl.read_database_uri(q, ["postgresql://u@h/db"]) # raises // after pl.read_database_uri(q, "postgresql://u@h/db")
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(uri, str):
raise TypeError("read_database_uri expects a single URI string") Type guard
def is_uri_string(uri) -> bool:
return isinstance(uri, str) and "://" in uri Try / catch
try:
df = pl.read_database_uri(q, uri)
except TypeError as e:
if "URI string" in str(e):
df = pl.read_database_uri(q, str(uri) if not isinstance(uri, (list, dict)) else uri[0])
else:
raise Prevention
- Coerce Path/URL objects with str() before calling
- Never pass connection objects to read_database_uri
- Validate URI shape at config boundaries with a small guard function
When it happens
Trigger: Calling `read_database_uri(query, ["postgres://..."])` (list of URIs), passing a connection object, passing a Path, or passing None as uri.
Common situations: Copying DBAPI-style code to the URI API; passing a list of URIs expecting multi-URI load balancing; passing an sqlalchemy Engine object by mistake.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- string URI is invalid here; call `read_database_uri` instead
- Unrecognised connection type
- 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`
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/51d68d0cba844cf5.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/database/functions.py:496
... "sqlite:///:memory:",
... engine="adbc",
... execute_options={"parameters": (30,)},
... ) # doctest: +SKIP
Or pass multiple parameters:
>>> df = pl.read_database_uri(
... "SELECT * FROM employees WHERE hourly_rate BETWEEN ? AND ?",
... "sqlite:///:memory:",
... engine="adbc",
... execute_options={"parameters": (40, 20)},
... ) # doctest: +SKIP
"""
from polars.io.database._utils import _read_sql_adbc, _read_sql_connectorx
if not isinstance(uri, str):
msg = f"expected connection to be a URI string; found {qualified_type_name(uri)!r}"
raise TypeError(msg)
elif engine is None:
engine = "connectorx"
if engine == "connectorx":
if execute_options:
msg = "the 'connectorx' engine does not support use of `execute_options`"
raise ValueError(msg)
if pre_execution_query:
issue_unstable_warning(
"the 'pre-execution-query' parameter is considered unstable."
)
return _read_sql_connectorx(
query,
connection_uri=uri,
partition_on=partition_on,
partition_range=partition_range,
partition_num=partition_num,
protocol=protocol,View on GitHub (pinned to fe841f959e)