From 703f02bf99b770c29fefc92f6757f595a2320ba2 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Tue, 17 Feb 2026 19:18:12 -0300 Subject: [PATCH] fix(test): prevent flaky failure in test_log_langfuse_v2_handles_null_usage_values This test has failed repeatedly in CI with: 'Expected _add_prompt_to_generation_params to have been called once. Called 0 times.' Root cause: _add_prompt_to_generation_params is only called when _supports_prompt() returns True. Under cross-test state contamination in CI (parallel workers), langfuse_sdk_version can be in an unexpected state, causing _supports_prompt() to return False and silently skip the call (exception swallowed by the outer try/except). Fixes: - Use reset_mock(side_effect=True) so setUp's trace side_effect is cleared and the explicit return_value assignment actually takes effect - Patch _supports_prompt on the logger instance to always return True, making the _add_prompt_to_generation_params assertion independent of SDK version state Co-Authored-By: Claude Sonnet 4.6 --- tests/test_litellm/integrations/test_langfuse.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/test_litellm/integrations/test_langfuse.py b/tests/test_litellm/integrations/test_langfuse.py index 010d9f863c..8da7dd8917 100644 --- a/tests/test_litellm/integrations/test_langfuse.py +++ b/tests/test_litellm/integrations/test_langfuse.py @@ -268,10 +268,10 @@ 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. """ - # 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() + # Reset the mock to ensure clean state; clear side_effect so return_value takes effect + self.mock_langfuse_client.reset_mock(side_effect=True) + self.mock_langfuse_trace.reset_mock(side_effect=True) + self.mock_langfuse_generation.reset_mock(side_effect=True) # Re-setup the trace and generation chain with clean state self.mock_langfuse_generation.trace_id = "test-trace-id" @@ -283,12 +283,14 @@ class TestLangfuseUsageDetails(unittest.TestCase): # 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", side_effect=lambda generation_params, **kwargs: generation_params, create=True, - ) as mock_add_prompt_params: + ) as mock_add_prompt_params, patch.object( + self.logger, "_supports_prompt", return_value=True + ): # Create a mock response object with usage information containing None values response_obj = MagicMock() response_obj.usage = MagicMock()