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
@@ -3,9 +3,7 @@
3
3
  from typing import (
4
4
  TYPE_CHECKING,
5
5
  Any,
6
- Optional,
7
6
  TypeVar,
8
- Union,
9
7
  cast,
10
8
  )
11
9
 
@@ -35,7 +33,7 @@ U = TypeVar("U")
35
33
 
36
34
 
37
35
  class ExponentialJitterParams(TypedDict, total=False):
38
- """Parameters for ``tenacity.wait_exponential_jitter``."""
36
+ """Parameters for `tenacity.wait_exponential_jitter`."""
39
37
 
40
38
  initial: float
41
39
  """Initial wait."""
@@ -62,36 +60,34 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
62
60
  Example:
63
61
  Here's an example that uses a RunnableLambda to raise an exception
64
62
 
65
- .. code-block:: python
63
+ ```python
64
+ import time
66
65
 
67
- import time
68
66
 
67
+ def foo(input) -> None:
68
+ '''Fake function that raises an exception.'''
69
+ raise ValueError(f"Invoking foo failed. At time {time.time()}")
69
70
 
70
- def foo(input) -> None:
71
- '''Fake function that raises an exception.'''
72
- raise ValueError(f"Invoking foo failed. At time {time.time()}")
73
71
 
72
+ runnable = RunnableLambda(foo)
74
73
 
75
- runnable = RunnableLambda(foo)
76
-
77
- runnable_with_retries = runnable.with_retry(
78
- retry_if_exception_type=(ValueError,), # Retry only on ValueError
79
- wait_exponential_jitter=True, # Add jitter to the exponential backoff
80
- stop_after_attempt=2, # Try twice
81
- exponential_jitter_params={
82
- "initial": 2
83
- }, # if desired, customize backoff
84
- )
74
+ runnable_with_retries = runnable.with_retry(
75
+ retry_if_exception_type=(ValueError,), # Retry only on ValueError
76
+ wait_exponential_jitter=True, # Add jitter to the exponential backoff
77
+ stop_after_attempt=2, # Try twice
78
+ exponential_jitter_params={"initial": 2}, # if desired, customize backoff
79
+ )
85
80
 
86
- # The method invocation above is equivalent to the longer form below:
81
+ # The method invocation above is equivalent to the longer form below:
87
82
 
88
- runnable_with_retries = RunnableRetry(
89
- bound=runnable,
90
- retry_exception_types=(ValueError,),
91
- max_attempt_number=2,
92
- wait_exponential_jitter=True,
93
- exponential_jitter_params={"initial": 2},
94
- )
83
+ runnable_with_retries = RunnableRetry(
84
+ bound=runnable,
85
+ retry_exception_types=(ValueError,),
86
+ max_attempt_number=2,
87
+ wait_exponential_jitter=True,
88
+ exponential_jitter_params={"initial": 2},
89
+ )
90
+ ```
95
91
 
96
92
  This logic can be used to retry any Runnable, including a chain of Runnables,
97
93
  but in general it's best practice to keep the scope of the retry as small as
@@ -99,22 +95,20 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
99
95
  the Runnable that is likely to fail, not the entire chain.
100
96
 
101
97
  Example:
98
+ ```python
99
+ from langchain_core.chat_models import ChatOpenAI
100
+ from langchain_core.prompts import PromptTemplate
102
101
 
103
- .. code-block:: python
104
-
105
- from langchain_core.chat_models import ChatOpenAI
106
- from langchain_core.prompts import PromptTemplate
107
-
108
- template = PromptTemplate.from_template("tell me a joke about {topic}.")
109
- model = ChatOpenAI(temperature=0.5)
110
-
111
- # Good
112
- chain = template | model.with_retry()
102
+ template = PromptTemplate.from_template("tell me a joke about {topic}.")
103
+ model = ChatOpenAI(temperature=0.5)
113
104
 
114
- # Bad
115
- chain = template | model
116
- retryable_chain = chain.with_retry()
105
+ # Good
106
+ chain = template | model.with_retry()
117
107
 
108
+ # Bad
109
+ chain = template | model
110
+ retryable_chain = chain.with_retry()
111
+ ```
118
112
  """
119
113
 
120
114
  retry_exception_types: tuple[type[BaseException], ...] = (Exception,)
@@ -130,9 +124,9 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
130
124
  wait_exponential_jitter: bool = True
131
125
  """Whether to add jitter to the exponential backoff."""
132
126
 
133
- exponential_jitter_params: Optional[ExponentialJitterParams] = None
134
- """Parameters for ``tenacity.wait_exponential_jitter``. Namely: ``initial``,
135
- ``max``, ``exp_base``, and ``jitter`` (all float values).
127
+ exponential_jitter_params: ExponentialJitterParams | None = None
128
+ """Parameters for `tenacity.wait_exponential_jitter`. Namely: `initial`,
129
+ `max`, `exp_base`, and `jitter` (all `float` values).
136
130
  """
137
131
 
138
132
  max_attempt_number: int = 3
@@ -178,7 +172,8 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
178
172
  retry_state: RetryCallState,
179
173
  ) -> list[RunnableConfig]:
180
174
  return [
181
- self._patch_config(c, rm, retry_state) for c, rm in zip(config, run_manager)
175
+ self._patch_config(c, rm, retry_state)
176
+ for c, rm in zip(config, run_manager, strict=False)
182
177
  ]
183
178
 
184
179
  def _invoke(
@@ -201,7 +196,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
201
196
 
202
197
  @override
203
198
  def invoke(
204
- self, input: Input, config: Optional[RunnableConfig] = None, **kwargs: Any
199
+ self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
205
200
  ) -> Output:
206
201
  return self._call_with_config(self._invoke, input, config, **kwargs)
207
202
 
@@ -225,7 +220,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
225
220
 
226
221
  @override
227
222
  async def ainvoke(
228
- self, input: Input, config: Optional[RunnableConfig] = None, **kwargs: Any
223
+ self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
229
224
  ) -> Output:
230
225
  return await self._acall_with_config(self._ainvoke, input, config, **kwargs)
231
226
 
@@ -235,7 +230,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
235
230
  run_manager: list["CallbackManagerForChainRun"],
236
231
  config: list[RunnableConfig],
237
232
  **kwargs: Any,
238
- ) -> list[Union[Output, Exception]]:
233
+ ) -> list[Output | Exception]:
239
234
  results_map: dict[int, Output] = {}
240
235
 
241
236
  not_set: list[Output] = []
@@ -284,7 +279,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
284
279
  if result is not_set:
285
280
  result = cast("list[Output]", [e] * len(inputs))
286
281
 
287
- outputs: list[Union[Output, Exception]] = []
282
+ outputs: list[Output | Exception] = []
288
283
  for idx in range(len(inputs)):
289
284
  if idx in results_map:
290
285
  outputs.append(results_map[idx])
@@ -296,7 +291,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
296
291
  def batch(
297
292
  self,
298
293
  inputs: list[Input],
299
- config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
294
+ config: RunnableConfig | list[RunnableConfig] | None = None,
300
295
  *,
301
296
  return_exceptions: bool = False,
302
297
  **kwargs: Any,
@@ -311,7 +306,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
311
306
  run_manager: list["AsyncCallbackManagerForChainRun"],
312
307
  config: list[RunnableConfig],
313
308
  **kwargs: Any,
314
- ) -> list[Union[Output, Exception]]:
309
+ ) -> list[Output | Exception]:
315
310
  results_map: dict[int, Output] = {}
316
311
 
317
312
  not_set: list[Output] = []
@@ -359,7 +354,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
359
354
  if result is not_set:
360
355
  result = cast("list[Output]", [e] * len(inputs))
361
356
 
362
- outputs: list[Union[Output, Exception]] = []
357
+ outputs: list[Output | Exception] = []
363
358
  for idx in range(len(inputs)):
364
359
  if idx in results_map:
365
360
  outputs.append(results_map[idx])
@@ -371,7 +366,7 @@ class RunnableRetry(RunnableBindingBase[Input, Output]): # type: ignore[no-rede
371
366
  async def abatch(
372
367
  self,
373
368
  inputs: list[Input],
374
- config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
369
+ config: RunnableConfig | list[RunnableConfig] | None = None,
375
370
  *,
376
371
  return_exceptions: bool = False,
377
372
  **kwargs: Any,
@@ -2,13 +2,10 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from collections.abc import Mapping
5
+ from collections.abc import Callable, Mapping
6
6
  from typing import (
7
7
  TYPE_CHECKING,
8
8
  Any,
9
- Callable,
10
- Optional,
11
- Union,
12
9
  cast,
13
10
  )
14
11
 
@@ -52,18 +49,16 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
52
49
  Returns the output of the selected Runnable.
53
50
 
54
51
  Example:
52
+ ```python
53
+ from langchain_core.runnables.router import RouterRunnable
54
+ from langchain_core.runnables import RunnableLambda
55
55
 
56
- .. code-block:: python
57
-
58
- from langchain_core.runnables.router import RouterRunnable
59
- from langchain_core.runnables import RunnableLambda
60
-
61
- add = RunnableLambda(func=lambda x: x + 1)
62
- square = RunnableLambda(func=lambda x: x**2)
63
-
64
- router = RouterRunnable(runnables={"add": add, "square": square})
65
- router.invoke({"key": "square", "input": 3})
56
+ add = RunnableLambda(func=lambda x: x + 1)
57
+ square = RunnableLambda(func=lambda x: x**2)
66
58
 
59
+ router = RouterRunnable(runnables={"add": add, "square": square})
60
+ router.invoke({"key": "square", "input": 3})
61
+ ```
67
62
  """
68
63
 
69
64
  runnables: Mapping[str, Runnable[Any, Output]]
@@ -77,7 +72,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
77
72
 
78
73
  def __init__(
79
74
  self,
80
- runnables: Mapping[str, Union[Runnable[Any, Output], Callable[[Any], Output]]],
75
+ runnables: Mapping[str, Runnable[Any, Output] | Callable[[Any], Output]],
81
76
  ) -> None:
82
77
  """Create a RouterRunnable.
83
78
 
@@ -101,16 +96,16 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
101
96
  @classmethod
102
97
  @override
103
98
  def get_lc_namespace(cls) -> list[str]:
104
- """Get the namespace of the langchain object.
99
+ """Get the namespace of the LangChain object.
105
100
 
106
101
  Returns:
107
- ``["langchain", "schema", "runnable"]``
102
+ `["langchain", "schema", "runnable"]`
108
103
  """
109
104
  return ["langchain", "schema", "runnable"]
110
105
 
111
106
  @override
112
107
  def invoke(
113
- self, input: RouterInput, config: Optional[RunnableConfig] = None, **kwargs: Any
108
+ self, input: RouterInput, config: RunnableConfig | None = None, **kwargs: Any
114
109
  ) -> Output:
115
110
  key = input["key"]
116
111
  actual_input = input["input"]
@@ -125,8 +120,8 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
125
120
  async def ainvoke(
126
121
  self,
127
122
  input: RouterInput,
128
- config: Optional[RunnableConfig] = None,
129
- **kwargs: Optional[Any],
123
+ config: RunnableConfig | None = None,
124
+ **kwargs: Any | None,
130
125
  ) -> Output:
131
126
  key = input["key"]
132
127
  actual_input = input["input"]
@@ -141,10 +136,10 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
141
136
  def batch(
142
137
  self,
143
138
  inputs: list[RouterInput],
144
- config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
139
+ config: RunnableConfig | list[RunnableConfig] | None = None,
145
140
  *,
146
141
  return_exceptions: bool = False,
147
- **kwargs: Optional[Any],
142
+ **kwargs: Any | None,
148
143
  ) -> list[Output]:
149
144
  if not inputs:
150
145
  return []
@@ -157,7 +152,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
157
152
 
158
153
  def invoke(
159
154
  runnable: Runnable, input_: Input, config: RunnableConfig
160
- ) -> Union[Output, Exception]:
155
+ ) -> Output | Exception:
161
156
  if return_exceptions:
162
157
  try:
163
158
  return runnable.invoke(input_, config, **kwargs)
@@ -178,10 +173,10 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
178
173
  async def abatch(
179
174
  self,
180
175
  inputs: list[RouterInput],
181
- config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
176
+ config: RunnableConfig | list[RunnableConfig] | None = None,
182
177
  *,
183
178
  return_exceptions: bool = False,
184
- **kwargs: Optional[Any],
179
+ **kwargs: Any | None,
185
180
  ) -> list[Output]:
186
181
  if not inputs:
187
182
  return []
@@ -194,7 +189,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
194
189
 
195
190
  async def ainvoke(
196
191
  runnable: Runnable, input_: Input, config: RunnableConfig
197
- ) -> Union[Output, Exception]:
192
+ ) -> Output | Exception:
198
193
  if return_exceptions:
199
194
  try:
200
195
  return await runnable.ainvoke(input_, config, **kwargs)
@@ -214,8 +209,8 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
214
209
  def stream(
215
210
  self,
216
211
  input: RouterInput,
217
- config: Optional[RunnableConfig] = None,
218
- **kwargs: Optional[Any],
212
+ config: RunnableConfig | None = None,
213
+ **kwargs: Any | None,
219
214
  ) -> Iterator[Output]:
220
215
  key = input["key"]
221
216
  actual_input = input["input"]
@@ -230,8 +225,8 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
230
225
  async def astream(
231
226
  self,
232
227
  input: RouterInput,
233
- config: Optional[RunnableConfig] = None,
234
- **kwargs: Optional[Any],
228
+ config: RunnableConfig | None = None,
229
+ **kwargs: Any | None,
235
230
  ) -> AsyncIterator[Output]:
236
231
  key = input["key"]
237
232
  actual_input = input["input"]
@@ -1,8 +1,8 @@
1
- """Module contains typedefs that are used with Runnables."""
1
+ """Module contains typedefs that are used with `Runnable` objects."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import TYPE_CHECKING, Any, Literal, Union
5
+ from typing import TYPE_CHECKING, Any, Literal
6
6
 
7
7
  from typing_extensions import NotRequired, TypedDict
8
8
 
@@ -14,120 +14,125 @@ class EventData(TypedDict, total=False):
14
14
  """Data associated with a streaming event."""
15
15
 
16
16
  input: Any
17
- """The input passed to the Runnable that generated the event.
17
+ """The input passed to the `Runnable` that generated the event.
18
18
 
19
- Inputs will sometimes be available at the *START* of the Runnable, and
20
- sometimes at the *END* of the Runnable.
19
+ Inputs will sometimes be available at the *START* of the `Runnable`, and
20
+ sometimes at the *END* of the `Runnable`.
21
21
 
22
- If a Runnable is able to stream its inputs, then its input by definition
23
- won't be known until the *END* of the Runnable when it has finished streaming
22
+ If a `Runnable` is able to stream its inputs, then its input by definition
23
+ won't be known until the *END* of the `Runnable` when it has finished streaming
24
24
  its inputs.
25
25
  """
26
+ error: NotRequired[BaseException]
27
+ """The error that occurred during the execution of the `Runnable`.
28
+
29
+ This field is only available if the `Runnable` raised an exception.
30
+
31
+ !!! version-added "Added in version 1.0.0"
32
+ """
26
33
  output: Any
27
- """The output of the Runnable that generated the event.
34
+ """The output of the `Runnable` that generated the event.
28
35
 
29
- Outputs will only be available at the *END* of the Runnable.
36
+ Outputs will only be available at the *END* of the `Runnable`.
30
37
 
31
- For most Runnables, this field can be inferred from the `chunk` field,
32
- though there might be some exceptions for special cased Runnables (e.g., like
38
+ For most `Runnable` objects, this field can be inferred from the `chunk` field,
39
+ though there might be some exceptions for special a cased `Runnable` (e.g., like
33
40
  chat models), which may return more information.
34
41
  """
35
42
  chunk: Any
36
43
  """A streaming chunk from the output that generated the event.
37
44
 
38
45
  chunks support addition in general, and adding them up should result
39
- in the output of the Runnable that generated the event.
46
+ in the output of the `Runnable` that generated the event.
40
47
  """
41
48
 
42
49
 
43
50
  class BaseStreamEvent(TypedDict):
44
51
  """Streaming event.
45
52
 
46
- Schema of a streaming event which is produced from the astream_events method.
53
+ Schema of a streaming event which is produced from the `astream_events` method.
47
54
 
48
55
  Example:
49
-
50
- .. code-block:: python
51
-
52
- from langchain_core.runnables import RunnableLambda
53
-
54
-
55
- async def reverse(s: str) -> str:
56
- return s[::-1]
57
-
58
-
59
- chain = RunnableLambda(func=reverse)
60
-
61
- events = [event async for event in chain.astream_events("hello")]
62
-
63
- # will produce the following events
64
- # (where some fields have been omitted for brevity):
65
- [
66
- {
67
- "data": {"input": "hello"},
68
- "event": "on_chain_start",
69
- "metadata": {},
70
- "name": "reverse",
71
- "tags": [],
72
- },
73
- {
74
- "data": {"chunk": "olleh"},
75
- "event": "on_chain_stream",
76
- "metadata": {},
77
- "name": "reverse",
78
- "tags": [],
79
- },
80
- {
81
- "data": {"output": "olleh"},
82
- "event": "on_chain_end",
83
- "metadata": {},
84
- "name": "reverse",
85
- "tags": [],
86
- },
87
- ]
88
-
56
+ ```python
57
+ from langchain_core.runnables import RunnableLambda
58
+
59
+
60
+ async def reverse(s: str) -> str:
61
+ return s[::-1]
62
+
63
+
64
+ chain = RunnableLambda(func=reverse)
65
+
66
+ events = [event async for event in chain.astream_events("hello")]
67
+
68
+ # Will produce the following events
69
+ # (where some fields have been omitted for brevity):
70
+ [
71
+ {
72
+ "data": {"input": "hello"},
73
+ "event": "on_chain_start",
74
+ "metadata": {},
75
+ "name": "reverse",
76
+ "tags": [],
77
+ },
78
+ {
79
+ "data": {"chunk": "olleh"},
80
+ "event": "on_chain_stream",
81
+ "metadata": {},
82
+ "name": "reverse",
83
+ "tags": [],
84
+ },
85
+ {
86
+ "data": {"output": "olleh"},
87
+ "event": "on_chain_end",
88
+ "metadata": {},
89
+ "name": "reverse",
90
+ "tags": [],
91
+ },
92
+ ]
93
+ ```
89
94
  """
90
95
 
91
96
  event: str
92
- """Event names are of the format: on_[runnable_type]_(start|stream|end).
97
+ """Event names are of the format: `on_[runnable_type]_(start|stream|end)`.
93
98
 
94
99
  Runnable types are one of:
95
100
 
96
101
  - **llm** - used by non chat models
97
102
  - **chat_model** - used by chat models
98
- - **prompt** -- e.g., ChatPromptTemplate
99
- - **tool** -- from tools defined via @tool decorator or inheriting
100
- from Tool/BaseTool
101
- - **chain** - most Runnables are of this type
103
+ - **prompt** -- e.g., `ChatPromptTemplate`
104
+ - **tool** -- from tools defined via `@tool` decorator or inheriting
105
+ from `Tool`/`BaseTool`
106
+ - **chain** - most `Runnable` objects are of this type
102
107
 
103
108
  Further, the events are categorized as one of:
104
109
 
105
- - **start** - when the Runnable starts
106
- - **stream** - when the Runnable is streaming
107
- - **end* - when the Runnable ends
110
+ - **start** - when the `Runnable` starts
111
+ - **stream** - when the `Runnable` is streaming
112
+ - **end* - when the `Runnable` ends
108
113
 
109
114
  start, stream and end are associated with slightly different `data` payload.
110
115
 
111
116
  Please see the documentation for `EventData` for more details.
112
117
  """
113
118
  run_id: str
114
- """An randomly generated ID to keep track of the execution of the given Runnable.
119
+ """An randomly generated ID to keep track of the execution of the given `Runnable`.
115
120
 
116
- Each child Runnable that gets invoked as part of the execution of a parent Runnable
117
- is assigned its own unique ID.
121
+ Each child `Runnable` that gets invoked as part of the execution of a parent
122
+ `Runnable` is assigned its own unique ID.
118
123
  """
119
124
  tags: NotRequired[list[str]]
120
- """Tags associated with the Runnable that generated this event.
125
+ """Tags associated with the `Runnable` that generated this event.
121
126
 
122
- Tags are always inherited from parent Runnables.
127
+ Tags are always inherited from parent `Runnable` objects.
123
128
 
124
- Tags can either be bound to a Runnable using `.with_config({"tags": ["hello"]})`
129
+ Tags can either be bound to a `Runnable` using `.with_config({"tags": ["hello"]})`
125
130
  or passed at run time using `.astream_events(..., {"tags": ["hello"]})`.
126
131
  """
127
132
  metadata: NotRequired[dict[str, Any]]
128
- """Metadata associated with the Runnable that generated this event.
133
+ """Metadata associated with the `Runnable` that generated this event.
129
134
 
130
- Metadata can either be bound to a Runnable using
135
+ Metadata can either be bound to a `Runnable` using
131
136
 
132
137
  `.with_config({"metadata": { "foo": "bar" }})`
133
138
 
@@ -141,8 +146,8 @@ class BaseStreamEvent(TypedDict):
141
146
 
142
147
  Root Events will have an empty list.
143
148
 
144
- For example, if a Runnable A calls Runnable B, then the event generated by Runnable
145
- B will have Runnable A's ID in the parent_ids field.
149
+ For example, if a `Runnable` A calls `Runnable` B, then the event generated by
150
+ `Runnable` B will have `Runnable` A's ID in the `parent_ids` field.
146
151
 
147
152
  The order of the parent IDs is from the root parent to the immediate parent.
148
153
 
@@ -159,13 +164,13 @@ class StandardStreamEvent(BaseStreamEvent):
159
164
  The contents of the event data depend on the event type.
160
165
  """
161
166
  name: str
162
- """The name of the Runnable that generated the event."""
167
+ """The name of the `Runnable` that generated the event."""
163
168
 
164
169
 
165
170
  class CustomStreamEvent(BaseStreamEvent):
166
171
  """Custom stream event created by the user.
167
172
 
168
- .. versionadded:: 0.2.15
173
+ !!! version-added "Added in version 0.2.15"
169
174
  """
170
175
 
171
176
  # Overwrite the event field to be more specific.
@@ -177,4 +182,4 @@ class CustomStreamEvent(BaseStreamEvent):
177
182
  """The data associated with the event. Free form and can be anything."""
178
183
 
179
184
 
180
- StreamEvent = Union[StandardStreamEvent, CustomStreamEvent]
185
+ StreamEvent = StandardStreamEvent | CustomStreamEvent