From de69ea26e8aa4b7083576d2a714aeb65dbad3a02 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Fri, 29 Sep 2023 11:44:30 -0400 Subject: [PATCH] Suppress warnings in interactive env that stem from tab completion (#11190) Suppress warnings in interactive environments that can arise from users relying on tab completion (without even using deprecated modules). jupyter seems to filter warnings by default (at least for me), but ipython surfaces them all --- libs/langchain/langchain/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libs/langchain/langchain/__init__.py b/libs/langchain/langchain/__init__.py index ea8ef56f03a..c22d789caeb 100644 --- a/libs/langchain/langchain/__init__.py +++ b/libs/langchain/langchain/__init__.py @@ -20,7 +20,21 @@ debug: bool = False llm_cache: Optional["BaseCache"] = None +def _is_interactive_env() -> bool: + """Determine if running within IPython or Jupyter.""" + import sys + + return hasattr(sys, "ps2") + + def _warn_on_import(name: str) -> None: + """Warn on import of deprecated module.""" + if _is_interactive_env(): + # No warnings for interactive environments. + # This is done to avoid polluting the output of interactive environments + # where users rely on auto-complete and may trigger this warning + # even if they are not using any deprecated modules + return warnings.warn( f"Importing {name} from langchain root module is no longer supported." )