Skip to content

Commit

Permalink
langchain[patch]: Improve deprecation warnings (#21262)
Browse files Browse the repository at this point in the history
* Remove spurious derprecation warning
* Make deprecation warnings consistent with 0.1 namespaces that were announced as deprecated
  • Loading branch information
eyurtsev committed May 3, 2024
1 parent 487aff7 commit d6e34f9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
65 changes: 52 additions & 13 deletions libs/langchain/langchain/_api/module_import.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import importlib
import warnings
from typing import Any, Callable, Dict, Optional

from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import warn_deprecated

from langchain._api.interactive_env import is_interactive_env

Expand All @@ -13,6 +12,33 @@
}


# For 0.1 releases keep this here
# Remove for 0.2 release so that deprecation warnings will
# be raised for all the new namespaces.
_NAMESPACES_WITH_DEPRECATION_WARNINGS_IN_0_1 = {
"langchain",
"langchain.adapters.openai",
"langchain.agents.agent_toolkits",
"langchain.callbacks",
"langchain.chat_models",
"langchain.docstore",
"langchain.document_loaders",
"langchain.document_transformers",
"langchain.embeddings",
"langchain.llms",
"langchain.memory.chat_message_histories",
"langchain.storage",
"langchain.tools",
"langchain.utilities",
"langchain.vectorstores",
}


def _should_deprecate_for_package(package: str) -> bool:
"""Should deprecate for this package?"""
return bool(package in _NAMESPACES_WITH_DEPRECATION_WARNINGS_IN_0_1)


def create_importer(
package: str,
*,
Expand Down Expand Up @@ -83,12 +109,19 @@ def import_by_name(name: str) -> Any:
not is_interactive_env()
and deprecated_lookups
and name in deprecated_lookups
and _should_deprecate_for_package(package)
):
warnings.warn(
f"Importing {name} from {package} is deprecated. "
"Please replace the import with the following:\n"
f"from {new_module} import {name}",
category=LangChainDeprecationWarning,
warn_deprecated(
since="0.1",
pending=False,
removal="0.4",
message=(
f"Importing {name} from {package} is deprecated."
f"Please replace imports that look like:"
f"`from {package} import {name}`\n"
"with the following:\n "
f"from {new_module} import {name}"
),
)
return result
except Exception as e:
Expand All @@ -100,12 +133,18 @@ def import_by_name(name: str) -> Any:
try:
module = importlib.import_module(fallback_module)
result = getattr(module, name)
if not is_interactive_env():
warnings.warn(
f"Importing {name} from {package} is deprecated. "
"Please replace the import with the following:\n"
f"from {fallback_module} import {name}",
category=LangChainDeprecationWarning,
if not is_interactive_env() and _should_deprecate_for_package(package):
warn_deprecated(
since="0.1",
pending=False,
removal="0.4",
message=(
f"Importing {name} from {package} is deprecated."
f"Please replace imports that look like:"
f"`from {package} import {name}`\n"
"with the following:\n "
f"from {fallback_module} import {name}"
),
)
return result

Expand Down
1 change: 0 additions & 1 deletion libs/langchain/langchain/output_parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from langchain.output_parsers.enum import EnumOutputParser
from langchain.output_parsers.fix import OutputFixingParser
from langchain.output_parsers.pandas_dataframe import PandasDataFrameOutputParser
from langchain.output_parsers.rail_parser import GuardrailsOutputParser
from langchain.output_parsers.regex import RegexParser
from langchain.output_parsers.regex_dict import RegexDictParser
from langchain.output_parsers.retry import RetryOutputParser, RetryWithErrorOutputParser
Expand Down

0 comments on commit d6e34f9

Please sign in to comment.