Skip to content

Fix LiteLLM Logging #8615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions dspy/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,20 +120,24 @@ 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"


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__ = [
Expand Down
Loading