From 679a0293bd9a7f1d3648ac08c582d038400ad92d Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Sun, 15 Feb 2026 14:11:24 -0300 Subject: [PATCH] fix(test): restore Langfuse client counter in test cleanup Fixes persistent test isolation issue in TestLangfuseUsageDetails by saving and restoring the global litellm.initialized_langfuse_clients counter. Changes: - Save litellm.initialized_langfuse_clients in setUp - Restore original value in tearDown - Prevents counter accumulation across tests Root Cause: PR #21248 added logger cleanup but missed the global client counter. Each test increments litellm.initialized_langfuse_clients when creating a LangFuseLogger, but the counter was never reset. This caused state accumulation that could affect test behavior when tests run in certain orders, leading to "Expected 'generation' to have been called once. Called 0 times" failures. Impact: - test_log_langfuse_v2_handles_null_usage_values was still flaky - Counter would accumulate: 1, 2, 3... across all tests - While unlikely to hit MAX (50), accumulated state affected behavior This completes the test isolation fix started in PR #21248. Related: #21248 Fixes: Remaining test isolation issues in Langfuse tests Co-Authored-By: Claude Sonnet 4.5 --- tests/test_litellm/integrations/test_langfuse.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_litellm/integrations/test_langfuse.py b/tests/test_litellm/integrations/test_langfuse.py index 15f252a4afc..cd3d5b9ebe3 100644 --- a/tests/test_litellm/integrations/test_langfuse.py +++ b/tests/test_litellm/integrations/test_langfuse.py @@ -21,6 +21,9 @@ from litellm.types.integrations.langfuse import * class TestLangfuseUsageDetails(unittest.TestCase): def setUp(self): + # Save global Langfuse client counter to restore after test + self._original_langfuse_clients_count = litellm.initialized_langfuse_clients + # Set up environment variables for testing self.env_patcher = patch.dict( "os.environ", @@ -130,6 +133,9 @@ class TestLangfuseUsageDetails(unittest.TestCase): # Delete logger instance to ensure complete cleanup del self.logger + # Restore global Langfuse client counter to prevent cross-test pollution + litellm.initialized_langfuse_clients = self._original_langfuse_clients_count + self.env_patcher.stop() self.langfuse_module_patcher.stop() # patch.dict automatically restores sys.modules