pola-rs/polars · error · ValueError

string URI is invalid here; call `read_database_uri` instead

Error message

string URI is invalid here; call `read_database_uri` instead

What it means

`read_database` expects a live DBAPI connection object (or an sqlalchemy/adbc connection), not a connection-string URI. If the `connection` argument is a string containing "://", polars assumes you meant `read_database_uri` and raises ValueError to redirect you.

Solutions

  1. Call `read_database_uri(query, uri)` instead for URI strings.
  2. Or open a real connection first (e.g. `conn = sqlite3.connect(path)` or an sqlalchemy engine) and pass that to read_database.
  3. For sqlalchemy, pass `engine.connect()` / the engine with the right dialect, not its URL.

Example fix

// before
pl.read_database("SELECT * FROM t", "postgresql://user@host/db")  # raises
// after
pl.read_database_uri("SELECT * FROM t", "postgresql://user@host/db")
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(connection, str):
    raise TypeError("read_database needs a connection object; use read_database_uri for URI strings")

Type guard

def is_dbapi_connection(conn) -> bool:
    return not isinstance(conn, str) and hasattr(conn, "cursor")

Try / catch

try:
    df = pl.read_database(q, connection)
except ValueError as e:
    if "read_database_uri" in str(e):
        df = pl.read_database_uri(q, connection)
    else:
        raise

Prevention

When it happens

Trigger: Calling `read_database(query, "postgresql://user:pass@host/db")` — a URI string passed where a connection object is required.

Common situations: Copy-paste from read_database_uri examples; confusion between the two APIs; code refactored from URI-based to connection-based querying.

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


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/0ae3f520d0c5291f. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/database/functions.py:257

    ...         query="SELECT * FROM test",
    ...         url="http://localhost:8000",
    ...     )
    ... )  # doctest: +SKIP

    """  # noqa: W505
    if isinstance(connection, str):
        # check for odbc connection string
        if re.search(r"\bdriver\s*=\s*{[^}]+?}", connection, re.IGNORECASE):
            _ = import_optional(
                module_name="arrow_odbc",
                err_prefix="use of ODBC connection string requires the",
                err_suffix="package",
            )
            connection = ODBCCursorProxy(connection)
        elif "://" in connection:
            # otherwise looks like a mistaken call to read_database_uri
            msg = "string URI is invalid here; call `read_database_uri` instead"
            raise ValueError(msg)
        else:
            msg = "unable to identify string connection as valid ODBC (no driver)"
            raise ValueError(msg)

    # adbc_driver_manager must be >= 1.7.0 to support passing Python sequences into
    # parameterised queries (via execute_options) without PyArrow installed
    if (
        execute_options is not None
        and not _PYARROW_AVAILABLE
        and type(connection).__module__.split(".", 1)[0].startswith("adbc")
    ):
        adbc_version_no_pyarrow_required = "1.7.0"
        adbc_driver_manager = import_optional("adbc_driver_manager")
        adbc_str_version = getattr(adbc_driver_manager, "__version__", "0.0")
        if not parse_version(adbc_str_version) >= parse_version(
            adbc_version_no_pyarrow_required
        ):
            msg = (

View on GitHub (pinned to fe841f959e)