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: improve Langfuse test isolation to prevent flaky failures (#21093)

jquinter43f9a588d9b2
diff --git a/tests/test_litellm/integrations/test_langfuse.py b/tests/test_litellm/integrations/test_langfuse.py
index cd3d5b9ebe3..010d9f863c2 100644
--- a/tests/test_litellm/integrations/test_langfuse.py
+++ b/tests/test_litellm/integrations/test_langfuse.py
@@ -268,22 +268,21 @@ class TestLangfuseUsageDetails(unittest.TestCase):
Test that _log_langfuse_v2 correctly handles None values in the usage object
by converting them to 0, preventing validation errors.
"""
- # Create fresh mocks for this test to avoid state pollution from setUp's side_effect
- # The setUp configures trace.side_effect which can interfere with return_value
- mock_trace = MagicMock()
- mock_generation = MagicMock()
- mock_generation.trace_id = "test-trace-id"
+ # Reset the mock to ensure clean state
+ self.mock_langfuse_client.reset_mock()
+ self.mock_langfuse_trace.reset_mock()
+ self.mock_langfuse_generation.reset_mock()
+
+ # Re-setup the trace and generation chain with clean state
+ self.mock_langfuse_generation.trace_id = "test-trace-id"
mock_span = MagicMock()
mock_span.end = MagicMock()
-
- mock_trace.generation.return_value = mock_generation
- mock_trace.span.return_value = mock_span
-
- mock_client = MagicMock()
- mock_client.trace.return_value = mock_trace
-
- # Use our fresh mock client
- self.logger.Langfuse = mock_client
+ self.mock_langfuse_trace.span.return_value = mock_span
+ self.mock_langfuse_trace.generation.return_value = self.mock_langfuse_generation
+
+ # Ensure trace returns our mock
+ self.mock_langfuse_client.trace.return_value = self.mock_langfuse_trace
+ self.logger.Langfuse = self.mock_langfuse_client
with patch(
"litellm.integrations.langfuse.langfuse._add_prompt_to_generation_params",
@@ -337,13 +336,13 @@ class TestLangfuseUsageDetails(unittest.TestCase):
)
except Exception as e:
self.fail(f"_log_langfuse_v2 raised an exception: {e}")
-
+
# Verify that trace was called first
- mock_client.trace.assert_called()
-
+ self.mock_langfuse_client.trace.assert_called()
+
# Check the arguments passed to the mocked langfuse generation call
- mock_trace.generation.assert_called_once()
- call_args, call_kwargs = mock_trace.generation.call_args
+ self.mock_langfuse_trace.generation.assert_called_once()
+ call_args, call_kwargs = self.mock_langfuse_trace.generation.call_args
# Inspect the usage and usage_details dictionaries
usage_arg = call_kwargs.get("usage")