{"record":{"id":"a1ae5c389cf88be1","repo":"pola-rs/polars","slug":"unable-to-determine-metadata-from-query-result-s","errorCode":null,"errorMessage":"Unable to determine metadata from query result; {self.result!r}","messagePattern":"Unable to determine metadata from query result; (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/database/_executor.py","lineNumber":308,"sourceCode":"            msg = (\n                \"Cannot set `iter_batches` without also setting a non-zero `batch_size`\"\n            )\n            raise ValueError(msg)\n\n        if is_async := isinstance(original_result := self.result, Coroutine):\n            self.result = _run_async(self.result)\n        try:\n            if hasattr(self.result, \"fetchall\"):\n                if is_alchemy := (self.driver_name == \"sqlalchemy\"):\n                    if hasattr(self.result, \"cursor\"):\n                        cursor_desc = [\n                            (d[0], d[1:]) for d in self.result.cursor.description\n                        ]\n                    elif hasattr(self.result, \"_metadata\"):\n                        cursor_desc = [(k, None) for k in self.result._metadata.keys]\n                    else:\n                        msg = f\"Unable to determine metadata from query result; {self.result!r}\"\n                        raise ValueError(msg)\n\n                elif hasattr(self.result, \"description\"):\n                    cursor_desc = [(d[0], d[1:]) for d in self.result.description]\n                else:\n                    cursor_desc = []\n\n                schema_overrides = self._inject_type_overrides(\n                    description=cursor_desc,\n                    schema_overrides=(schema_overrides or {}),\n                )\n                result_columns = [nm for nm, _ in cursor_desc]\n                frames = (\n                    DataFrame(\n                        data=rows,\n                        schema=result_columns or None,\n                        schema_overrides=schema_overrides,\n                        infer_schema_length=infer_schema_length,\n                        orient=\"row\",","sourceCodeStart":290,"sourceCodeEnd":326,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/database/_executor.py#L290-L326","documentation":"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.","triggerScenarios":"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.","commonSituations":"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`.","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."],"exampleFix":"// before: custom cursor missing metadata\nclass MyCursor:\n    def fetchall(self): return rows\n// after: implement DB-API description\nclass MyCursor:\n    def fetchall(self): return rows\n    @property\n    def description(self):\n        return [(\"a\", None), (\"b\", None)]","handlingStrategy":"validation","validationCode":"def result_has_metadata(result) -> bool:\n    cur = getattr(result, \"cursor\", result)\n    return bool(\n        hasattr(cur, \"description\")\n        or hasattr(result, \"description\")\n        or hasattr(result, \"_metadata\")\n    )\n\nassert result_has_metadata(cursor), \"result does not expose DB-API description\"","typeGuard":"def has_description(result: object) -> bool:\n    desc = getattr(result, \"description\", None)\n    return isinstance(desc, (list, tuple)) and len(desc) > 0","tryCatchPattern":"try:\n    df = pl.read_database(query, connection)\nexcept ValueError as e:\n    if \"Unable to determine metadata\" in str(e):\n        logger.error(\"result object %s lacks DB-API description; check driver wrapper\", e)\n    raise","preventionTips":["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."],"tags":["polars","database","metadata","dbapi","python"],"backgroundTag":"unexpected-response-shape","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}