Skip to content

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]

[Fix] should_create_missing_views returns False for reltuples=0 (falsy zero bug)

Yuneng Jiang9c0b73e5f41f
diff --git a/litellm/proxy/db/create_views.py b/litellm/proxy/db/create_views.py
index 3598045545b..d84cebcf05a 100644
--- a/litellm/proxy/db/create_views.py
+++ b/litellm/proxy/db/create_views.py
@@ -251,7 +251,7 @@ async def should_create_missing_views(db: _db) -> bool:
and len(result) > 0
and isinstance(result[0], dict)
and "reltuples" in result[0]
- and result[0]["reltuples"]
+ and result[0]["reltuples"] is not None
and (result[0]["reltuples"] == 0 or result[0]["reltuples"] == -1)
):
verbose_logger.debug("Should create views")
diff --git a/tests/pass_through_tests/test_vertex_ai.py b/tests/pass_through_tests/test_vertex_ai.py
index ba27a4cc460..73bf03c5000 100644
--- a/tests/pass_through_tests/test_vertex_ai.py
+++ b/tests/pass_through_tests/test_vertex_ai.py
@@ -72,6 +72,10 @@ async def call_spend_logs_endpoint():
response = requests.get(url, headers=headers)
print("response from call_spend_logs_endpoint", response)
+ if response.status_code != 200:
+ print(f"spend logs endpoint returned {response.status_code}: {response.text}")
+ return None
+
json_response = response.json()
# get spend for today
diff --git a/tests/test_litellm/proxy/db/test_create_views.py b/tests/test_litellm/proxy/db/test_create_views.py
index 1a90b4c204d..c0c09d0137b 100644
--- a/tests/test_litellm/proxy/db/test_create_views.py
+++ b/tests/test_litellm/proxy/db/test_create_views.py
@@ -130,6 +130,42 @@ async def test_create_views_reraises_undefined_function_error():
mock_db.execute_raw.assert_not_called()
+async def test_should_create_missing_views_reltuples_zero():
+ """should return True when reltuples is 0 (fresh empty table)."""
+ from litellm.proxy.db.create_views import should_create_missing_views
+
+ mock_db = MagicMock()
+ mock_db.query_raw = AsyncMock(return_value=[{"reltuples": 0}])
+
+ result = await should_create_missing_views(mock_db)
+ assert result is True
+
+
+async def test_should_create_missing_views_reltuples_negative_one():
+ """should return True when reltuples is -1 (table created, no ANALYZE yet)."""
+ from litellm.proxy.db.create_views import should_create_missing_views
+
+ mock_db = MagicMock()
+ mock_db.query_raw = AsyncMock(return_value=[{"reltuples": -1}])
+
+ result = await should_create_missing_views(mock_db)
+ assert result is True
+
+
+async def test_should_create_missing_views_reltuples_positive():
+ """should return False when reltuples > 0 (table has data)."""
+ from litellm.proxy.db.create_views import should_create_missing_views
+
+ mock_db = MagicMock()
+ mock_db.query_raw = AsyncMock(return_value=[{"reltuples": 1000}])
+
+ result = await should_create_missing_views(mock_db)
+ assert result is False
+
+
@pytest.mark.asyncio
async def test_create_views_creates_view_on_undefined_table_error():
"""should treat 'undefined table' as a missing-view signal and attempt creation."""