diff --git a/dspy/clients/__init__.py b/dspy/clients/__init__.py index 735691e9ce..496aa48eb7 100644 --- a/dspy/clients/__init__.py +++ b/dspy/clients/__init__.py @@ -3,9 +3,43 @@ from pathlib import Path from typing import Optional +# Set environment variables before importing litellm +os.environ["LITELLM_LOG"] = "ERROR" +os.environ["OPENAI_LOG"] = "ERROR" + import litellm from litellm.caching.caching import Cache as LitellmCache +def _configure_litellm_logging(level: str = "ERROR"): + """Configure LiteLLM logging to the specified level.""" + # Update environment variables + os.environ["LITELLM_LOG"] = level + os.environ["OPENAI_LOG"] = level + + # Cover both capitalization variants used by LiteLLM + logger_names = [ + "LiteLLM", + "LiteLLM.utils", + "LiteLLM.proxy.utils", + "litellm", + "litellm.utils", + "litellm.proxy.utils", + ] + _level = getattr(logging, level) + for logger_name in logger_names: + lg = logging.getLogger(logger_name) + lg.setLevel(_level) + lg.propagate = False + # Remove all existing handlers or force them to the desired level + for h in lg.handlers[:]: + h.setLevel(_level) + # Ensure there is at least a NullHandler to swallow logs + if not lg.handlers: + lg.addHandler(logging.NullHandler()) + +# Immediately disable LiteLLM logging after import +_configure_litellm_logging("ERROR") + from dspy.clients.base_lm import BaseLM, inspect_history from dspy.clients.cache import Cache from dspy.clients.embedding import Embedder @@ -86,9 +120,6 @@ def configure_cache( memory_max_entries=1000000, ) -# Turn off by default to avoid LiteLLM logging during every LM call. -litellm.suppress_debug_info = True - if "LITELLM_LOCAL_MODEL_COST_MAP" not in os.environ: # Accessed at run time by litellm; i.e., fine to keep after import os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True" @@ -96,10 +127,17 @@ def configure_cache( def enable_litellm_logging(): litellm.suppress_debug_info = False + _configure_litellm_logging("INFO") + # Remove environment variables to allow logging + if "LITELLM_LOG" in os.environ: + del os.environ["LITELLM_LOG"] + if "OPENAI_LOG" in os.environ: + del os.environ["OPENAI_LOG"] def disable_litellm_logging(): litellm.suppress_debug_info = True + _configure_litellm_logging("ERROR") __all__ = [