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
@@ -11,17 +11,15 @@ from collections.abc import (
11
11
  AsyncIterable,
12
12
  AsyncIterator,
13
13
  Awaitable,
14
+ Callable,
14
15
  Iterator,
15
16
  )
16
17
  from contextlib import AbstractAsyncContextManager
17
18
  from types import TracebackType
18
19
  from typing import (
19
20
  Any,
20
- Callable,
21
21
  Generic,
22
- Optional,
23
22
  TypeVar,
24
- Union,
25
23
  cast,
26
24
  overload,
27
25
  )
@@ -36,8 +34,8 @@ _no_default = object()
36
34
  # https://github.com/python/cpython/blob/main/Lib/test/test_asyncgen.py#L54
37
35
  # before 3.10, the builtin anext() was not available
38
36
  def py_anext(
39
- iterator: AsyncIterator[T], default: Union[T, Any] = _no_default
40
- ) -> Awaitable[Union[T, Any, None]]:
37
+ iterator: AsyncIterator[T], default: T | Any = _no_default
38
+ ) -> Awaitable[T | Any | None]:
41
39
  """Pure-Python implementation of anext() for testing purposes.
42
40
 
43
41
  Closely matches the builtin anext() C implementation.
@@ -52,7 +50,7 @@ def py_anext(
52
50
 
53
51
  Returns:
54
52
  The next value from the iterator, or the default value
55
- if the iterator is exhausted.
53
+ if the iterator is exhausted.
56
54
 
57
55
  Raises:
58
56
  TypeError: If the iterator is not an async iterator.
@@ -68,7 +66,7 @@ def py_anext(
68
66
  if default is _no_default:
69
67
  return __anext__(iterator)
70
68
 
71
- async def anext_impl() -> Union[T, Any]:
69
+ async def anext_impl() -> T | Any:
72
70
  try:
73
71
  # The C code is way more low-level than this, as it implements
74
72
  # all methods of the iterator protocol. In this implementation
@@ -90,9 +88,9 @@ class NoLock:
90
88
 
91
89
  async def __aexit__(
92
90
  self,
93
- exc_type: Optional[type[BaseException]],
94
- exc_val: Optional[BaseException],
95
- exc_tb: Optional[TracebackType],
91
+ exc_type: type[BaseException] | None,
92
+ exc_val: BaseException | None,
93
+ exc_tb: TracebackType | None,
96
94
  ) -> bool:
97
95
  """Return False, exception not suppressed."""
98
96
  return False
@@ -106,10 +104,10 @@ async def tee_peer(
106
104
  peers: list[deque[T]],
107
105
  lock: AbstractAsyncContextManager[Any],
108
106
  ) -> AsyncGenerator[T, None]:
109
- """An individual iterator of a :py:func:`~.tee`.
107
+ """An individual iterator of a `tee`.
110
108
 
111
109
  This function is a generator that yields items from the shared iterator
112
- ``iterator``. It buffers items until the least advanced iterator has
110
+ `iterator`. It buffers items until the least advanced iterator has
113
111
  yielded them as well. The buffer is shared with all other peers.
114
112
 
115
113
  Args:
@@ -155,39 +153,39 @@ async def tee_peer(
155
153
 
156
154
 
157
155
  class Tee(Generic[T]):
158
- """Create ``n`` separate asynchronous iterators over ``iterable``.
156
+ """Create `n` separate asynchronous iterators over `iterable`.
159
157
 
160
- This splits a single ``iterable`` into multiple iterators, each providing
158
+ This splits a single `iterable` into multiple iterators, each providing
161
159
  the same items in the same order.
162
160
  All child iterators may advance separately but share the same items
163
- from ``iterable`` -- when the most advanced iterator retrieves an item,
161
+ from `iterable` -- when the most advanced iterator retrieves an item,
164
162
  it is buffered until the least advanced iterator has yielded it as well.
165
- A ``tee`` works lazily and can handle an infinite ``iterable``, provided
163
+ A `tee` works lazily and can handle an infinite `iterable`, provided
166
164
  that all iterators advance.
167
165
 
168
- .. code-block:: python
169
-
170
- async def derivative(sensor_data):
171
- previous, current = a.tee(sensor_data, n=2)
172
- await a.anext(previous) # advance one iterator
173
- return a.map(operator.sub, previous, current)
174
-
175
- Unlike :py:func:`itertools.tee`, :py:func:`~.tee` returns a custom type instead
176
- of a :py:class:`tuple`. Like a tuple, it can be indexed, iterated and unpacked
177
- to get the child iterators. In addition, its :py:meth:`~.tee.aclose` method
178
- immediately closes all children, and it can be used in an ``async with`` context
166
+ ```python
167
+ async def derivative(sensor_data):
168
+ previous, current = a.tee(sensor_data, n=2)
169
+ await a.anext(previous) # advance one iterator
170
+ return a.map(operator.sub, previous, current)
171
+ ```
172
+
173
+ Unlike `itertools.tee`, `.tee` returns a custom type instead
174
+ of a :py`tuple`. Like a tuple, it can be indexed, iterated and unpacked
175
+ to get the child iterators. In addition, its `.tee.aclose` method
176
+ immediately closes all children, and it can be used in an `async with` context
179
177
  for the same effect.
180
178
 
181
- If ``iterable`` is an iterator and read elsewhere, ``tee`` will *not*
182
- provide these items. Also, ``tee`` must internally buffer each item until the
179
+ If `iterable` is an iterator and read elsewhere, `tee` will *not*
180
+ provide these items. Also, `tee` must internally buffer each item until the
183
181
  last iterator has yielded it; if the most and least advanced iterator differ
184
- by most data, using a :py:class:`list` is more efficient (but not lazy).
182
+ by most data, using a :py`list` is more efficient (but not lazy).
185
183
 
186
- If the underlying iterable is concurrency safe (``anext`` may be awaited
184
+ If the underlying iterable is concurrency safe (`anext` may be awaited
187
185
  concurrently) the resulting iterators are concurrency safe as well. Otherwise,
188
186
  the iterators are safe if there is only ever one single "most advanced" iterator.
189
- To enforce sequential use of ``anext``, provide a ``lock``
190
- - e.g. an :py:class:`asyncio.Lock` instance in an :py:mod:`asyncio` application -
187
+ To enforce sequential use of `anext`, provide a `lock`
188
+ - e.g. an :py`asyncio.Lock` instance in an :py:mod:`asyncio` application -
191
189
  and access is automatically synchronised.
192
190
 
193
191
  """
@@ -197,15 +195,15 @@ class Tee(Generic[T]):
197
195
  iterable: AsyncIterator[T],
198
196
  n: int = 2,
199
197
  *,
200
- lock: Optional[AbstractAsyncContextManager[Any]] = None,
198
+ lock: AbstractAsyncContextManager[Any] | None = None,
201
199
  ):
202
- """Create a ``tee``.
200
+ """Create a `tee`.
203
201
 
204
202
  Args:
205
203
  iterable: The iterable to split.
206
- n: The number of iterators to create. Defaults to 2.
204
+ n: The number of iterators to create.
207
205
  lock: The lock to synchronise access to the shared buffers.
208
- Defaults to None.
206
+
209
207
  """
210
208
  self._iterator = iterable.__aiter__() # before 3.10 aiter() doesn't exist
211
209
  self._buffers: list[deque[T]] = [deque() for _ in range(n)]
@@ -230,8 +228,8 @@ class Tee(Generic[T]):
230
228
  def __getitem__(self, item: slice) -> tuple[AsyncIterator[T], ...]: ...
231
229
 
232
230
  def __getitem__(
233
- self, item: Union[int, slice]
234
- ) -> Union[AsyncIterator[T], tuple[AsyncIterator[T], ...]]:
231
+ self, item: int | slice
232
+ ) -> AsyncIterator[T] | tuple[AsyncIterator[T], ...]:
235
233
  """Return the child iterator(s) for the given index or slice."""
236
234
  return self._children[item]
237
235
 
@@ -249,9 +247,9 @@ class Tee(Generic[T]):
249
247
 
250
248
  async def __aexit__(
251
249
  self,
252
- exc_type: Optional[type[BaseException]],
253
- exc_val: Optional[BaseException],
254
- exc_tb: Optional[TracebackType],
250
+ exc_type: type[BaseException] | None,
251
+ exc_val: BaseException | None,
252
+ exc_tb: TracebackType | None,
255
253
  ) -> bool:
256
254
  """Close all child iterators.
257
255
 
@@ -271,30 +269,28 @@ atee = Tee
271
269
 
272
270
 
273
271
  class aclosing(AbstractAsyncContextManager): # noqa: N801
274
- """Async context manager to wrap an AsyncGenerator that has a ``aclose()`` method.
272
+ """Async context manager to wrap an AsyncGenerator that has a `aclose()` method.
275
273
 
276
274
  Code like this:
277
275
 
278
- .. code-block:: python
279
-
280
- async with aclosing(<module>.fetch(<arguments>)) as agen:
281
- <block>
276
+ ```python
277
+ async with aclosing(<module>.fetch(<arguments>)) as agen:
278
+ <block>
279
+ ```
282
280
 
283
281
  is equivalent to this:
284
282
 
285
- .. code-block:: python
286
-
287
- agen = <module>.fetch(<arguments>)
288
- try:
289
- <block>
290
- finally:
291
- await agen.aclose()
283
+ ```python
284
+ agen = <module>.fetch(<arguments>)
285
+ try:
286
+ <block>
287
+ finally:
288
+ await agen.aclose()
292
289
 
290
+ ```
293
291
  """
294
292
 
295
- def __init__(
296
- self, thing: Union[AsyncGenerator[Any, Any], AsyncIterator[Any]]
297
- ) -> None:
293
+ def __init__(self, thing: AsyncGenerator[Any, Any] | AsyncIterator[Any]) -> None:
298
294
  """Create the context manager.
299
295
 
300
296
  Args:
@@ -303,15 +299,15 @@ class aclosing(AbstractAsyncContextManager): # noqa: N801
303
299
  self.thing = thing
304
300
 
305
301
  @override
306
- async def __aenter__(self) -> Union[AsyncGenerator[Any, Any], AsyncIterator[Any]]:
302
+ async def __aenter__(self) -> AsyncGenerator[Any, Any] | AsyncIterator[Any]:
307
303
  return self.thing
308
304
 
309
305
  @override
310
306
  async def __aexit__(
311
307
  self,
312
- exc_type: Optional[type[BaseException]],
313
- exc_value: Optional[BaseException],
314
- traceback: Optional[TracebackType],
308
+ exc_type: type[BaseException] | None,
309
+ exc_value: BaseException | None,
310
+ traceback: TracebackType | None,
315
311
  ) -> None:
316
312
  if hasattr(self.thing, "aclose"):
317
313
  await self.thing.aclose()
@@ -3,17 +3,17 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import os
6
- from typing import Any, Optional, Union
6
+ from typing import Any
7
7
 
8
8
 
9
9
  def env_var_is_set(env_var: str) -> bool:
10
10
  """Check if an environment variable is set.
11
11
 
12
12
  Args:
13
- env_var (str): The name of the environment variable.
13
+ env_var: The name of the environment variable.
14
14
 
15
15
  Returns:
16
- bool: True if the environment variable is set, False otherwise.
16
+ `True` if the environment variable is set, `False` otherwise.
17
17
  """
18
18
  return env_var in os.environ and os.environ[env_var] not in {
19
19
  "",
@@ -25,9 +25,9 @@ def env_var_is_set(env_var: str) -> bool:
25
25
 
26
26
  def get_from_dict_or_env(
27
27
  data: dict[str, Any],
28
- key: Union[str, list[str]],
28
+ key: str | list[str],
29
29
  env_key: str,
30
- default: Optional[str] = None,
30
+ default: str | None = None,
31
31
  ) -> str:
32
32
  """Get a value from a dictionary or an environment variable.
33
33
 
@@ -38,7 +38,7 @@ def get_from_dict_or_env(
38
38
  env_key: The environment variable to look up if the key is not
39
39
  in the dictionary.
40
40
  default: The default value to return if the key is not in the dictionary
41
- or the environment. Defaults to None.
41
+ or the environment.
42
42
 
43
43
  Returns:
44
44
  The dict value or the environment variable value.
@@ -56,7 +56,7 @@ def get_from_dict_or_env(
56
56
  return get_from_env(key_for_err, env_key, default=default)
57
57
 
58
58
 
59
- def get_from_env(key: str, env_key: str, default: Optional[str] = None) -> str:
59
+ def get_from_env(key: str, env_key: str, default: str | None = None) -> str:
60
60
  """Get a value from a dictionary or an environment variable.
61
61
 
62
62
  Args:
@@ -64,10 +64,10 @@ def get_from_env(key: str, env_key: str, default: Optional[str] = None) -> str:
64
64
  env_key: The environment variable to look up if the key is not
65
65
  in the dictionary.
66
66
  default: The default value to return if the key is not in the dictionary
67
- or the environment. Defaults to None.
67
+ or the environment.
68
68
 
69
69
  Returns:
70
- str: The value of the key.
70
+ The value of the key.
71
71
 
72
72
  Raises:
73
73
  ValueError: If the key is not in the dictionary and no default value is