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 <noreply@anthropic.com>
This commit is contained in:
Julio Quinteros Pro
2026-02-17 21:29:13 -03:00
co-authored by Claude Opus 4.5
parent 77f315eb11
commit e6abb865d3
@@ -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()