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
Merge remote-tracking branch 'origin' into litellm_tag_spend_dedupe
yuneng-jiang68419cfe4e2f
diff --cc tests/test_litellm/proxy/db/test_db_spend_update_writer.pyindex 8a5860165a0,db6c318357c..03c13750b60--- a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py+++ b/tests/test_litellm/proxy/db/test_db_spend_update_writer.py@@@ -223,59 -223,107 +223,162 @@@ async def test_update_daily_spend_sortimock_table.upsert.assert_has_calls(upsert_calls)+async def test_update_daily_spend_tag_with_request_id():+ """+ Test that request_id is included in update_data when updating tag transactions.+ """+ # Setup+ mock_prisma_client = MagicMock()+ mock_batcher = MagicMock()+ mock_table = MagicMock()+ mock_prisma_client.db.batch_.return_value.__aenter__.return_value = mock_batcher+ mock_batcher.litellm_dailytagspend = mock_table++ # Create a transaction with request_id+ daily_spend_transactions = {+ "test_key": {+ "tag": "prod-tag",+ "date": "2024-01-01",+ "api_key": "test-api-key",+ "model": "gpt-4",+ "custom_llm_provider": "openai",+ "mcp_namespaced_tool_name": "",+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "spend": 0.1,+ "api_requests": 1,+ "successful_requests": 1,+ "failed_requests": 0,+ "request_id": "test-request-id-123",+ }+ }++ # Call the method+ await DBSpendUpdateWriter._update_daily_spend(+ n_retry_times=1,+ prisma_client=mock_prisma_client,+ proxy_logging_obj=MagicMock(),+ daily_spend_transactions=daily_spend_transactions,+ entity_type="tag",+ entity_id_field="tag",+ table_name="litellm_dailytagspend",+ unique_constraint_name="tag_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name",+ )++ # Verify that table.upsert was called+ mock_table.upsert.assert_called_once()++ # Verify request_id is in update_data+ call_args = mock_table.upsert.call_args[1]+ update_data = call_args["data"]["update"]+ assert "request_id" in update_data+ assert update_data["request_id"] == "test-request-id-123"+++++++ @pytest.mark.asyncio+ async def test_update_daily_spend_with_none_values_in_sorting_fields():+ """+ Test that _update_daily_spend handles None values in sorting fields correctly.++ This test ensures that when fields like date, api_key, model, or custom_llm_provider+ are None, the sorting doesn't crash with TypeError: '<' not supported between+ instances of 'NoneType' and 'str'.+ """+ # Setup+ mock_prisma_client = MagicMock()+ mock_batcher = MagicMock()+ mock_table = MagicMock()+ mock_prisma_client.db.batch_.return_value.__aenter__.return_value = mock_batcher+ mock_batcher.litellm_dailyuserspend = mock_table++ # Create transactions with None values in various sorting fields+ daily_spend_transactions = {+ "key1": {+ "user_id": "user1",+ "date": None, # None date+ "api_key": "test-api-key",+ "model": "gpt-4",+ "custom_llm_provider": "openai",+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "spend": 0.1,+ "api_requests": 1,+ "successful_requests": 1,+ "failed_requests": 0,+ },+ "key2": {+ "user_id": "user2",+ "date": "2024-01-01",+ "api_key": None, # None api_key+ "model": "gpt-4",+ "custom_llm_provider": "openai",+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "spend": 0.1,+ "api_requests": 1,+ "successful_requests": 1,+ "failed_requests": 0,+ },+ "key3": {+ "user_id": "user3",+ "date": "2024-01-01",+ "api_key": "test-api-key",+ "model": None, # None model+ "custom_llm_provider": "openai",+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "spend": 0.1,+ "api_requests": 1,+ "successful_requests": 1,+ "failed_requests": 0,+ },+ "key4": {+ "user_id": "user4",+ "date": "2024-01-01",+ "api_key": "test-api-key",+ "model": "gpt-4",+ "custom_llm_provider": None, # None custom_llm_provider+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "spend": 0.1,+ "api_requests": 1,+ "successful_requests": 1,+ "failed_requests": 0,+ },+ "key5": {+ "user_id": None, # None entity_id+ "date": "2024-01-01",+ "api_key": "test-api-key",+ "model": "gpt-4",+ "custom_llm_provider": "openai",+ "prompt_tokens": 10,+ "completion_tokens": 20,+ "spend": 0.1,+ "api_requests": 1,+ "successful_requests": 1,+ "failed_requests": 0,+ },+ }++ # Call the method - this should not raise TypeError+ await DBSpendUpdateWriter._update_daily_spend(+ n_retry_times=1,+ prisma_client=mock_prisma_client,+ proxy_logging_obj=MagicMock(),+ daily_spend_transactions=daily_spend_transactions,+ entity_type="user",+ entity_id_field="user_id",+ table_name="litellm_dailyuserspend",+ unique_constraint_name="user_id_date_api_key_model_custom_llm_provider",+ )++ # Verify that table.upsert was called (should be called 5 times, once for each transaction)+ assert mock_table.upsert.call_count == 5++# Tag Spend Tracking Tests