langchain-core 0.3.79__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langchain-core might be problematic. Click here for more details.

Files changed (165) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +23 -26
  4. langchain_core/_api/deprecation.py +52 -65
  5. langchain_core/_api/path.py +3 -6
  6. langchain_core/_import_utils.py +3 -4
  7. langchain_core/agents.py +19 -19
  8. langchain_core/caches.py +53 -63
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +323 -334
  11. langchain_core/callbacks/file.py +44 -44
  12. langchain_core/callbacks/manager.py +441 -507
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +32 -32
  15. langchain_core/callbacks/usage.py +60 -57
  16. langchain_core/chat_history.py +48 -63
  17. langchain_core/document_loaders/base.py +23 -23
  18. langchain_core/document_loaders/langsmith.py +37 -37
  19. langchain_core/documents/__init__.py +0 -1
  20. langchain_core/documents/base.py +62 -65
  21. langchain_core/documents/compressor.py +4 -4
  22. langchain_core/documents/transformers.py +28 -29
  23. langchain_core/embeddings/fake.py +50 -54
  24. langchain_core/example_selectors/length_based.py +1 -1
  25. langchain_core/example_selectors/semantic_similarity.py +21 -25
  26. langchain_core/exceptions.py +10 -11
  27. langchain_core/globals.py +3 -151
  28. langchain_core/indexing/api.py +61 -66
  29. langchain_core/indexing/base.py +58 -58
  30. langchain_core/indexing/in_memory.py +3 -3
  31. langchain_core/language_models/__init__.py +14 -27
  32. langchain_core/language_models/_utils.py +270 -84
  33. langchain_core/language_models/base.py +55 -162
  34. langchain_core/language_models/chat_models.py +442 -402
  35. langchain_core/language_models/fake.py +11 -11
  36. langchain_core/language_models/fake_chat_models.py +61 -39
  37. langchain_core/language_models/llms.py +123 -231
  38. langchain_core/load/dump.py +4 -5
  39. langchain_core/load/load.py +18 -28
  40. langchain_core/load/mapping.py +2 -4
  41. langchain_core/load/serializable.py +39 -40
  42. langchain_core/messages/__init__.py +61 -22
  43. langchain_core/messages/ai.py +368 -163
  44. langchain_core/messages/base.py +214 -43
  45. langchain_core/messages/block_translators/__init__.py +111 -0
  46. langchain_core/messages/block_translators/anthropic.py +470 -0
  47. langchain_core/messages/block_translators/bedrock.py +94 -0
  48. langchain_core/messages/block_translators/bedrock_converse.py +297 -0
  49. langchain_core/messages/block_translators/google_genai.py +530 -0
  50. langchain_core/messages/block_translators/google_vertexai.py +21 -0
  51. langchain_core/messages/block_translators/groq.py +143 -0
  52. langchain_core/messages/block_translators/langchain_v0.py +301 -0
  53. langchain_core/messages/block_translators/openai.py +1010 -0
  54. langchain_core/messages/chat.py +2 -6
  55. langchain_core/messages/content.py +1423 -0
  56. langchain_core/messages/function.py +6 -10
  57. langchain_core/messages/human.py +41 -38
  58. langchain_core/messages/modifier.py +2 -2
  59. langchain_core/messages/system.py +38 -28
  60. langchain_core/messages/tool.py +96 -103
  61. langchain_core/messages/utils.py +478 -504
  62. langchain_core/output_parsers/__init__.py +1 -14
  63. langchain_core/output_parsers/base.py +58 -61
  64. langchain_core/output_parsers/json.py +7 -8
  65. langchain_core/output_parsers/list.py +5 -7
  66. langchain_core/output_parsers/openai_functions.py +49 -47
  67. langchain_core/output_parsers/openai_tools.py +14 -19
  68. langchain_core/output_parsers/pydantic.py +12 -13
  69. langchain_core/output_parsers/string.py +2 -2
  70. langchain_core/output_parsers/transform.py +15 -17
  71. langchain_core/output_parsers/xml.py +8 -10
  72. langchain_core/outputs/__init__.py +1 -1
  73. langchain_core/outputs/chat_generation.py +18 -18
  74. langchain_core/outputs/chat_result.py +1 -3
  75. langchain_core/outputs/generation.py +8 -8
  76. langchain_core/outputs/llm_result.py +10 -10
  77. langchain_core/prompt_values.py +12 -12
  78. langchain_core/prompts/__init__.py +3 -27
  79. langchain_core/prompts/base.py +45 -55
  80. langchain_core/prompts/chat.py +254 -313
  81. langchain_core/prompts/dict.py +5 -5
  82. langchain_core/prompts/few_shot.py +81 -88
  83. langchain_core/prompts/few_shot_with_templates.py +11 -13
  84. langchain_core/prompts/image.py +12 -14
  85. langchain_core/prompts/loading.py +6 -8
  86. langchain_core/prompts/message.py +3 -3
  87. langchain_core/prompts/prompt.py +24 -39
  88. langchain_core/prompts/string.py +4 -4
  89. langchain_core/prompts/structured.py +42 -50
  90. langchain_core/rate_limiters.py +51 -60
  91. langchain_core/retrievers.py +49 -190
  92. langchain_core/runnables/base.py +1484 -1709
  93. langchain_core/runnables/branch.py +45 -61
  94. langchain_core/runnables/config.py +80 -88
  95. langchain_core/runnables/configurable.py +117 -134
  96. langchain_core/runnables/fallbacks.py +83 -79
  97. langchain_core/runnables/graph.py +85 -95
  98. langchain_core/runnables/graph_ascii.py +27 -28
  99. langchain_core/runnables/graph_mermaid.py +38 -50
  100. langchain_core/runnables/graph_png.py +15 -16
  101. langchain_core/runnables/history.py +135 -148
  102. langchain_core/runnables/passthrough.py +124 -150
  103. langchain_core/runnables/retry.py +46 -51
  104. langchain_core/runnables/router.py +25 -30
  105. langchain_core/runnables/schema.py +79 -74
  106. langchain_core/runnables/utils.py +62 -68
  107. langchain_core/stores.py +81 -115
  108. langchain_core/structured_query.py +8 -8
  109. langchain_core/sys_info.py +27 -29
  110. langchain_core/tools/__init__.py +1 -14
  111. langchain_core/tools/base.py +179 -187
  112. langchain_core/tools/convert.py +131 -139
  113. langchain_core/tools/render.py +10 -10
  114. langchain_core/tools/retriever.py +11 -11
  115. langchain_core/tools/simple.py +19 -24
  116. langchain_core/tools/structured.py +30 -39
  117. langchain_core/tracers/__init__.py +1 -9
  118. langchain_core/tracers/base.py +97 -99
  119. langchain_core/tracers/context.py +29 -52
  120. langchain_core/tracers/core.py +50 -60
  121. langchain_core/tracers/evaluation.py +11 -11
  122. langchain_core/tracers/event_stream.py +115 -70
  123. langchain_core/tracers/langchain.py +21 -21
  124. langchain_core/tracers/log_stream.py +43 -43
  125. langchain_core/tracers/memory_stream.py +3 -3
  126. langchain_core/tracers/root_listeners.py +16 -16
  127. langchain_core/tracers/run_collector.py +2 -4
  128. langchain_core/tracers/schemas.py +0 -129
  129. langchain_core/tracers/stdout.py +3 -3
  130. langchain_core/utils/__init__.py +1 -4
  131. langchain_core/utils/_merge.py +46 -8
  132. langchain_core/utils/aiter.py +57 -61
  133. langchain_core/utils/env.py +9 -9
  134. langchain_core/utils/function_calling.py +89 -191
  135. langchain_core/utils/html.py +7 -8
  136. langchain_core/utils/input.py +6 -6
  137. langchain_core/utils/interactive_env.py +1 -1
  138. langchain_core/utils/iter.py +37 -42
  139. langchain_core/utils/json.py +4 -3
  140. langchain_core/utils/json_schema.py +8 -8
  141. langchain_core/utils/mustache.py +9 -11
  142. langchain_core/utils/pydantic.py +33 -35
  143. langchain_core/utils/strings.py +5 -5
  144. langchain_core/utils/usage.py +1 -1
  145. langchain_core/utils/utils.py +80 -54
  146. langchain_core/vectorstores/base.py +129 -164
  147. langchain_core/vectorstores/in_memory.py +99 -174
  148. langchain_core/vectorstores/utils.py +5 -5
  149. langchain_core/version.py +1 -1
  150. {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
  151. langchain_core-1.0.0.dist-info/RECORD +172 -0
  152. {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
  153. langchain_core/beta/__init__.py +0 -1
  154. langchain_core/beta/runnables/__init__.py +0 -1
  155. langchain_core/beta/runnables/context.py +0 -447
  156. langchain_core/memory.py +0 -120
  157. langchain_core/messages/content_blocks.py +0 -176
  158. langchain_core/prompts/pipeline.py +0 -138
  159. langchain_core/pydantic_v1/__init__.py +0 -30
  160. langchain_core/pydantic_v1/dataclasses.py +0 -23
  161. langchain_core/pydantic_v1/main.py +0 -23
  162. langchain_core/tracers/langchain_v1.py +0 -31
  163. langchain_core/utils/loading.py +0 -35
  164. langchain_core-0.3.79.dist-info/RECORD +0 -174
  165. langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
@@ -18,40 +18,38 @@ class FakeEmbeddings(Embeddings, BaseModel):
18
18
 
19
19
  This embedding model creates embeddings by sampling from a normal distribution.
20
20
 
21
- Do not use this outside of testing, as it is not a real embedding model.
21
+ !!! warning
22
+ Do not use this outside of testing, as it is not a real embedding model.
22
23
 
23
24
  Instantiate:
24
- .. code-block:: python
25
+ ```python
26
+ from langchain_core.embeddings import FakeEmbeddings
25
27
 
26
- from langchain_core.embeddings import FakeEmbeddings
27
-
28
- embed = FakeEmbeddings(size=100)
28
+ embed = FakeEmbeddings(size=100)
29
+ ```
29
30
 
30
31
  Embed single text:
31
- .. code-block:: python
32
-
33
- input_text = "The meaning of life is 42"
34
- vector = embed.embed_query(input_text)
35
- print(vector[:3])
36
-
37
- .. code-block:: python
38
-
39
- [-0.700234640213188, -0.581266257710429, -1.1328482266445354]
32
+ ```python
33
+ input_text = "The meaning of life is 42"
34
+ vector = embed.embed_query(input_text)
35
+ print(vector[:3])
36
+ ```
37
+ ```python
38
+ [-0.700234640213188, -0.581266257710429, -1.1328482266445354]
39
+ ```
40
40
 
41
41
  Embed multiple texts:
42
- .. code-block:: python
43
-
44
- input_texts = ["Document 1...", "Document 2..."]
45
- vectors = embed.embed_documents(input_texts)
46
- print(len(vectors))
47
- # The first 3 coordinates for the first vector
48
- print(vectors[0][:3])
49
-
50
- .. code-block:: python
51
-
52
- 2
53
- [-0.5670477847544458, -0.31403828652395727, -0.5840547508955257]
54
-
42
+ ```python
43
+ input_texts = ["Document 1...", "Document 2..."]
44
+ vectors = embed.embed_documents(input_texts)
45
+ print(len(vectors))
46
+ # The first 3 coordinates for the first vector
47
+ print(vectors[0][:3])
48
+ ```
49
+ ```python
50
+ 2
51
+ [-0.5670477847544458, -0.31403828652395727, -0.5840547508955257]
52
+ ```
55
53
  """
56
54
 
57
55
  size: int
@@ -75,40 +73,38 @@ class DeterministicFakeEmbedding(Embeddings, BaseModel):
75
73
  This embedding model creates embeddings by sampling from a normal distribution
76
74
  with a seed based on the hash of the text.
77
75
 
78
- Do not use this outside of testing, as it is not a real embedding model.
76
+ !!! warning
77
+ Do not use this outside of testing, as it is not a real embedding model.
79
78
 
80
79
  Instantiate:
81
- .. code-block:: python
80
+ ```python
81
+ from langchain_core.embeddings import DeterministicFakeEmbedding
82
82
 
83
- from langchain_core.embeddings import DeterministicFakeEmbedding
84
-
85
- embed = DeterministicFakeEmbedding(size=100)
83
+ embed = DeterministicFakeEmbedding(size=100)
84
+ ```
86
85
 
87
86
  Embed single text:
88
- .. code-block:: python
89
-
90
- input_text = "The meaning of life is 42"
91
- vector = embed.embed_query(input_text)
92
- print(vector[:3])
93
-
94
- .. code-block:: python
95
-
96
- [-0.700234640213188, -0.581266257710429, -1.1328482266445354]
87
+ ```python
88
+ input_text = "The meaning of life is 42"
89
+ vector = embed.embed_query(input_text)
90
+ print(vector[:3])
91
+ ```
92
+ ```python
93
+ [-0.700234640213188, -0.581266257710429, -1.1328482266445354]
94
+ ```
97
95
 
98
96
  Embed multiple texts:
99
- .. code-block:: python
100
-
101
- input_texts = ["Document 1...", "Document 2..."]
102
- vectors = embed.embed_documents(input_texts)
103
- print(len(vectors))
104
- # The first 3 coordinates for the first vector
105
- print(vectors[0][:3])
106
-
107
- .. code-block:: python
108
-
109
- 2
110
- [-0.5670477847544458, -0.31403828652395727, -0.5840547508955257]
111
-
97
+ ```python
98
+ input_texts = ["Document 1...", "Document 2..."]
99
+ vectors = embed.embed_documents(input_texts)
100
+ print(len(vectors))
101
+ # The first 3 coordinates for the first vector
102
+ print(vectors[0][:3])
103
+ ```
104
+ ```python
105
+ 2
106
+ [-0.5670477847544458, -0.31403828652395727, -0.5840547508955257]
107
+ ```
112
108
  """
113
109
 
114
110
  size: int
@@ -1,7 +1,7 @@
1
1
  """Select examples based on length."""
2
2
 
3
3
  import re
4
- from typing import Callable
4
+ from collections.abc import Callable
5
5
 
6
6
  from pydantic import BaseModel, Field, model_validator
7
7
  from typing_extensions import Self
@@ -3,7 +3,7 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  from abc import ABC
6
- from typing import TYPE_CHECKING, Any, Optional
6
+ from typing import TYPE_CHECKING, Any
7
7
 
8
8
  from pydantic import BaseModel, ConfigDict
9
9
 
@@ -35,12 +35,12 @@ class _VectorStoreExampleSelector(BaseExampleSelector, BaseModel, ABC):
35
35
  """VectorStore that contains information about examples."""
36
36
  k: int = 4
37
37
  """Number of examples to select."""
38
- example_keys: Optional[list[str]] = None
38
+ example_keys: list[str] | None = None
39
39
  """Optional keys to filter examples to."""
40
- input_keys: Optional[list[str]] = None
40
+ input_keys: list[str] | None = None
41
41
  """Optional keys to filter input to. If provided, the search is based on
42
42
  the input variables instead of all variables."""
43
- vectorstore_kwargs: Optional[dict[str, Any]] = None
43
+ vectorstore_kwargs: dict[str, Any] | None = None
44
44
  """Extra arguments passed to similarity_search function of the vectorstore."""
45
45
 
46
46
  model_config = ConfigDict(
@@ -49,9 +49,7 @@ class _VectorStoreExampleSelector(BaseExampleSelector, BaseModel, ABC):
49
49
  )
50
50
 
51
51
  @staticmethod
52
- def _example_to_text(
53
- example: dict[str, str], input_keys: Optional[list[str]]
54
- ) -> str:
52
+ def _example_to_text(example: dict[str, str], input_keys: list[str] | None) -> str:
55
53
  if input_keys:
56
54
  return " ".join(sorted_values({key: example[key] for key in input_keys}))
57
55
  return " ".join(sorted_values(example))
@@ -142,10 +140,10 @@ class SemanticSimilarityExampleSelector(_VectorStoreExampleSelector):
142
140
  embeddings: Embeddings,
143
141
  vectorstore_cls: type[VectorStore],
144
142
  k: int = 4,
145
- input_keys: Optional[list[str]] = None,
143
+ input_keys: list[str] | None = None,
146
144
  *,
147
- example_keys: Optional[list[str]] = None,
148
- vectorstore_kwargs: Optional[dict] = None,
145
+ example_keys: list[str] | None = None,
146
+ vectorstore_kwargs: dict | None = None,
149
147
  **vectorstore_cls_kwargs: Any,
150
148
  ) -> SemanticSimilarityExampleSelector:
151
149
  """Create k-shot example selector using example list and embeddings.
@@ -156,7 +154,7 @@ class SemanticSimilarityExampleSelector(_VectorStoreExampleSelector):
156
154
  examples: List of examples to use in the prompt.
157
155
  embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings().
158
156
  vectorstore_cls: A vector store DB interface class, e.g. FAISS.
159
- k: Number of examples to select. Default is 4.
157
+ k: Number of examples to select.
160
158
  input_keys: If provided, the search is based on the input variables
161
159
  instead of all variables.
162
160
  example_keys: If provided, keys to filter examples to.
@@ -186,10 +184,10 @@ class SemanticSimilarityExampleSelector(_VectorStoreExampleSelector):
186
184
  embeddings: Embeddings,
187
185
  vectorstore_cls: type[VectorStore],
188
186
  k: int = 4,
189
- input_keys: Optional[list[str]] = None,
187
+ input_keys: list[str] | None = None,
190
188
  *,
191
- example_keys: Optional[list[str]] = None,
192
- vectorstore_kwargs: Optional[dict] = None,
189
+ example_keys: list[str] | None = None,
190
+ vectorstore_kwargs: dict | None = None,
193
191
  **vectorstore_cls_kwargs: Any,
194
192
  ) -> SemanticSimilarityExampleSelector:
195
193
  """Async create k-shot example selector using example list and embeddings.
@@ -200,7 +198,7 @@ class SemanticSimilarityExampleSelector(_VectorStoreExampleSelector):
200
198
  examples: List of examples to use in the prompt.
201
199
  embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings().
202
200
  vectorstore_cls: A vector store DB interface class, e.g. FAISS.
203
- k: Number of examples to select. Default is 4.
201
+ k: Number of examples to select.
204
202
  input_keys: If provided, the search is based on the input variables
205
203
  instead of all variables.
206
204
  example_keys: If provided, keys to filter examples to.
@@ -273,10 +271,10 @@ class MaxMarginalRelevanceExampleSelector(_VectorStoreExampleSelector):
273
271
  embeddings: Embeddings,
274
272
  vectorstore_cls: type[VectorStore],
275
273
  k: int = 4,
276
- input_keys: Optional[list[str]] = None,
274
+ input_keys: list[str] | None = None,
277
275
  fetch_k: int = 20,
278
- example_keys: Optional[list[str]] = None,
279
- vectorstore_kwargs: Optional[dict] = None,
276
+ example_keys: list[str] | None = None,
277
+ vectorstore_kwargs: dict | None = None,
280
278
  **vectorstore_cls_kwargs: Any,
281
279
  ) -> MaxMarginalRelevanceExampleSelector:
282
280
  """Create k-shot example selector using example list and embeddings.
@@ -287,9 +285,8 @@ class MaxMarginalRelevanceExampleSelector(_VectorStoreExampleSelector):
287
285
  examples: List of examples to use in the prompt.
288
286
  embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings().
289
287
  vectorstore_cls: A vector store DB interface class, e.g. FAISS.
290
- k: Number of examples to select. Default is 4.
288
+ k: Number of examples to select.
291
289
  fetch_k: Number of Documents to fetch to pass to MMR algorithm.
292
- Default is 20.
293
290
  input_keys: If provided, the search is based on the input variables
294
291
  instead of all variables.
295
292
  example_keys: If provided, keys to filter examples to.
@@ -321,10 +318,10 @@ class MaxMarginalRelevanceExampleSelector(_VectorStoreExampleSelector):
321
318
  vectorstore_cls: type[VectorStore],
322
319
  *,
323
320
  k: int = 4,
324
- input_keys: Optional[list[str]] = None,
321
+ input_keys: list[str] | None = None,
325
322
  fetch_k: int = 20,
326
- example_keys: Optional[list[str]] = None,
327
- vectorstore_kwargs: Optional[dict] = None,
323
+ example_keys: list[str] | None = None,
324
+ vectorstore_kwargs: dict | None = None,
328
325
  **vectorstore_cls_kwargs: Any,
329
326
  ) -> MaxMarginalRelevanceExampleSelector:
330
327
  """Create k-shot example selector using example list and embeddings.
@@ -335,9 +332,8 @@ class MaxMarginalRelevanceExampleSelector(_VectorStoreExampleSelector):
335
332
  examples: List of examples to use in the prompt.
336
333
  embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings().
337
334
  vectorstore_cls: A vector store DB interface class, e.g. FAISS.
338
- k: Number of examples to select. Default is 4.
335
+ k: Number of examples to select.
339
336
  fetch_k: Number of Documents to fetch to pass to MMR algorithm.
340
- Default is 20.
341
337
  input_keys: If provided, the search is based on the input variables
342
338
  instead of all variables.
343
339
  example_keys: If provided, keys to filter examples to.
@@ -1,7 +1,7 @@
1
1
  """Custom **exceptions** for LangChain."""
2
2
 
3
3
  from enum import Enum
4
- from typing import Any, Optional
4
+ from typing import Any
5
5
 
6
6
 
7
7
  class LangChainException(Exception): # noqa: N818
@@ -16,7 +16,7 @@ class OutputParserException(ValueError, LangChainException): # noqa: N818
16
16
  """Exception that output parsers should raise to signify a parsing error.
17
17
 
18
18
  This exists to differentiate parsing errors from other code or execution errors
19
- that also may arise inside the output parser. OutputParserExceptions will be
19
+ that also may arise inside the output parser. `OutputParserException` will be
20
20
  available to catch and handle in ways to fix the parsing error, while other
21
21
  errors will be raised.
22
22
  """
@@ -24,28 +24,27 @@ class OutputParserException(ValueError, LangChainException): # noqa: N818
24
24
  def __init__(
25
25
  self,
26
26
  error: Any,
27
- observation: Optional[str] = None,
28
- llm_output: Optional[str] = None,
27
+ observation: str | None = None,
28
+ llm_output: str | None = None,
29
29
  send_to_llm: bool = False, # noqa: FBT001,FBT002
30
30
  ):
31
- """Create an OutputParserException.
31
+ """Create an `OutputParserException`.
32
32
 
33
33
  Args:
34
34
  error: The error that's being re-raised or an error message.
35
35
  observation: String explanation of error which can be passed to a
36
- model to try and remediate the issue. Defaults to None.
36
+ model to try and remediate the issue.
37
37
  llm_output: String model output which is error-ing.
38
- Defaults to None.
38
+
39
39
  send_to_llm: Whether to send the observation and llm_output back to an Agent
40
- after an OutputParserException has been raised.
40
+ after an `OutputParserException` has been raised.
41
41
  This gives the underlying model driving the agent the context that the
42
42
  previous output was improperly structured, in the hopes that it will
43
43
  update the output to the correct format.
44
- Defaults to False.
45
44
 
46
45
  Raises:
47
- ValueError: If ``send_to_llm`` is True but either observation or
48
- ``llm_output`` are not provided.
46
+ ValueError: If `send_to_llm` is True but either observation or
47
+ `llm_output` are not provided.
49
48
  """
50
49
  if isinstance(error, str):
51
50
  error = create_message(
langchain_core/globals.py CHANGED
@@ -1,18 +1,10 @@
1
1
  """Global values and configuration that apply to all of LangChain."""
2
2
 
3
- import warnings
4
3
  from typing import TYPE_CHECKING, Optional
5
4
 
6
5
  if TYPE_CHECKING:
7
6
  from langchain_core.caches import BaseCache
8
7
 
9
- try:
10
- import langchain # type: ignore[import-not-found]
11
-
12
- _HAS_LANGCHAIN = True
13
- except ImportError:
14
- _HAS_LANGCHAIN = False
15
-
16
8
 
17
9
  # DO NOT USE THESE VALUES DIRECTLY!
18
10
  # Use them only via `get_<X>()` and `set_<X>()` below,
@@ -29,26 +21,6 @@ def set_verbose(value: bool) -> None: # noqa: FBT001
29
21
  Args:
30
22
  value: The new value for the `verbose` global setting.
31
23
  """
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
-
52
24
  global _verbose # noqa: PLW0603
53
25
  _verbose = value
54
26
 
@@ -59,35 +31,7 @@ def get_verbose() -> bool:
59
31
  Returns:
60
32
  The value of the `verbose` global setting.
61
33
  """
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
91
35
 
92
36
 
93
37
  def set_debug(value: bool) -> None: # noqa: FBT001
@@ -96,24 +40,6 @@ def set_debug(value: bool) -> None: # noqa: FBT001
96
40
  Args:
97
41
  value: The new value for the `debug` global setting.
98
42
  """
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
-
117
43
  global _debug # noqa: PLW0603
118
44
  _debug = value
119
45
 
@@ -124,32 +50,7 @@ def get_debug() -> bool:
124
50
  Returns:
125
51
  The value of the `debug` global setting.
126
52
  """
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
153
54
 
154
55
 
155
56
  def set_llm_cache(value: Optional["BaseCache"]) -> None:
@@ -158,26 +59,6 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
158
59
  Args:
159
60
  value: The new LLM cache to use. If `None`, the LLM cache is disabled.
160
61
  """
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
-
181
62
  global _llm_cache # noqa: PLW0603
182
63
  _llm_cache = value
183
64
 
@@ -188,33 +69,4 @@ def get_llm_cache() -> Optional["BaseCache"]:
188
69
  Returns:
189
70
  The value of the `llm_cache` global setting.
190
71
  """
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