The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(spend-tracking): hand plain dict rows to polars in the CloudZero and Focus exports
mateo-berri2b7e14872f7d
diff --git a/litellm/integrations/cloudzero/database.py b/litellm/integrations/cloudzero/database.pyindex 4adf725fd0f..2fb10ad8a96 100644--- a/litellm/integrations/cloudzero/database.py+++ b/litellm/integrations/cloudzero/database.py@@ -106,6 +106,6 @@ class LiteLLMDatabase:else [])recovered_rows: Final = await fill_missing_api_key_aliases(client, usage_rows)- return pl.DataFrame(tuple(recovered_rows), infer_schema_length=None)+ return pl.DataFrame([dict(row) for row in recovered_rows], infer_schema_length=None)except Exception as e:raise Exception(f"Error retrieving usage data: {e}")diff --git a/litellm/integrations/focus/database.py b/litellm/integrations/focus/database.pyindex f214aa02b5b..657c7e0d264 100644--- a/litellm/integrations/focus/database.py+++ b/litellm/integrations/focus/database.py@@ -108,7 +108,7 @@ class FocusLiteLLMDatabase:else [])recovered_rows: Final = await fill_missing_api_key_aliases(client, usage_rows)- return pl.DataFrame(tuple(recovered_rows), infer_schema_length=None)+ return pl.DataFrame([dict(row) for row in recovered_rows], infer_schema_length=None)except Exception as exc:raise RuntimeError(f"Error retrieving usage data: {exc}") from excdiff --git a/tests/test_litellm/integrations/cloudzero/test_cloudzero.py b/tests/test_litellm/integrations/cloudzero/test_cloudzero.pyindex 1b6e8ca513c..6ddb8cbaa7c 100644--- a/tests/test_litellm/integrations/cloudzero/test_cloudzero.py+++ b/tests/test_litellm/integrations/cloudzero/test_cloudzero.py@@ -1,3 +1,4 @@+import hashlibfrom datetime import datetimefrom unittest.mock import AsyncMock, MagicMock, patch@@ -165,3 +166,26 @@ class TestCloudZeroHourlyExport:logger = CloudZeroLogger(api_key="test", connection_id="test")await logger._hourly_usage_data_export()+++class TestLiteLLMDatabaseUsageData:+ @pytest.mark.asyncio+ async def test_builds_frame_from_rows_recovered_for_double_hashed_keys(self, monkeypatch: pytest.MonkeyPatch):+ double_hashed = hashlib.sha256(b"sk-hashed-token").hexdigest()+ joined_row = {"api_key": "sk-joined", "api_key_alias": "joined", "team_id": "team-0", "user_email": None, "spend": 0.1}+ dirty_row = {"api_key": double_hashed, "api_key_alias": None, "team_id": None, "user_email": None, "spend": 0.5}++ async def query_raw(query: str, *params):+ if "sha256(" in query:+ return [{"digest": double_hashed, "key_alias": "batch-worker", "team_id": "team-1", "user_id": None}]+ return [joined_row, dirty_row]++ fake_client = MagicMock()+ fake_client.db.query_raw = AsyncMock(side_effect=query_raw)+ db = LiteLLMDatabase()+ monkeypatch.setattr(db, "_ensure_prisma_client", lambda: fake_client)++ result = await db.get_usage_data()++ assert result["api_key_alias"].to_list() == ["joined", "batch-worker"]+ assert result["team_id"].to_list() == ["team-0", "team-1"]diff --git a/tests/test_litellm/integrations/focus/test_focus_database.py b/tests/test_litellm/integrations/focus/test_focus_database.pyindex 5c13665f1f1..06240eac387 100644--- a/tests/test_litellm/integrations/focus/test_focus_database.py+++ b/tests/test_litellm/integrations/focus/test_focus_database.py@@ -1,5 +1,6 @@"""Tests for FocusLiteLLMDatabase query construction."""+import hashlibfrom datetime import datetime, timezonefrom types import SimpleNamespacefrom unittest.mock import AsyncMock@@ -87,3 +88,24 @@ async def test_should_join_organization_table(monkeypatch: pytest.MonkeyPatch):)assert "ot.organization_alias as organization_alias" in query_textassert 'LEFT JOIN "LiteLLM_OrganizationTable" ot' in query_text+++async def test_should_build_frame_from_rows_recovered_for_double_hashed_keys(monkeypatch: pytest.MonkeyPatch):+ double_hashed = hashlib.sha256(b"sk-hashed-token").hexdigest()+ joined_row = {"api_key": "sk-joined", "api_key_alias": "joined", "team_id": "team-0", "user_email": None, "spend": 0.1}+ dirty_row = {"api_key": double_hashed, "api_key_alias": None, "team_id": None, "user_email": None, "spend": 0.5}++ async def query_raw(query: str, *params):+ if "sha256(" in query:+ return [{"digest": double_hashed, "key_alias": "batch-worker", "team_id": "team-1", "user_id": None}]+ return [joined_row, dirty_row]++ mock_client = SimpleNamespace(db=SimpleNamespace(query_raw=AsyncMock(side_effect=query_raw)))+ db = FocusLiteLLMDatabase()+ monkeypatch.setattr(db, "_ensure_prisma_client", lambda: mock_client)++ result = await db.get_usage_data()++ assert result["api_key_alias"].to_list() == ["joined", "batch-worker"]+ assert result["team_id"].to_list() == ["team-0", "team-1"]