From a885e215432d94fe2f9443c871f96c4565aed509 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Sat, 6 Dec 2025 05:56:24 -0800 Subject: [PATCH] Fix Langfuse logger test mock setup (#17588) * Fix test_log_langfuse_v2_handles_null_usage_values test failure The test was failing because the logger's Langfuse client wasn't properly mocked. Even though sys.modules was mocked, the logger's __init__ method creates its own Langfuse client instance that wasn't using the test's mock. Changes: - Explicitly set logger.Langfuse to the mock client after initialization - Set logger.langfuse_sdk_version to ensure _supports_* methods work correctly - Added mock_langfuse_client.client attribute to prevent errors during init - Added trace_id to mock_langfuse_generation for proper return value handling - Removed redundant mock setup code This ensures the test can properly verify that _log_langfuse_v2 correctly converts None usage values to 0 by allowing the mock's generation method to be called and asserted. Fixes: AssertionError: Expected 'generation' to have been called once. Called 0 times. --- .../integrations/test_langfuse.py | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/test_litellm/integrations/test_langfuse.py b/tests/test_litellm/integrations/test_langfuse.py index b9009d20056..5610ac8fff6 100644 --- a/tests/test_litellm/integrations/test_langfuse.py +++ b/tests/test_litellm/integrations/test_langfuse.py @@ -35,8 +35,11 @@ class TestLangfuseUsageDetails(unittest.TestCase): # Create mock objects self.mock_langfuse_client = MagicMock() + # Mock the client attribute to prevent errors during logger initialization + self.mock_langfuse_client.client = MagicMock() self.mock_langfuse_trace = MagicMock() self.mock_langfuse_generation = MagicMock() + self.mock_langfuse_generation.trace_id = "test-trace-id" # Setup the trace and generation chain self.mock_langfuse_trace.generation.return_value = self.mock_langfuse_generation @@ -63,22 +66,13 @@ class TestLangfuseUsageDetails(unittest.TestCase): sys.modules["langfuse"] = self.mock_langfuse sys.modules["langfuse"].Langfuse = self.mock_langfuse_class - # Mock the Langfuse client - self.mock_langfuse_client = MagicMock() - self.mock_langfuse_trace = MagicMock() - self.mock_langfuse_generation = MagicMock() - - # Setup the trace and generation chain - self.mock_langfuse_trace.generation.return_value = self.mock_langfuse_generation - self.mock_langfuse_client.trace.return_value = self.mock_langfuse_trace - - # Mock the Langfuse class - self.mock_langfuse_class = MagicMock() - self.mock_langfuse_class.return_value = self.mock_langfuse_client - self.mock_langfuse.Langfuse = self.mock_langfuse_class - # Create the logger self.logger = LangFuseLogger() + + # Explicitly set the Langfuse client to our mock + self.logger.Langfuse = self.mock_langfuse_client + # Ensure langfuse_sdk_version is set correctly for _supports_* methods + self.logger.langfuse_sdk_version = "3.0.0" # Add the log_event_on_langfuse method to the instance def log_event_on_langfuse(