{"record":{"id":"5551befe9b2e8e5a","repo":"BerriAI/litellm","slug":"error-retrieving-usage-data-e","errorCode":null,"errorMessage":"Error retrieving usage data: {e}","messagePattern":"Error retrieving usage data: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/integrations/cloudzero/database.py","lineNumber":101,"sourceCode":"        params: Final[list[Any]] = [\n            start_time_utc,\n            end_time_utc,\n        ]\n\n        if limit is not None:\n            try:\n                params.append(int(limit))\n            except (TypeError, ValueError):\n                raise ValueError(\"limit must be an integer\")\n            query += \" LIMIT $3\"\n\n        try:\n            db_response: Final = await client.db.query_raw(query, *params)\n            # Convert the response to polars DataFrame with full schema inference\n            # This prevents schema mismatch errors when data types vary across rows\n            return pl.DataFrame(db_response, infer_schema_length=None)\n        except Exception as e:\n            raise Exception(f\"Error retrieving usage data: {e}\")\n","sourceCodeStart":83,"sourceCodeEnd":102,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/integrations/cloudzero/database.py#L83-L102","documentation":"get_usage_data() wraps both the Prisma query_raw call and the polars conversion (pl.DataFrame(db_response, infer_schema_length=None)) in a single try/except Exception and re-raises as a new generic Exception prefixed 'Error retrieving usage data:'. The real cause — SQL failure, permissions, dropped connection, or polars schema inference problems — survives only in the appended message text, so the root cause must be read out of the string.","triggerScenarios":"Missing/renamed LiteLLM_DailyUserSpend columns after schema drift; DB user without SELECT on the spend table; connection reset mid-query; polars failing to build a DataFrame when returned rows have incompatible types even with full inference; statement timeout on very large date ranges.","commonSituations":"Export job pointed at the wrong database or credentials; litellm upgraded but Prisma migrations not applied so the SQL no longer matches; long unbounded date ranges pulling millions of rows; transient network issues between the export job and Postgres.","solutions":["Read the text after 'Error retrieving usage data:' — it is the actual Postgres or polars error; fix that root cause first","Verify connectivity and grants: psql \"$DATABASE_URL\" -c 'SELECT 1 FROM \"LiteLLM_DailyUserSpend\" LIMIT 1'","Apply pending litellm DB migrations (restart the proxy so auto-migrate runs) so columns match the integration's SQL","Bound the query: pass limit and start_time_utc/end_time_utc windows to shrink result sets"],"exampleFix":"# before\ndf = await db.get_usage_data(start_time_utc=year_ago())  # Exception: Error retrieving usage data: statement timeout\n\n# after\ndf = await db.get_usage_data(start_time_utc=week_ago(), end_time_utc=now_utc(), limit=50_000)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    df = await db.get_usage_data(limit=limit, start_time_utc=start, end_time_utc=end)\nexcept Exception as e:\n    msg = str(e)\n    if not msg.startswith('Error retrieving usage data'):\n        raise\n    root = msg.split(':', 1)[1].strip()\n    logger.error('cloudzero export query failed: %s', root)\n    # do not blindly retry: the root cause is often schema or permissions\n    raise","preventionTips":["Run litellm DB migrations after every upgrade before exporting","Grant the export DB user SELECT on LiteLLM_* tables and verify with a manual query","Chunk exports by date window and set a limit to bound result size"],"tags":["database","query-failed","error-wrapping","cloudzero","polars"],"backgroundTag":"database-query-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}