|
|
|
@ -27,36 +27,6 @@ class LangChainDeprecationWarning(DeprecationWarning):
|
|
|
|
|
class LangChainPendingDeprecationWarning(PendingDeprecationWarning):
|
|
|
|
|
"""A class for issuing deprecation warnings for LangChain users."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_warning_internal() -> bool:
|
|
|
|
|
"""Check if the warning is internal to LangChain libraries."""
|
|
|
|
|
# Get the current stack frame
|
|
|
|
|
try:
|
|
|
|
|
stack = inspect.stack(context=1)
|
|
|
|
|
for idx, frame in enumerate(stack):
|
|
|
|
|
if frame.filename.split("/")[-1] != "deprecation.py":
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# Here we will be looking at the first frame which is not in deprecation.py
|
|
|
|
|
# This frame is the caller of the warning, and it will be the
|
|
|
|
|
# @deprecated decorator usually (i.e., the file that uses the decorator)
|
|
|
|
|
# We want to step another frame back to get the actual caller of the function
|
|
|
|
|
# that uses the decorator.
|
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
|
|
caller_frame = stack[idx].frame
|
|
|
|
|
# You can now use the frame object, for example, to get information about the code
|
|
|
|
|
if "__package__" not in caller_frame.f_globals:
|
|
|
|
|
return False
|
|
|
|
|
package = caller_frame.f_globals["__package__"]
|
|
|
|
|
# Most langchain libraries start with langchain.
|
|
|
|
|
# So this will suppress warnings from most langchain libraries.
|
|
|
|
|
# This does not have to be perfect
|
|
|
|
|
return package.startswith("langchain")
|
|
|
|
|
except Exception:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SURFACE_INTERNAL_WARNINGS = (
|
|
|
|
|
os.environ.get("SURFACE_INTERNAL_WARNINGS", "false").lower() == "true"
|
|
|
|
|
)
|
|
|
|
@ -446,10 +416,9 @@ def caller_aware_warn(
|
|
|
|
|
surface_internal_warnings: bool = False,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Warn deprecated"""
|
|
|
|
|
is_warning_internal()
|
|
|
|
|
if surface_internal_warnings:
|
|
|
|
|
warnings.warn(message, category=category, stacklevel=2)
|
|
|
|
|
else:
|
|
|
|
|
if is_warning_internal():
|
|
|
|
|
if is_caller_internal(depth=2):
|
|
|
|
|
return
|
|
|
|
|
warnings.warn(message, category=category, stacklevel=2)
|
|
|
|
|