pola-rs/polars · error · ValueError
Unable to determine metadata from query result;
Error message
Unable to determine metadata from query result; {self.result!r} What it means
Raised by `_from_rows` when polars cannot infer column names/metadata from the returned result object: the result exposes neither a usable `cursor.description`, nor a top-level `description`, nor `_metadata.keys`. Polars needs at least column names to build the DataFrame from row data, so an unrecognizable result type is rejected rather than guessed at.
Solutions
- Inspect `repr(result)` from the message: verify it's the object type you intended, and that you passed the correct connection/cursor to `read_database`.
- Ensure the result object exposes DB-API `description` (sequence of 7-tuples) after executing the query.
- If using a custom driver/wrapper, implement `description` or `_metadata` with a `keys` attribute on the returned result.
- If the driver has an Arrow interface, use it (Arrow path) so metadata comes from the Arrow schema instead.
- For async drivers, ensure the coroutine result is resolved before being handed to the executor.
Example fix
// before: custom cursor missing metadata
class MyCursor:
def fetchall(self): return rows
// after: implement DB-API description
class MyCursor:
def fetchall(self): return rows
@property
def description(self):
return [("a", None), ("b", None)] Defensive patterns
Strategy: validation
Validate before calling
def result_has_metadata(result) -> bool:
cur = getattr(result, "cursor", result)
return bool(
hasattr(cur, "description")
or hasattr(result, "description")
or hasattr(result, "_metadata")
)
assert result_has_metadata(cursor), "result does not expose DB-API description" Type guard
def has_description(result: object) -> bool:
desc = getattr(result, "description", None)
return isinstance(desc, (list, tuple)) and len(desc) > 0 Try / catch
try:
df = pl.read_database(query, connection)
except ValueError as e:
if "Unable to determine metadata" in str(e):
logger.error("result object %s lacks DB-API description; check driver wrapper", e)
raise Prevention
- Use official, maintained DB-API drivers instead of hand-rolled wrappers.
- In tests, make cursor mocks implement `description` exactly as DB-API specifies.
- Prefer Arrow-capable drivers so polars reads schema from Arrow metadata, not cursor.description.
- Log `type(result)` and `repr(result)` when debugging drivers to catch wrong-object mistakes early.
When it happens
Trigger: Calling `pl.read_database` with a `connection`/result object that doesn't implement the DB-API `description` attribute (and lacks `_metadata`), e.g. a raw connection passed instead of a cursor, a custom/fake cursor, an async result not awaited, or a driver returning an unexpected wrapper object.
Common situations: Wrapping a non-DB-API driver in a thin shim that forgot `description`; passing the wrong object as the connection (e.g. engine/session instead of cursor-bearing object); mocking cursors in tests without a `description` attribute; driver upgrades that renamed internal `_metadata`.
Related errors
- Cannot set `iter_batches` for
- Cannot set `iter_batches` without also setting a non-zero…
- engine must be one of
- the 'adbc' engine does not support use of…
- accessing ` ` from the top-level `polars` module was…
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/a1ae5c389cf88be1.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/io/database/_executor.py:308
msg = (
"Cannot set `iter_batches` without also setting a non-zero `batch_size`"
)
raise ValueError(msg)
if is_async := isinstance(original_result := self.result, Coroutine):
self.result = _run_async(self.result)
try:
if hasattr(self.result, "fetchall"):
if is_alchemy := (self.driver_name == "sqlalchemy"):
if hasattr(self.result, "cursor"):
cursor_desc = [
(d[0], d[1:]) for d in self.result.cursor.description
]
elif hasattr(self.result, "_metadata"):
cursor_desc = [(k, None) for k in self.result._metadata.keys]
else:
msg = f"Unable to determine metadata from query result; {self.result!r}"
raise ValueError(msg)
elif hasattr(self.result, "description"):
cursor_desc = [(d[0], d[1:]) for d in self.result.description]
else:
cursor_desc = []
schema_overrides = self._inject_type_overrides(
description=cursor_desc,
schema_overrides=(schema_overrides or {}),
)
result_columns = [nm for nm, _ in cursor_desc]
frames = (
DataFrame(
data=rows,
schema=result_columns or None,
schema_overrides=schema_overrides,
infer_schema_length=infer_schema_length,
orient="row",View on GitHub (pinned to fe841f959e)