From 968d7a3eca3bbc663bc1b7b69a79a2996af81e96 Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Sun, 15 Mar 2026 13:28:14 -0700 Subject: [PATCH] Fix test isolation: save/restore pre_call_rules and post_call_rules test_post_call_rule_streaming in test_rules.py sets litellm.post_call_rules but never cleans up. Since pytest_collection_modifyitems sorts tests by name across modules, the leaked rule causes failures in test_streaming.py, test_register_model.py, and test_sagemaker.py. Add pre_call_rules and post_call_rules to the isolate_litellm_state fixture's save/restore and clear lists. Co-Authored-By: Claude Opus 4.6 --- tests/local_testing/conftest.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/local_testing/conftest.py b/tests/local_testing/conftest.py index 6c5c0d3b35..71cfec4157 100644 --- a/tests/local_testing/conftest.py +++ b/tests/local_testing/conftest.py @@ -43,6 +43,12 @@ def isolate_litellm_state(): if hasattr(litellm, attr): original_state[attr] = getattr(litellm, attr) + # Save rules that tests may set (e.g. test_rules.py) + for attr in ("pre_call_rules", "post_call_rules"): + if hasattr(litellm, attr): + val = getattr(litellm, attr) + original_state[attr] = val.copy() if val else [] + # Save transport/network globals for attr in ("disable_aiohttp_transport", "force_ipv4"): if hasattr(litellm, attr): @@ -52,13 +58,15 @@ def isolate_litellm_state(): if hasattr(litellm, "in_memory_llm_clients_cache"): litellm.in_memory_llm_clients_cache.flush_cache() - # Clear callbacks before test + # Clear callbacks and rules before test for attr in ( "callbacks", "success_callback", "failure_callback", "_async_success_callback", "_async_failure_callback", + "pre_call_rules", + "post_call_rules", ): if hasattr(litellm, attr): setattr(litellm, attr, [])