From dc003e2a94c783fe0c9685110ba831637a212d61 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Tue, 17 Feb 2026 22:33:06 -0300 Subject: [PATCH 1/2] fix: prevent sys.modules["langfuse"] import failures in langfuse unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three test failures caused by the real langfuse SDK import being triggered at test time: 1. test_langfuse_prompt_management.py: Both tests create LangfusePromptManagement() which calls `import langfuse`. Since earlier TestLangfuseUsageDetails tests remove sys.modules["langfuse"] via patch.dict teardown, the real langfuse import runs and fails on Python 3.14 (pydantic v1 incompatibility). Fix: add setup_method/teardown_method to mock sys.modules["langfuse"]. 2. test_langfuse.py::test_max_langfuse_clients_limit: Same root cause — creates LangFuseLogger() without mocking sys.modules["langfuse"]. Fix: wrap test body with patch.dict("sys.modules", {"langfuse": mock}). 3. test_langfuse_otel.py::test_extract_langfuse_metadata_with_header_enrichment: Replaces sys.modules["litellm.integrations.langfuse.langfuse"] with a stub without restoring it, causing patch() in later tests to target the stub instead of the real module. Fix: use monkeypatch.setitem() which auto-restores after the test. Co-Authored-By: Claude Sonnet 4.6 --- .../langfuse/test_langfuse_prompt_management.py | 16 +++++++++++++++- tests/test_litellm/integrations/test_langfuse.py | 9 ++++++++- .../integrations/test_langfuse_otel.py | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tests/test_litellm/integrations/langfuse/test_langfuse_prompt_management.py b/tests/test_litellm/integrations/langfuse/test_langfuse_prompt_management.py index 5389cdf7377..c557bdb67ee 100644 --- a/tests/test_litellm/integrations/langfuse/test_langfuse_prompt_management.py +++ b/tests/test_litellm/integrations/langfuse/test_langfuse_prompt_management.py @@ -1,5 +1,5 @@ import os -from unittest.mock import patch +from unittest.mock import MagicMock, patch from litellm.integrations.langfuse.langfuse_prompt_management import ( LangfusePromptManagement, @@ -7,6 +7,20 @@ from litellm.integrations.langfuse.langfuse_prompt_management import ( class TestLangfusePromptManagement: + def setup_method(self): + # Mock langfuse package to avoid triggering real import. + # The real langfuse import fails on Python 3.14 due to pydantic v1 incompatibility. + # This also prevents test-ordering issues when earlier tests remove sys.modules["langfuse"]. + self._mock_langfuse = MagicMock() + self._mock_langfuse.version.__version__ = "3.0.0" + self._langfuse_patcher = patch.dict( + "sys.modules", {"langfuse": self._mock_langfuse} + ) + self._langfuse_patcher.start() + + def teardown_method(self): + self._langfuse_patcher.stop() + def test_get_prompt_from_id(self): langfuse_prompt_management = LangfusePromptManagement() with patch.object( diff --git a/tests/test_litellm/integrations/test_langfuse.py b/tests/test_litellm/integrations/test_langfuse.py index 8da7dd8917b..77ff5dbb97e 100644 --- a/tests/test_litellm/integrations/test_langfuse.py +++ b/tests/test_litellm/integrations/test_langfuse.py @@ -472,8 +472,15 @@ def test_max_langfuse_clients_limit(): """ Test that the max langfuse clients limit is respected when initializing multiple clients """ + # Mock langfuse package to avoid triggering real import. + # The real langfuse import fails on Python 3.14 due to pydantic v1 incompatibility, + # and sys.modules["langfuse"] may be absent after other tests in the suite clean up. + mock_langfuse = MagicMock() + mock_langfuse.version.__version__ = "3.0.0" # Set max clients to 2 for testing - with patch.object(langfuse_module, "MAX_LANGFUSE_INITIALIZED_CLIENTS", 2): + with patch.dict("sys.modules", {"langfuse": mock_langfuse}), patch.object( + langfuse_module, "MAX_LANGFUSE_INITIALIZED_CLIENTS", 2 + ): # Reset the counter litellm.initialized_langfuse_clients = 0 diff --git a/tests/test_litellm/integrations/test_langfuse_otel.py b/tests/test_litellm/integrations/test_langfuse_otel.py index 62851b8f99f..44853d9dce5 100644 --- a/tests/test_litellm/integrations/test_langfuse_otel.py +++ b/tests/test_litellm/integrations/test_langfuse_otel.py @@ -161,7 +161,7 @@ class TestLangfuseOtelIntegration: # Use monkeypatch so the real module is restored after the test runs, # preventing sys.modules corruption that would break patch() targets in # later tests (the patch would hit the stub while the real module's - # globals remain unpatch-ed). + # globals remain unpatched). monkeypatch.setitem(sys.modules, "litellm.integrations.langfuse.langfuse", stub_module) # type: ignore kwargs = {"litellm_params": {"metadata": {"foo": "bar"}}} From 5fe1dd3ec87ca54575ed83fc15fefe5e5d4d4f97 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Wed, 18 Feb 2026 19:09:35 -0300 Subject: [PATCH 2/2] fix(tests): restore initialized_langfuse_clients counter after test Save and restore litellm.initialized_langfuse_clients around test_max_langfuse_clients_limit to prevent ordering-dependent failures in other tests that rely on the counter's value. Co-Authored-By: Claude Sonnet 4.6 --- tests/test_litellm/integrations/test_langfuse.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_litellm/integrations/test_langfuse.py b/tests/test_litellm/integrations/test_langfuse.py index 77ff5dbb97e..10d3323a255 100644 --- a/tests/test_litellm/integrations/test_langfuse.py +++ b/tests/test_litellm/integrations/test_langfuse.py @@ -478,6 +478,7 @@ def test_max_langfuse_clients_limit(): mock_langfuse = MagicMock() mock_langfuse.version.__version__ = "3.0.0" # Set max clients to 2 for testing + original_initialized_langfuse_clients = litellm.initialized_langfuse_clients with patch.dict("sys.modules", {"langfuse": mock_langfuse}), patch.object( langfuse_module, "MAX_LANGFUSE_INITIALIZED_CLIENTS", 2 ): @@ -513,3 +514,5 @@ def test_max_langfuse_clients_limit(): # Counter should still be 2 (third client failed to initialize) assert litellm.initialized_langfuse_clients == 2 + + litellm.initialized_langfuse_clients = original_initialized_langfuse_clients