From 13a46598e71fac39cd71f3e34c80c444de634c2f Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Sun, 15 Mar 2026 18:48:16 -0700 Subject: [PATCH] Fix logging_testing: clear _in_memory_loggers and add missing globals - Clear _in_memory_loggers before/after each test to prevent cached logger instances (LangsmithLogger, SlackAlerting, etc.) from leaking stale state - Add pre_call_rules, post_call_rules to list attrs save/restore - Add vector_store_registry to scalar attrs save/restore Co-Authored-By: Claude Opus 4.6 --- tests/logging_callback_tests/conftest.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/logging_callback_tests/conftest.py b/tests/logging_callback_tests/conftest.py index 67874083b1..79784cf9b4 100644 --- a/tests/logging_callback_tests/conftest.py +++ b/tests/logging_callback_tests/conftest.py @@ -1,9 +1,9 @@ # conftest.py # # xdist-compatible test isolation for logging callback tests. -# Pattern matches tests/guardrails_tests/conftest.py: # - Function-scoped fixture saves/restores litellm globals (no reload) # - Module-scoped fixture reloads only in single-process mode +# - Clears _in_memory_loggers to prevent cached logger instance leaks import importlib import os @@ -24,6 +24,8 @@ _LIST_ATTRS = ( "_async_success_callback", "_async_failure_callback", "service_callback", + "pre_call_rules", + "post_call_rules", ) _SCALAR_ATTRS = ( @@ -35,6 +37,7 @@ _SCALAR_ATTRS = ( "redact_user_api_key_info", "s3_callback_params", "datadog_params", + "vector_store_registry", ) @@ -46,6 +49,8 @@ def isolate_litellm_state(): Saves and restores litellm callback/global state so tests don't leak side effects. Works safely under pytest-xdist parallel execution. """ + from litellm.litellm_core_utils import litellm_logging as ll_logging + original_state = {} # Save list-type attrs (callbacks) @@ -59,10 +64,13 @@ def isolate_litellm_state(): if hasattr(litellm, attr): original_state[attr] = getattr(litellm, attr) - # Flush cache before test + # Flush cache and clear internal logger instances before test if hasattr(litellm, "in_memory_llm_clients_cache"): litellm.in_memory_llm_clients_cache.flush_cache() + # Clear cached logger instances (LangsmithLogger, SlackAlerting, etc.) + ll_logging._in_memory_loggers.clear() + # Clear callbacks before test for attr in _LIST_ATTRS: if hasattr(litellm, attr): @@ -74,6 +82,8 @@ def isolate_litellm_state(): if hasattr(litellm, "in_memory_llm_clients_cache"): litellm.in_memory_llm_clients_cache.flush_cache() + ll_logging._in_memory_loggers.clear() + for attr, original_value in original_state.items(): if hasattr(litellm, attr): setattr(litellm, attr, original_value)