Skip to content

The agent engineering platform.

core[patch]: add alternative_import to deprecated (#15781)

Erick Friis656e87beb955
diff --git a/libs/core/langchain_core/_api/deprecation.py b/libs/core/langchain_core/_api/deprecation.py
index eee686a181..b9a0c39e48 100644
--- a/libs/core/langchain_core/_api/deprecation.py
+++ b/libs/core/langchain_core/_api/deprecation.py
@@ -39,6 +39,7 @@ def deprecated(
message: str = "",
name: str = "",
alternative: str = "",
+ alternative_import: str = "",
pending: bool = False,
obj_type: str = "",
addendum: str = "",
@@ -105,6 +106,7 @@ def deprecated(
_name: str = name,
_message: str = message,
_alternative: str = alternative,
+ _alternative_import: str = alternative_import,
_pending: bool = pending,
_addendum: str = addendum,
) -> T:
@@ -117,6 +119,7 @@ def deprecated(
message=_message,
name=_name,
alternative=_alternative,
+ alternative_import=_alternative_import,
pending=_pending,
obj_type=_obj_type,
addendum=_addendum,
@@ -145,7 +148,9 @@ def deprecated(
if not _obj_type:
_obj_type = "class"
wrapped = obj.__init__ # type: ignore
- _name = _name or obj.__name__
+ _name = _name or (
+ f"{obj.__module__}.{obj.__name__}" if obj.__module__ else obj.__name__
+ )
old_doc = obj.__doc__
def finalize(_: Any, new_doc: str) -> T:
@@ -271,6 +276,7 @@ def warn_deprecated(
message: str = "",
name: str = "",
alternative: str = "",
+ alternative_import: str = "",
pending: bool = False,
obj_type: str = "",
addendum: str = "",
@@ -307,6 +313,10 @@ def warn_deprecated(
"""
if pending and removal:
raise ValueError("A pending deprecation cannot have a scheduled removal")
+ if alternative and alternative_import:
+ raise ValueError("Cannot specify both alternative and alternative_import")
+ if alternative_import and "." not in alternative_import:
+ raise ValueError("alternative_import must be a fully qualified module path")
if not pending:
if not removal:
@@ -320,6 +330,7 @@ def warn_deprecated(
if not message:
message = ""
+ package = name.split(".")[0].replace("_", "-") if "." in name else "LangChain"
if obj_type:
message += f"The {obj_type} `{name}`"
@@ -329,12 +340,24 @@ def warn_deprecated(
if pending:
message += " will be deprecated in a future version"
else:
- message += f" was deprecated in LangChain {since}"
+ message += f" was deprecated in {package} {since}"
if removal:
message += f" and will be removed {removal}"
- if alternative:
+ if alternative_import:
+ alt_package = alternative_import.split(".")[0].replace("_", "-")
+ if alt_package == package:
+ message += f". Use {alternative_import} instead."
+ else:
+ alt_module, alt_name = alternative_import.rsplit(".", 1)
+ message += (
+ f". An updated version of the {obj_type} exists in the "
+ f"{alt_package} package and should be used instead. To use it run "
+ f"`pip install -U {alt_package}` and import as "
+ f"`from {alt_module} import {alt_name}`."
+ )
+ elif alternative:
message += f". Use {alternative} instead."
if addendum:
diff --git a/libs/core/tests/unit_tests/_api/test_deprecation.py b/libs/core/tests/unit_tests/_api/test_deprecation.py
index 238ba231fa..dc7f40de13 100644
--- a/libs/core/tests/unit_tests/_api/test_deprecation.py
+++ b/libs/core/tests/unit_tests/_api/test_deprecation.py
@@ -219,8 +219,8 @@ def test_whole_class_deprecation() -> None:
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
- "The class `DeprecatedClass` was deprecated in "
- "LangChain 2.0.0 and will be removed in 3.0.0"
+ "The class `tests.unit_tests._api.test_deprecation.DeprecatedClass` was "
+ "deprecated in tests 2.0.0 and will be removed in 3.0.0"
)
warning = warning_list[1].message