From e6abb865d3e2e2e989692d95b9e148e55f6ab60e Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Fri, 6 Feb 2026 16:34:36 -0300 Subject: [PATCH] fix: properly reload litellm in setup_and_teardown fixture Use importlib.import_module + reload uniformly in both code paths to ensure fresh module state regardless of whether litellm was previously in sys.modules. This fixes the inconsistency where the "not in sys.modules" branch didn't reload the module. Co-Authored-By: Claude Opus 4.5 --- .../proxy/guardrails/test_pillar_guardrails.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/test_litellm/proxy/guardrails/test_pillar_guardrails.py b/tests/test_litellm/proxy/guardrails/test_pillar_guardrails.py index cd7e726bdd..c33203c0c1 100644 --- a/tests/test_litellm/proxy/guardrails/test_pillar_guardrails.py +++ b/tests/test_litellm/proxy/guardrails/test_pillar_guardrails.py @@ -6,6 +6,7 @@ and following LiteLLM testing patterns and best practices. """ # Standard library imports +import importlib import os import sys from typing import Dict @@ -49,18 +50,14 @@ def setup_and_teardown(): to speed up testing by removing callbacks being chained. """ import asyncio - import importlib - import sys global litellm - # Reload litellm to ensure clean state - # During parallel test execution, another worker might have removed litellm from sys.modules - # so we need to ensure it's imported before reloading - if "litellm" not in sys.modules: - import litellm as fresh_litellm - litellm = fresh_litellm # Update module-level reference - else: - litellm = importlib.reload(litellm) # Update module-level reference with reloaded module + # Always import then reload to ensure fresh state + # This handles both cases uniformly: + # 1. litellm not in sys.modules (parallel worker removed it) + # 2. litellm already imported (normal case) + _module = importlib.import_module("litellm") + litellm = importlib.reload(_module) # Set up async loop loop = asyncio.get_event_loop_policy().new_event_loop()