The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
chore(core,langchain,langchain_v1)!: remove globals from langchain-v1, update globals in langchain-classic, langchain-core (#33251)
Eugene Yurtsev7f5be6b65c41
diff --git a/libs/core/langchain_core/globals.py b/libs/core/langchain_core/globals.pyindex 01e851f942..6880f675fd 100644--- a/libs/core/langchain_core/globals.py+++ b/libs/core/langchain_core/globals.py@@ -1,18 +1,10 @@"""Global values and configuration that apply to all of LangChain."""-import warningsfrom typing import TYPE_CHECKING, Optionalif TYPE_CHECKING:from langchain_core.caches import BaseCache-try:- import langchain # type: ignore[import-not-found]-- _HAS_LANGCHAIN = True-except ImportError:- _HAS_LANGCHAIN = False-# DO NOT USE THESE VALUES DIRECTLY!# Use them only via `get_<X>()` and `set_<X>()` below,@@ -29,26 +21,6 @@ def set_verbose(value: bool) -> None: # noqa: FBT001Args:value: The new value for the `verbose` global setting."""- if _HAS_LANGCHAIN:- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get- # warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing verbose from langchain root module "- "is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.verbose` is no longer supported, and once all- # users have migrated to using `set_verbose()` here.- langchain.verbose = value-global _verbose # noqa: PLW0603_verbose = value@@ -59,35 +31,7 @@ def get_verbose() -> bool:Returns:The value of the `verbose` global setting."""- if _HAS_LANGCHAIN:- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get- # warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- ".*Importing verbose from langchain root module "- "is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.verbose` is no longer supported, and once all- # users have migrated to using `set_verbose()` here.- #- # In the meantime, the `verbose` setting is considered True if either the- # old or the new value are True. This accommodates users who haven't- # migrated to using `set_verbose()` yet. Those users are getting- # deprecation warnings directing them to use `set_verbose()` when they- # import `langchain.verbose`.- old_verbose = langchain.verbose- else:- old_verbose = False-- return _verbose or old_verbose+ return _verbosedef set_debug(value: bool) -> None: # noqa: FBT001@@ -96,24 +40,6 @@ def set_debug(value: bool) -> None: # noqa: FBT001Args:value: The new value for the `debug` global setting."""- if _HAS_LANGCHAIN:- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get- # warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message="Importing debug from langchain root module "- "is no longer supported",- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.debug` is no longer supported, and once all- # users have migrated to using `set_debug()` here.- langchain.debug = value-global _debug # noqa: PLW0603_debug = value@@ -124,32 +50,7 @@ def get_debug() -> bool:Returns:The value of the `debug` global setting."""- if _HAS_LANGCHAIN:- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get- # warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message="Importing debug from langchain root module "- "is no longer supported",- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.debug` is no longer supported, and once all- # users have migrated to using `set_debug()` here.- #- # In the meantime, the `debug` setting is considered True if either the old- # or the new value are True. This accommodates users who haven't migrated- # to using `set_debug()` yet. Those users are getting deprecation warnings- # directing them to use `set_debug()` when they import `langchain.debug`.- old_debug = langchain.debug- else:- old_debug = False-- return _debug or old_debug+ return _debugdef set_llm_cache(value: Optional["BaseCache"]) -> None:@@ -158,26 +59,6 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:Args:value: The new LLM cache to use. If `None`, the LLM cache is disabled."""- if _HAS_LANGCHAIN:- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get- # warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing llm_cache from langchain root module "- "is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.llm_cache` is no longer supported, and- # once all users have migrated to using `set_llm_cache()` here.- langchain.llm_cache = value-global _llm_cache # noqa: PLW0603_llm_cache = value@@ -188,33 +69,4 @@ def get_llm_cache() -> Optional["BaseCache"]:Returns:The value of the `llm_cache` global setting."""- if _HAS_LANGCHAIN:- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get- # warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing llm_cache from langchain root module "- "is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.llm_cache` is no longer supported, and- # once all users have migrated to using `set_llm_cache()` here.- #- # In the meantime, the `llm_cache` setting returns whichever of- # its two backing sources is truthy (not `None` and non-empty),- # or the old value if both are falsy. This accommodates users- # who haven't migrated to using `set_llm_cache()` yet.- # Those users are getting deprecation warnings directing them- # to use `set_llm_cache()` when they import `langchain.llm_cache`.- old_llm_cache = langchain.llm_cache- else:- old_llm_cache = None-- return _llm_cache or old_llm_cache+ return _llm_cachediff --git a/libs/langchain/langchain/__init__.py b/libs/langchain/langchain/__init__.pyindex 2d441e75ce..323d7633c0 100644--- a/libs/langchain/langchain/__init__.py+++ b/libs/langchain/langchain/__init__.py@@ -366,39 +366,6 @@ def __getattr__(name: str) -> Any:)return SerpAPIWrapper- if name == "verbose":- from langchain.globals import _verbose-- _warn_on_import(- name,- replacement=(- "langchain.globals.set_verbose() / langchain.globals.get_verbose()"- ),- )-- return _verbose- if name == "debug":- from langchain.globals import _debug-- _warn_on_import(- name,- replacement=(- "langchain.globals.set_debug() / langchain.globals.get_debug()"- ),- )-- return _debug- if name == "llm_cache":- from langchain.globals import _llm_cache-- _warn_on_import(- name,- replacement=(- "langchain.globals.set_llm_cache() / langchain.globals.get_llm_cache()"- ),- )-- return _llm_cachemsg = f"Could not find: {name}"raise AttributeError(msg)diff --git a/libs/langchain/langchain/globals.py b/libs/langchain/langchain/globals.pyindex 34ad94cd0f..df23d53cfa 100644--- a/libs/langchain/langchain/globals.py+++ b/libs/langchain/langchain/globals.py@@ -1,180 +1,19 @@"""Global values and configuration that apply to all of LangChain."""-import warnings-from typing import TYPE_CHECKING, Optional--if TYPE_CHECKING:- from langchain_core.caches import BaseCache---# DO NOT USE THESE VALUES DIRECTLY!-# Use them only via `get_<X>()` and `set_<X>()` below,-# or else your code may behave unexpectedly with other uses of these global settings:-# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004-_verbose: bool = False-_debug: bool = False-_llm_cache: Optional["BaseCache"] = None---def set_verbose(- value: bool, # noqa: FBT001-) -> None:- """Set a new value for the `verbose` global setting."""- import langchain-- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing verbose from langchain root module is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.verbose` is no longer supported, and once all users- # have migrated to using `set_verbose()` here.- langchain.verbose = value-- global _verbose # noqa: PLW0603- _verbose = value---def get_verbose() -> bool:- """Get the value of the `verbose` global setting."""- import langchain-- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing verbose from langchain root module is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.verbose` is no longer supported, and once all users- # have migrated to using `set_verbose()` here.- #- # In the meantime, the `verbose` setting is considered True if either the old- # or the new value are True. This accommodates users who haven't migrated- # to using `set_verbose()` yet. Those users are getting deprecation warnings- # directing them to use `set_verbose()` when they import `langchain.verbose`.- old_verbose = langchain.verbose-- return _verbose or old_verbose---def set_debug(- value: bool, # noqa: FBT001-) -> None:- """Set a new value for the `debug` global setting."""- import langchain-- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message="Importing debug from langchain root module is no longer supported",- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.debug` is no longer supported, and once all users- # have migrated to using `set_debug()` here.- langchain.debug = value-- global _debug # noqa: PLW0603- _debug = value---def get_debug() -> bool:- """Get the value of the `debug` global setting."""- import langchain-- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message="Importing debug from langchain root module is no longer supported",- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.debug` is no longer supported, and once all users- # have migrated to using `set_debug()` here.- #- # In the meantime, the `debug` setting is considered True if either the old- # or the new value are True. This accommodates users who haven't migrated- # to using `set_debug()` yet. Those users are getting deprecation warnings- # directing them to use `set_debug()` when they import `langchain.debug`.- old_debug = langchain.debug-- return _debug or old_debug---def set_llm_cache(value: Optional["BaseCache"]) -> None:- """Set a new LLM cache, overwriting the previous value, if any."""- import langchain-- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing llm_cache from langchain root module is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.llm_cache` is no longer supported, and- # once all users have migrated to using `set_llm_cache()` here.- langchain.llm_cache = value-- global _llm_cache # noqa: PLW0603- _llm_cache = value---def get_llm_cache() -> "BaseCache":- """Get the value of the `llm_cache` global setting."""- import langchain-- # We're about to run some deprecated code, don't report warnings from it.- # The user called the correct (non-deprecated) code path and shouldn't get warnings.- with warnings.catch_warnings():- warnings.filterwarnings(- "ignore",- message=(- "Importing llm_cache from langchain root module is no longer supported"- ),- )- # N.B.: This is a workaround for an unfortunate quirk of Python's- # module-level `__getattr__()` implementation:- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004- #- # Remove it once `langchain.llm_cache` is no longer supported, and- # once all users have migrated to using `set_llm_cache()` here.- #- # In the meantime, the `llm_cache` setting returns whichever of- # its two backing sources is truthy (not `None` and non-empty),- # or the old value if both are falsy. This accommodates users- # who haven't migrated to using `set_llm_cache()` yet.- # Those users are getting deprecation warnings directing them- # to use `set_llm_cache()` when they import `langchain.llm_cache`.- old_llm_cache = langchain.llm_cache-- return _llm_cache or old_llm_cache+from langchain_core.globals import (+ get_debug,+ get_llm_cache,+ get_verbose,+ set_debug,+ set_llm_cache,+ set_verbose,+)++__all__ = [+ "get_debug",+ "get_llm_cache",+ "get_verbose",+ "set_debug",+ "set_llm_cache",+ "set_verbose",+]diff --git a/libs/langchain/tests/unit_tests/llms/test_base.py b/libs/langchain/tests/unit_tests/llms/test_base.pyindex 1410d94dab..35e9bcd95d 100644--- a/libs/langchain/tests/unit_tests/llms/test_base.py+++ b/libs/langchain/tests/unit_tests/llms/test_base.py@@ -25,10 +25,12 @@ def test_caching() -> None:params = llm.dict()params["stop"] = Nonellm_string = str(sorted([(k, v) for k, v in params.items()]))- get_llm_cache().update("foo", llm_string, [Generation(text="fizz")])+ cache = get_llm_cache()+ assert cache is not None+ cache.update("foo", llm_string, [Generation(text="fizz")])output = llm.generate(["foo", "bar", "foo"])expected_cache_output = [Generation(text="foo")]- cache_output = get_llm_cache().lookup("bar", llm_string)+ cache_output = cache.lookup("bar", llm_string)assert cache_output == expected_cache_outputset_llm_cache(None)expected_generations = [diff --git a/libs/langchain/tests/unit_tests/test_globals.py b/libs/langchain/tests/unit_tests/test_globals.pyindex fdb0b0ccf8..ae196f3f40 100644--- a/libs/langchain/tests/unit_tests/test_globals.py+++ b/libs/langchain/tests/unit_tests/test_globals.py@@ -22,42 +22,10 @@ def test_no_warning() -> None:core_set_verbose(value=False)-def test_debug_is_settable_directly() -> None:- from langchain_core.callbacks.manager import _get_debug-- import langchain-- previous_value = langchain.debug- previous_fn_reading = _get_debug()- assert previous_value == previous_fn_reading-- # Flip the value of the flag.- langchain.debug = not previous_value-- new_value = langchain.debug- new_fn_reading = _get_debug()-- try:- # We successfully changed the value of `debug`.- assert new_value != previous_value-- # If we access `debug` via a function used elsewhere in langchain,- # it also sees the same new value.- assert new_value == new_fn_reading-- # If we access `debug` via `get_debug()` we also get the same value.- assert new_value == get_debug()- finally:- # Make sure we don't alter global state, even if the test fails.- # Always reset `debug` to the value it had before.- set_debug(previous_value)--def test_debug_is_settable_via_setter() -> None:+ from langchain_core import globals as langchain_globalsfrom langchain_core.callbacks.manager import _get_debug- from langchain import globals as langchain_globals-previous_value = langchain_globals._debugprevious_fn_reading = _get_debug()assert previous_value == previous_fn_reading@@ -84,38 +52,9 @@ def test_debug_is_settable_via_setter() -> None:set_debug(previous_value)-def test_verbose_is_settable_directly() -> None:- import langchain- from langchain.chains.base import _get_verbosity-- previous_value = langchain.verbose- previous_fn_reading = _get_verbosity()- assert previous_value == previous_fn_reading-- # Flip the value of the flag.- langchain.verbose = not previous_value-- new_value = langchain.verbose- new_fn_reading = _get_verbosity()-- try:- # We successfully changed the value of `verbose`.- assert new_value != previous_value-- # If we access `verbose` via a function used elsewhere in langchain,- # it also sees the same new value.- assert new_value == new_fn_reading-- # If we access `verbose` via `get_verbose()` we also get the same value.- assert new_value == get_verbose()- finally:- # Make sure we don't alter global state, even if the test fails.- # Always reset `verbose` to the value it had before.- set_verbose(previous_value)--def test_verbose_is_settable_via_setter() -> None:- from langchain import globals as langchain_globals+ from langchain_core import globals as langchain_globals+from langchain.chains.base import _get_verbosityprevious_value = langchain_globals._verbosediff --git a/libs/langchain_v1/langchain/globals.py b/libs/langchain_v1/langchain/globals.pydeleted file mode 100644index ee1c0a90cd..0000000000--- a/libs/langchain_v1/langchain/globals.py+++ /dev/null@@ -1,18 +0,0 @@-"""Global values and configuration that apply to all of LangChain.--TODO: will be removed in a future alpha version.-"""--from typing import TYPE_CHECKING, Optional--if TYPE_CHECKING:- from langchain_core.caches import BaseCache---# DO NOT USE THESE VALUES DIRECTLY!-# Use them only via `get_<X>()` and `set_<X>()` below,-# or else your code may behave unexpectedly with other uses of these global settings:-# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004-_verbose: bool = False-_debug: bool = False-_llm_cache: Optional["BaseCache"] = None