langchain[patch]: Improve deprecation warnings (#21262)

* Remove spurious derprecation warning
* Make deprecation warnings consistent with 0.1 namespaces that were announced as deprecated
This commit is contained in:
Eugene Yurtsev 2024-05-03 13:40:16 -04:00 committed by GitHub
parent 487aff7e46
commit d6e34f9ee5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 14 deletions

View File

@ -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
@ -13,6 +12,33 @@ ALLOWED_TOP_LEVEL_PKGS = {
}
# 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,
*,
@ -83,12 +109,19 @@ def create_importer(
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:
@ -100,12 +133,18 @@ def create_importer(
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

View File

@ -35,7 +35,6 @@ from langchain.output_parsers.datetime import DatetimeOutputParser
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