Skip to content

Commit 7f5be6b

Browse files
authored
chore(core,langchain,langchain_v1)!: remove globals from langchain-v1, update globals in langchain-classic, langchain-core (#33251)
* Remove globals.py from langchain_v1 * Adjust langchain-core to not inspect langchain namespace
1 parent 1074ce5 commit 7f5be6b

File tree

6 files changed

+27
-446
lines changed

6 files changed

+27
-446
lines changed
Lines changed: 3 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
"""Global values and configuration that apply to all of LangChain."""
22

3-
import warnings
43
from typing import TYPE_CHECKING, Optional
54

65
if TYPE_CHECKING:
76
from langchain_core.caches import BaseCache
87

9-
try:
10-
import langchain # type: ignore[import-not-found]
11-
12-
_HAS_LANGCHAIN = True
13-
except ImportError:
14-
_HAS_LANGCHAIN = False
15-
168

179
# DO NOT USE THESE VALUES DIRECTLY!
1810
# Use them only via `get_<X>()` and `set_<X>()` below,
@@ -29,26 +21,6 @@ def set_verbose(value: bool) -> None: # noqa: FBT001
2921
Args:
3022
value: The new value for the `verbose` global setting.
3123
"""
32-
if _HAS_LANGCHAIN:
33-
# We're about to run some deprecated code, don't report warnings from it.
34-
# The user called the correct (non-deprecated) code path and shouldn't get
35-
# warnings.
36-
with warnings.catch_warnings():
37-
warnings.filterwarnings(
38-
"ignore",
39-
message=(
40-
"Importing verbose from langchain root module "
41-
"is no longer supported"
42-
),
43-
)
44-
# N.B.: This is a workaround for an unfortunate quirk of Python's
45-
# module-level `__getattr__()` implementation:
46-
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
47-
#
48-
# Remove it once `langchain.verbose` is no longer supported, and once all
49-
# users have migrated to using `set_verbose()` here.
50-
langchain.verbose = value
51-
5224
global _verbose # noqa: PLW0603
5325
_verbose = value
5426

@@ -59,35 +31,7 @@ def get_verbose() -> bool:
5931
Returns:
6032
The value of the `verbose` global setting.
6133
"""
62-
if _HAS_LANGCHAIN:
63-
# We're about to run some deprecated code, don't report warnings from it.
64-
# The user called the correct (non-deprecated) code path and shouldn't get
65-
# warnings.
66-
with warnings.catch_warnings():
67-
warnings.filterwarnings(
68-
"ignore",
69-
message=(
70-
".*Importing verbose from langchain root module "
71-
"is no longer supported"
72-
),
73-
)
74-
# N.B.: This is a workaround for an unfortunate quirk of Python's
75-
# module-level `__getattr__()` implementation:
76-
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
77-
#
78-
# Remove it once `langchain.verbose` is no longer supported, and once all
79-
# users have migrated to using `set_verbose()` here.
80-
#
81-
# In the meantime, the `verbose` setting is considered True if either the
82-
# old or the new value are True. This accommodates users who haven't
83-
# migrated to using `set_verbose()` yet. Those users are getting
84-
# deprecation warnings directing them to use `set_verbose()` when they
85-
# import `langchain.verbose`.
86-
old_verbose = langchain.verbose
87-
else:
88-
old_verbose = False
89-
90-
return _verbose or old_verbose
34+
return _verbose
9135

9236

9337
def set_debug(value: bool) -> None: # noqa: FBT001
@@ -96,24 +40,6 @@ def set_debug(value: bool) -> None: # noqa: FBT001
9640
Args:
9741
value: The new value for the `debug` global setting.
9842
"""
99-
if _HAS_LANGCHAIN:
100-
# We're about to run some deprecated code, don't report warnings from it.
101-
# The user called the correct (non-deprecated) code path and shouldn't get
102-
# warnings.
103-
with warnings.catch_warnings():
104-
warnings.filterwarnings(
105-
"ignore",
106-
message="Importing debug from langchain root module "
107-
"is no longer supported",
108-
)
109-
# N.B.: This is a workaround for an unfortunate quirk of Python's
110-
# module-level `__getattr__()` implementation:
111-
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
112-
#
113-
# Remove it once `langchain.debug` is no longer supported, and once all
114-
# users have migrated to using `set_debug()` here.
115-
langchain.debug = value
116-
11743
global _debug # noqa: PLW0603
11844
_debug = value
11945

@@ -124,32 +50,7 @@ def get_debug() -> bool:
12450
Returns:
12551
The value of the `debug` global setting.
12652
"""
127-
if _HAS_LANGCHAIN:
128-
# We're about to run some deprecated code, don't report warnings from it.
129-
# The user called the correct (non-deprecated) code path and shouldn't get
130-
# warnings.
131-
with warnings.catch_warnings():
132-
warnings.filterwarnings(
133-
"ignore",
134-
message="Importing debug from langchain root module "
135-
"is no longer supported",
136-
)
137-
# N.B.: This is a workaround for an unfortunate quirk of Python's
138-
# module-level `__getattr__()` implementation:
139-
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
140-
#
141-
# Remove it once `langchain.debug` is no longer supported, and once all
142-
# users have migrated to using `set_debug()` here.
143-
#
144-
# In the meantime, the `debug` setting is considered True if either the old
145-
# or the new value are True. This accommodates users who haven't migrated
146-
# to using `set_debug()` yet. Those users are getting deprecation warnings
147-
# directing them to use `set_debug()` when they import `langchain.debug`.
148-
old_debug = langchain.debug
149-
else:
150-
old_debug = False
151-
152-
return _debug or old_debug
53+
return _debug
15354

15455

15556
def set_llm_cache(value: Optional["BaseCache"]) -> None:
@@ -158,26 +59,6 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
15859
Args:
15960
value: The new LLM cache to use. If `None`, the LLM cache is disabled.
16061
"""
161-
if _HAS_LANGCHAIN:
162-
# We're about to run some deprecated code, don't report warnings from it.
163-
# The user called the correct (non-deprecated) code path and shouldn't get
164-
# warnings.
165-
with warnings.catch_warnings():
166-
warnings.filterwarnings(
167-
"ignore",
168-
message=(
169-
"Importing llm_cache from langchain root module "
170-
"is no longer supported"
171-
),
172-
)
173-
# N.B.: This is a workaround for an unfortunate quirk of Python's
174-
# module-level `__getattr__()` implementation:
175-
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
176-
#
177-
# Remove it once `langchain.llm_cache` is no longer supported, and
178-
# once all users have migrated to using `set_llm_cache()` here.
179-
langchain.llm_cache = value
180-
18162
global _llm_cache # noqa: PLW0603
18263
_llm_cache = value
18364

@@ -188,33 +69,4 @@ def get_llm_cache() -> Optional["BaseCache"]:
18869
Returns:
18970
The value of the `llm_cache` global setting.
19071
"""
191-
if _HAS_LANGCHAIN:
192-
# We're about to run some deprecated code, don't report warnings from it.
193-
# The user called the correct (non-deprecated) code path and shouldn't get
194-
# warnings.
195-
with warnings.catch_warnings():
196-
warnings.filterwarnings(
197-
"ignore",
198-
message=(
199-
"Importing llm_cache from langchain root module "
200-
"is no longer supported"
201-
),
202-
)
203-
# N.B.: This is a workaround for an unfortunate quirk of Python's
204-
# module-level `__getattr__()` implementation:
205-
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
206-
#
207-
# Remove it once `langchain.llm_cache` is no longer supported, and
208-
# once all users have migrated to using `set_llm_cache()` here.
209-
#
210-
# In the meantime, the `llm_cache` setting returns whichever of
211-
# its two backing sources is truthy (not `None` and non-empty),
212-
# or the old value if both are falsy. This accommodates users
213-
# who haven't migrated to using `set_llm_cache()` yet.
214-
# Those users are getting deprecation warnings directing them
215-
# to use `set_llm_cache()` when they import `langchain.llm_cache`.
216-
old_llm_cache = langchain.llm_cache
217-
else:
218-
old_llm_cache = None
219-
220-
return _llm_cache or old_llm_cache
72+
return _llm_cache

libs/langchain/langchain/__init__.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -366,39 +366,6 @@ def __getattr__(name: str) -> Any:
366366
)
367367

368368
return SerpAPIWrapper
369-
if name == "verbose":
370-
from langchain.globals import _verbose
371-
372-
_warn_on_import(
373-
name,
374-
replacement=(
375-
"langchain.globals.set_verbose() / langchain.globals.get_verbose()"
376-
),
377-
)
378-
379-
return _verbose
380-
if name == "debug":
381-
from langchain.globals import _debug
382-
383-
_warn_on_import(
384-
name,
385-
replacement=(
386-
"langchain.globals.set_debug() / langchain.globals.get_debug()"
387-
),
388-
)
389-
390-
return _debug
391-
if name == "llm_cache":
392-
from langchain.globals import _llm_cache
393-
394-
_warn_on_import(
395-
name,
396-
replacement=(
397-
"langchain.globals.set_llm_cache() / langchain.globals.get_llm_cache()"
398-
),
399-
)
400-
401-
return _llm_cache
402369
msg = f"Could not find: {name}"
403370
raise AttributeError(msg)
404371

0 commit comments

Comments
 (0)