From 32922449a3f61032538c5e00f92b070a613ab906 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Tue, 17 Feb 2026 22:01:21 -0300 Subject: [PATCH] fix: restore sys.modules after stub injection in langfuse otel test test_extract_langfuse_metadata_with_header_enrichment replaced sys.modules["litellm.integrations.langfuse.langfuse"] with a stub module but never restored it. This caused subsequent tests using patch("litellm.integrations.langfuse.langfuse._add_prompt_to_generation_params") to patch the stub instead of the real module, while _log_langfuse_v2 executed from the real module's globals (unpatched), triggering ModuleNotFoundError and assertion failures. Fix: use monkeypatch.setitem() so pytest automatically restores the original module after the test completes. Co-Authored-By: Claude Sonnet 4.6 --- tests/test_litellm/integrations/test_langfuse_otel.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_litellm/integrations/test_langfuse_otel.py b/tests/test_litellm/integrations/test_langfuse_otel.py index ba4a096be2..62851b8f99 100644 --- a/tests/test_litellm/integrations/test_langfuse_otel.py +++ b/tests/test_litellm/integrations/test_langfuse_otel.py @@ -157,8 +157,12 @@ class TestLangfuseOtelIntegration: stub_module.LangFuseLogger = StubLFLogger # type: ignore - # Register stub in sys.modules so import inside method succeeds - sys.modules["litellm.integrations.langfuse.langfuse"] = stub_module # type: ignore + # Register stub in sys.modules so import inside method succeeds. + # 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). + monkeypatch.setitem(sys.modules, "litellm.integrations.langfuse.langfuse", stub_module) # type: ignore kwargs = {"litellm_params": {"metadata": {"foo": "bar"}}} extracted = LangfuseOtelLogger._extract_langfuse_metadata(kwargs)