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
@@ -8,10 +8,11 @@ import functools
8
8
  import logging
9
9
  import uuid
10
10
  from abc import ABC, abstractmethod
11
+ from collections.abc import Callable
11
12
  from concurrent.futures import ThreadPoolExecutor
12
13
  from contextlib import asynccontextmanager, contextmanager
13
14
  from contextvars import copy_context
14
- from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, cast
15
+ from typing import TYPE_CHECKING, Any, TypeVar, cast
15
16
  from uuid import UUID
16
17
 
17
18
  from langsmith.run_helpers import get_tracing_context
@@ -62,14 +63,14 @@ def _get_debug() -> bool:
62
63
  @contextmanager
63
64
  def trace_as_chain_group(
64
65
  group_name: str,
65
- callback_manager: Optional[CallbackManager] = None,
66
+ callback_manager: CallbackManager | None = None,
66
67
  *,
67
- inputs: Optional[dict[str, Any]] = None,
68
- project_name: Optional[str] = None,
69
- example_id: Optional[Union[str, UUID]] = None,
70
- run_id: Optional[UUID] = None,
71
- tags: Optional[list[str]] = None,
72
- metadata: Optional[dict[str, Any]] = None,
68
+ inputs: dict[str, Any] | None = None,
69
+ project_name: str | None = None,
70
+ example_id: str | UUID | None = None,
71
+ run_id: UUID | None = None,
72
+ tags: list[str] | None = None,
73
+ metadata: dict[str, Any] | None = None,
73
74
  ) -> Generator[CallbackManagerForChainGroup, None, None]:
74
75
  """Get a callback manager for a chain group in a context manager.
75
76
 
@@ -77,39 +78,30 @@ def trace_as_chain_group(
77
78
  they aren't composed in a single chain.
78
79
 
79
80
  Args:
80
- group_name (str): The name of the chain group.
81
- callback_manager (CallbackManager, optional): The callback manager to use.
82
- Defaults to None.
83
- inputs (dict[str, Any], optional): The inputs to the chain group.
84
- Defaults to None.
85
- project_name (str, optional): The name of the project.
86
- Defaults to None.
87
- example_id (str or UUID, optional): The ID of the example.
88
- Defaults to None.
89
- run_id (UUID, optional): The ID of the run.
90
- tags (list[str], optional): The inheritable tags to apply to all runs.
91
- Defaults to None.
92
- metadata (dict[str, Any], optional): The metadata to apply to all runs.
93
- Defaults to None.
94
-
95
- .. note::
96
- Must have ``LANGCHAIN_TRACING_V2`` env var set to true to see the trace in
81
+ group_name: The name of the chain group.
82
+ callback_manager: The callback manager to use.
83
+ inputs: The inputs to the chain group.
84
+ project_name: The name of the project.
85
+ example_id: The ID of the example.
86
+ run_id: The ID of the run.
87
+ tags: The inheritable tags to apply to all runs.
88
+ metadata: The metadata to apply to all runs.
89
+
90
+ !!! note
91
+ Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
97
92
  LangSmith.
98
93
 
99
94
  Yields:
100
95
  The callback manager for the chain group.
101
96
 
102
97
  Example:
103
- .. code-block:: python
104
-
105
- llm_input = "Foo"
106
- with trace_as_chain_group(
107
- "group_name", inputs={"input": llm_input}
108
- ) as manager:
109
- # Use the callback manager for the chain group
110
- res = llm.invoke(llm_input, {"callbacks": manager})
111
- manager.on_chain_end({"output": res})
112
-
98
+ ```python
99
+ llm_input = "Foo"
100
+ with trace_as_chain_group("group_name", inputs={"input": llm_input}) as manager:
101
+ # Use the callback manager for the chain group
102
+ res = llm.invoke(llm_input, {"callbacks": manager})
103
+ manager.on_chain_end({"output": res})
104
+ ```
113
105
  """
114
106
  cb = _get_trace_callbacks(
115
107
  project_name, example_id, callback_manager=callback_manager
@@ -146,14 +138,14 @@ def trace_as_chain_group(
146
138
  @asynccontextmanager
147
139
  async def atrace_as_chain_group(
148
140
  group_name: str,
149
- callback_manager: Optional[AsyncCallbackManager] = None,
141
+ callback_manager: AsyncCallbackManager | None = None,
150
142
  *,
151
- inputs: Optional[dict[str, Any]] = None,
152
- project_name: Optional[str] = None,
153
- example_id: Optional[Union[str, UUID]] = None,
154
- run_id: Optional[UUID] = None,
155
- tags: Optional[list[str]] = None,
156
- metadata: Optional[dict[str, Any]] = None,
143
+ inputs: dict[str, Any] | None = None,
144
+ project_name: str | None = None,
145
+ example_id: str | UUID | None = None,
146
+ run_id: UUID | None = None,
147
+ tags: list[str] | None = None,
148
+ metadata: dict[str, Any] | None = None,
157
149
  ) -> AsyncGenerator[AsyncCallbackManagerForChainGroup, None]:
158
150
  """Get an async callback manager for a chain group in a context manager.
159
151
 
@@ -161,39 +153,33 @@ async def atrace_as_chain_group(
161
153
  they aren't composed in a single chain.
162
154
 
163
155
  Args:
164
- group_name (str): The name of the chain group.
165
- callback_manager (AsyncCallbackManager, optional): The async callback manager
166
- to use, which manages tracing and other callback behavior. Defaults to None.
167
- inputs (dict[str, Any], optional): The inputs to the chain group.
168
- Defaults to None.
169
- project_name (str, optional): The name of the project.
170
- Defaults to None.
171
- example_id (str or UUID, optional): The ID of the example.
172
- Defaults to None.
173
- run_id (UUID, optional): The ID of the run.
174
- tags (list[str], optional): The inheritable tags to apply to all runs.
175
- Defaults to None.
176
- metadata (dict[str, Any], optional): The metadata to apply to all runs.
177
- Defaults to None.
156
+ group_name: The name of the chain group.
157
+ callback_manager: The async callback manager to use,
158
+ which manages tracing and other callback behavior.
159
+ inputs: The inputs to the chain group.
160
+ project_name: The name of the project.
161
+ example_id: The ID of the example.
162
+ run_id: The ID of the run.
163
+ tags: The inheritable tags to apply to all runs.
164
+ metadata: The metadata to apply to all runs.
178
165
 
179
166
  Yields:
180
167
  The async callback manager for the chain group.
181
168
 
182
- .. note::
183
- Must have ``LANGCHAIN_TRACING_V2`` env var set to true to see the trace in
169
+ !!! note
170
+ Must have `LANGCHAIN_TRACING_V2` env var set to true to see the trace in
184
171
  LangSmith.
185
172
 
186
173
  Example:
187
- .. code-block:: python
188
-
189
- llm_input = "Foo"
190
- async with atrace_as_chain_group(
191
- "group_name", inputs={"input": llm_input}
192
- ) as manager:
193
- # Use the async callback manager for the chain group
194
- res = await llm.ainvoke(llm_input, {"callbacks": manager})
195
- await manager.on_chain_end({"output": res})
196
-
174
+ ```python
175
+ llm_input = "Foo"
176
+ async with atrace_as_chain_group(
177
+ "group_name", inputs={"input": llm_input}
178
+ ) as manager:
179
+ # Use the async callback manager for the chain group
180
+ res = await llm.ainvoke(llm_input, {"callbacks": manager})
181
+ await manager.on_chain_end({"output": res})
182
+ ```
197
183
  """
198
184
  cb = _get_trace_callbacks(
199
185
  project_name, example_id, callback_manager=callback_manager
@@ -234,10 +220,10 @@ def shielded(func: Func) -> Func:
234
220
  """Makes so an awaitable method is always shielded from cancellation.
235
221
 
236
222
  Args:
237
- func (Callable): The function to shield.
223
+ func: The function to shield.
238
224
 
239
225
  Returns:
240
- Callable: The shielded function
226
+ The shielded function
241
227
 
242
228
  """
243
229
 
@@ -251,18 +237,18 @@ def shielded(func: Func) -> Func:
251
237
  def handle_event(
252
238
  handlers: list[BaseCallbackHandler],
253
239
  event_name: str,
254
- ignore_condition_name: Optional[str],
240
+ ignore_condition_name: str | None,
255
241
  *args: Any,
256
242
  **kwargs: Any,
257
243
  ) -> None:
258
244
  """Generic event handler for CallbackManager.
259
245
 
260
- .. note::
261
- This function is used by ``LangServe`` to handle events.
246
+ !!! note
247
+ This function is used by `LangServe` to handle events.
262
248
 
263
249
  Args:
264
250
  handlers: The list of handlers that will handle the event.
265
- event_name: The name of the event (e.g., ``'on_llm_start'``).
251
+ event_name: The name of the event (e.g., `'on_llm_start'`).
266
252
  ignore_condition_name: Name of the attribute defined on handler
267
253
  that if True will cause the handler to be skipped for the given event.
268
254
  *args: The arguments to pass to the event handler.
@@ -272,7 +258,7 @@ def handle_event(
272
258
  coros: list[Coroutine[Any, Any, Any]] = []
273
259
 
274
260
  try:
275
- message_strings: Optional[list[str]] = None
261
+ message_strings: list[str] | None = None
276
262
  for handler in handlers:
277
263
  try:
278
264
  if ignore_condition_name is None or not getattr(
@@ -366,7 +352,7 @@ def _run_coros(coros: list[Coroutine[Any, Any, Any]]) -> None:
366
352
  async def _ahandle_event_for_handler(
367
353
  handler: BaseCallbackHandler,
368
354
  event_name: str,
369
- ignore_condition_name: Optional[str],
355
+ ignore_condition_name: str | None,
370
356
  *args: Any,
371
357
  **kwargs: Any,
372
358
  ) -> None:
@@ -418,18 +404,18 @@ async def _ahandle_event_for_handler(
418
404
  async def ahandle_event(
419
405
  handlers: list[BaseCallbackHandler],
420
406
  event_name: str,
421
- ignore_condition_name: Optional[str],
407
+ ignore_condition_name: str | None,
422
408
  *args: Any,
423
409
  **kwargs: Any,
424
410
  ) -> None:
425
- """Async generic event handler for ``AsyncCallbackManager``.
411
+ """Async generic event handler for `AsyncCallbackManager`.
426
412
 
427
- .. note::
428
- This function is used by ``LangServe`` to handle events.
413
+ !!! note
414
+ This function is used by `LangServe` to handle events.
429
415
 
430
416
  Args:
431
417
  handlers: The list of handlers that will handle the event.
432
- event_name: The name of the event (e.g., ``'on_llm_start'``).
418
+ event_name: The name of the event (e.g., `'on_llm_start'`).
433
419
  ignore_condition_name: Name of the attribute defined on handler
434
420
  that if True will cause the handler to be skipped for the given event.
435
421
  *args: The arguments to pass to the event handler.
@@ -464,28 +450,23 @@ class BaseRunManager(RunManagerMixin):
464
450
  run_id: UUID,
465
451
  handlers: list[BaseCallbackHandler],
466
452
  inheritable_handlers: list[BaseCallbackHandler],
467
- parent_run_id: Optional[UUID] = None,
468
- tags: Optional[list[str]] = None,
469
- inheritable_tags: Optional[list[str]] = None,
470
- metadata: Optional[dict[str, Any]] = None,
471
- inheritable_metadata: Optional[dict[str, Any]] = None,
453
+ parent_run_id: UUID | None = None,
454
+ tags: list[str] | None = None,
455
+ inheritable_tags: list[str] | None = None,
456
+ metadata: dict[str, Any] | None = None,
457
+ inheritable_metadata: dict[str, Any] | None = None,
472
458
  ) -> None:
473
459
  """Initialize the run manager.
474
460
 
475
461
  Args:
476
- run_id (UUID): The ID of the run.
477
- handlers (list[BaseCallbackHandler]): The list of handlers.
478
- inheritable_handlers (list[BaseCallbackHandler]):
479
- The list of inheritable handlers.
480
- parent_run_id (UUID, optional): The ID of the parent run.
481
- Defaults to None.
482
- tags (Optional[list[str]]): The list of tags. Defaults to None.
483
- inheritable_tags (Optional[list[str]]): The list of inheritable tags.
484
- Defaults to None.
485
- metadata (Optional[dict[str, Any]]): The metadata.
486
- Defaults to None.
487
- inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
488
- Defaults to None.
462
+ run_id: The ID of the run.
463
+ handlers: The list of handlers.
464
+ inheritable_handlers: The list of inheritable handlers.
465
+ parent_run_id: The ID of the parent run.
466
+ tags: The list of tags.
467
+ inheritable_tags: The list of inheritable tags.
468
+ metadata: The metadata.
469
+ inheritable_metadata: The inheritable metadata.
489
470
 
490
471
  """
491
472
  self.run_id = run_id
@@ -502,7 +483,7 @@ class BaseRunManager(RunManagerMixin):
502
483
  """Return a manager that doesn't perform any operations.
503
484
 
504
485
  Returns:
505
- BaseRunManager: The noop manager.
486
+ The noop manager.
506
487
 
507
488
  """
508
489
  return cls(
@@ -527,8 +508,8 @@ class RunManager(BaseRunManager):
527
508
  """Run when a text is received.
528
509
 
529
510
  Args:
530
- text (str): The received text.
531
- **kwargs (Any): Additional keyword arguments.
511
+ text: The received text.
512
+ **kwargs: Additional keyword arguments.
532
513
  """
533
514
  if not self.handlers:
534
515
  return
@@ -551,8 +532,8 @@ class RunManager(BaseRunManager):
551
532
  """Run when a retry is received.
552
533
 
553
534
  Args:
554
- retry_state (RetryCallState): The retry state.
555
- **kwargs (Any): Additional keyword arguments.
535
+ retry_state: The retry state.
536
+ **kwargs: Additional keyword arguments.
556
537
 
557
538
  """
558
539
  if not self.handlers:
@@ -572,15 +553,14 @@ class RunManager(BaseRunManager):
572
553
  class ParentRunManager(RunManager):
573
554
  """Sync Parent Run Manager."""
574
555
 
575
- def get_child(self, tag: Optional[str] = None) -> CallbackManager:
556
+ def get_child(self, tag: str | None = None) -> CallbackManager:
576
557
  """Get a child callback manager.
577
558
 
578
559
  Args:
579
- tag (str, optional): The tag for the child callback manager.
580
- Defaults to None.
560
+ tag: The tag for the child callback manager.
581
561
 
582
562
  Returns:
583
- CallbackManager: The child callback manager.
563
+ The child callback manager.
584
564
 
585
565
  """
586
566
  manager = CallbackManager(handlers=[], parent_run_id=self.run_id)
@@ -600,7 +580,7 @@ class AsyncRunManager(BaseRunManager, ABC):
600
580
  """Get the equivalent sync RunManager.
601
581
 
602
582
  Returns:
603
- RunManager: The sync RunManager.
583
+ The sync RunManager.
604
584
 
605
585
  """
606
586
 
@@ -612,8 +592,8 @@ class AsyncRunManager(BaseRunManager, ABC):
612
592
  """Run when a text is received.
613
593
 
614
594
  Args:
615
- text (str): The received text.
616
- **kwargs (Any): Additional keyword arguments.
595
+ text: The received text.
596
+ **kwargs: Additional keyword arguments.
617
597
  """
618
598
  if not self.handlers:
619
599
  return
@@ -636,8 +616,8 @@ class AsyncRunManager(BaseRunManager, ABC):
636
616
  """Async run when a retry is received.
637
617
 
638
618
  Args:
639
- retry_state (RetryCallState): The retry state.
640
- **kwargs (Any): Additional keyword arguments.
619
+ retry_state: The retry state.
620
+ **kwargs: Additional keyword arguments.
641
621
 
642
622
  """
643
623
  if not self.handlers:
@@ -657,15 +637,14 @@ class AsyncRunManager(BaseRunManager, ABC):
657
637
  class AsyncParentRunManager(AsyncRunManager):
658
638
  """Async Parent Run Manager."""
659
639
 
660
- def get_child(self, tag: Optional[str] = None) -> AsyncCallbackManager:
640
+ def get_child(self, tag: str | None = None) -> AsyncCallbackManager:
661
641
  """Get a child callback manager.
662
642
 
663
643
  Args:
664
- tag (str, optional): The tag for the child callback manager.
665
- Defaults to None.
644
+ tag: The tag for the child callback manager.
666
645
 
667
646
  Returns:
668
- AsyncCallbackManager: The child callback manager.
647
+ The child callback manager.
669
648
 
670
649
  """
671
650
  manager = AsyncCallbackManager(handlers=[], parent_run_id=self.run_id)
@@ -684,16 +663,15 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
684
663
  self,
685
664
  token: str,
686
665
  *,
687
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
666
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
688
667
  **kwargs: Any,
689
668
  ) -> None:
690
669
  """Run when LLM generates a new token.
691
670
 
692
671
  Args:
693
- token (str): The new token.
694
- chunk (Optional[Union[GenerationChunk, ChatGenerationChunk]], optional):
695
- The chunk. Defaults to None.
696
- **kwargs (Any): Additional keyword arguments.
672
+ token: The new token.
673
+ chunk: The chunk.
674
+ **kwargs: Additional keyword arguments.
697
675
 
698
676
  """
699
677
  if not self.handlers:
@@ -714,8 +692,8 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
714
692
  """Run when LLM ends running.
715
693
 
716
694
  Args:
717
- response (LLMResult): The LLM result.
718
- **kwargs (Any): Additional keyword arguments.
695
+ response: The LLM result.
696
+ **kwargs: Additional keyword arguments.
719
697
 
720
698
  """
721
699
  if not self.handlers:
@@ -739,8 +717,8 @@ class CallbackManagerForLLMRun(RunManager, LLMManagerMixin):
739
717
  """Run when LLM errors.
740
718
 
741
719
  Args:
742
- error (Exception or KeyboardInterrupt): The error.
743
- kwargs (Any): Additional keyword arguments.
720
+ error: The error.
721
+ **kwargs: Additional keyword arguments.
744
722
  - response (LLMResult): The response which was generated before
745
723
  the error occurred.
746
724
  """
@@ -765,7 +743,7 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
765
743
  """Get the equivalent sync RunManager.
766
744
 
767
745
  Returns:
768
- CallbackManagerForLLMRun: The sync RunManager.
746
+ The sync RunManager.
769
747
 
770
748
  """
771
749
  return CallbackManagerForLLMRun(
@@ -783,16 +761,15 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
783
761
  self,
784
762
  token: str,
785
763
  *,
786
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
764
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
787
765
  **kwargs: Any,
788
766
  ) -> None:
789
767
  """Run when LLM generates a new token.
790
768
 
791
769
  Args:
792
- token (str): The new token.
793
- chunk (Optional[Union[GenerationChunk, ChatGenerationChunk]], optional):
794
- The chunk. Defaults to None.
795
- **kwargs (Any): Additional keyword arguments.
770
+ token: The new token.
771
+ chunk: The chunk.
772
+ **kwargs: Additional keyword arguments.
796
773
 
797
774
  """
798
775
  if not self.handlers:
@@ -814,8 +791,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
814
791
  """Run when LLM ends running.
815
792
 
816
793
  Args:
817
- response (LLMResult): The LLM result.
818
- **kwargs (Any): Additional keyword arguments.
794
+ response: The LLM result.
795
+ **kwargs: Additional keyword arguments.
819
796
 
820
797
  """
821
798
  if not self.handlers:
@@ -840,8 +817,8 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
840
817
  """Run when LLM errors.
841
818
 
842
819
  Args:
843
- error (Exception or KeyboardInterrupt): The error.
844
- kwargs (Any): Additional keyword arguments.
820
+ error: The error.
821
+ **kwargs: Additional keyword arguments.
845
822
  - response (LLMResult): The response which was generated before
846
823
  the error occurred.
847
824
 
@@ -865,12 +842,12 @@ class AsyncCallbackManagerForLLMRun(AsyncRunManager, LLMManagerMixin):
865
842
  class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
866
843
  """Callback manager for chain run."""
867
844
 
868
- def on_chain_end(self, outputs: Union[dict[str, Any], Any], **kwargs: Any) -> None:
845
+ def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
869
846
  """Run when chain ends running.
870
847
 
871
848
  Args:
872
- outputs (Union[dict[str, Any], Any]): The outputs of the chain.
873
- **kwargs (Any): Additional keyword arguments.
849
+ outputs: The outputs of the chain.
850
+ **kwargs: Additional keyword arguments.
874
851
 
875
852
  """
876
853
  if not self.handlers:
@@ -894,8 +871,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
894
871
  """Run when chain errors.
895
872
 
896
873
  Args:
897
- error (Exception or KeyboardInterrupt): The error.
898
- **kwargs (Any): Additional keyword arguments.
874
+ error: The error.
875
+ **kwargs: Additional keyword arguments.
899
876
 
900
877
  """
901
878
  if not self.handlers:
@@ -915,8 +892,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
915
892
  """Run when agent action is received.
916
893
 
917
894
  Args:
918
- action (AgentAction): The agent action.
919
- **kwargs (Any): Additional keyword arguments.
895
+ action: The agent action.
896
+ **kwargs: Additional keyword arguments.
920
897
  """
921
898
  if not self.handlers:
922
899
  return
@@ -935,8 +912,8 @@ class CallbackManagerForChainRun(ParentRunManager, ChainManagerMixin):
935
912
  """Run when agent finish is received.
936
913
 
937
914
  Args:
938
- finish (AgentFinish): The agent finish.
939
- **kwargs (Any): Additional keyword arguments.
915
+ finish: The agent finish.
916
+ **kwargs: Additional keyword arguments.
940
917
  """
941
918
  if not self.handlers:
942
919
  return
@@ -959,7 +936,7 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
959
936
  """Get the equivalent sync RunManager.
960
937
 
961
938
  Returns:
962
- CallbackManagerForChainRun: The sync RunManager.
939
+ The sync RunManager.
963
940
  """
964
941
  return CallbackManagerForChainRun(
965
942
  run_id=self.run_id,
@@ -973,14 +950,12 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
973
950
  )
974
951
 
975
952
  @shielded
976
- async def on_chain_end(
977
- self, outputs: Union[dict[str, Any], Any], **kwargs: Any
978
- ) -> None:
953
+ async def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
979
954
  """Run when a chain ends running.
980
955
 
981
956
  Args:
982
- outputs (Union[dict[str, Any], Any]): The outputs of the chain.
983
- **kwargs (Any): Additional keyword arguments.
957
+ outputs: The outputs of the chain.
958
+ **kwargs: Additional keyword arguments.
984
959
 
985
960
  """
986
961
  if not self.handlers:
@@ -1005,8 +980,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
1005
980
  """Run when chain errors.
1006
981
 
1007
982
  Args:
1008
- error (Exception or KeyboardInterrupt): The error.
1009
- **kwargs (Any): Additional keyword arguments.
983
+ error: The error.
984
+ **kwargs: Additional keyword arguments.
1010
985
 
1011
986
  """
1012
987
  if not self.handlers:
@@ -1026,8 +1001,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
1026
1001
  """Run when agent action is received.
1027
1002
 
1028
1003
  Args:
1029
- action (AgentAction): The agent action.
1030
- **kwargs (Any): Additional keyword arguments.
1004
+ action: The agent action.
1005
+ **kwargs: Additional keyword arguments.
1031
1006
  """
1032
1007
  if not self.handlers:
1033
1008
  return
@@ -1046,8 +1021,8 @@ class AsyncCallbackManagerForChainRun(AsyncParentRunManager, ChainManagerMixin):
1046
1021
  """Run when agent finish is received.
1047
1022
 
1048
1023
  Args:
1049
- finish (AgentFinish): The agent finish.
1050
- **kwargs (Any): Additional keyword arguments.
1024
+ finish: The agent finish.
1025
+ **kwargs: Additional keyword arguments.
1051
1026
  """
1052
1027
  if not self.handlers:
1053
1028
  return
@@ -1074,8 +1049,8 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
1074
1049
  """Run when the tool ends running.
1075
1050
 
1076
1051
  Args:
1077
- output (Any): The output of the tool.
1078
- **kwargs (Any): The keyword arguments to pass to the event handler
1052
+ output: The output of the tool.
1053
+ **kwargs: The keyword arguments to pass to the event handler
1079
1054
 
1080
1055
  """
1081
1056
  if not self.handlers:
@@ -1099,8 +1074,8 @@ class CallbackManagerForToolRun(ParentRunManager, ToolManagerMixin):
1099
1074
  """Run when tool errors.
1100
1075
 
1101
1076
  Args:
1102
- error (Exception or KeyboardInterrupt): The error.
1103
- **kwargs (Any): Additional keyword arguments.
1077
+ error: The error.
1078
+ **kwargs: Additional keyword arguments.
1104
1079
 
1105
1080
  """
1106
1081
  if not self.handlers:
@@ -1124,7 +1099,7 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
1124
1099
  """Get the equivalent sync RunManager.
1125
1100
 
1126
1101
  Returns:
1127
- CallbackManagerForToolRun: The sync RunManager.
1102
+ The sync RunManager.
1128
1103
  """
1129
1104
  return CallbackManagerForToolRun(
1130
1105
  run_id=self.run_id,
@@ -1141,8 +1116,8 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
1141
1116
  """Async run when the tool ends running.
1142
1117
 
1143
1118
  Args:
1144
- output (Any): The output of the tool.
1145
- **kwargs (Any): Additional keyword arguments.
1119
+ output: The output of the tool.
1120
+ **kwargs: Additional keyword arguments.
1146
1121
 
1147
1122
  """
1148
1123
  if not self.handlers:
@@ -1166,8 +1141,8 @@ class AsyncCallbackManagerForToolRun(AsyncParentRunManager, ToolManagerMixin):
1166
1141
  """Run when tool errors.
1167
1142
 
1168
1143
  Args:
1169
- error (Exception or KeyboardInterrupt): The error.
1170
- **kwargs (Any): Additional keyword arguments.
1144
+ error: The error.
1145
+ **kwargs: Additional keyword arguments.
1171
1146
 
1172
1147
  """
1173
1148
  if not self.handlers:
@@ -1195,8 +1170,8 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
1195
1170
  """Run when retriever ends running.
1196
1171
 
1197
1172
  Args:
1198
- documents (Sequence[Document]): The retrieved documents.
1199
- **kwargs (Any): Additional keyword arguments.
1173
+ documents: The retrieved documents.
1174
+ **kwargs: Additional keyword arguments.
1200
1175
 
1201
1176
  """
1202
1177
  if not self.handlers:
@@ -1220,8 +1195,8 @@ class CallbackManagerForRetrieverRun(ParentRunManager, RetrieverManagerMixin):
1220
1195
  """Run when retriever errors.
1221
1196
 
1222
1197
  Args:
1223
- error (BaseException): The error.
1224
- **kwargs (Any): Additional keyword arguments.
1198
+ error: The error.
1199
+ **kwargs: Additional keyword arguments.
1225
1200
 
1226
1201
  """
1227
1202
  if not self.handlers:
@@ -1248,7 +1223,7 @@ class AsyncCallbackManagerForRetrieverRun(
1248
1223
  """Get the equivalent sync RunManager.
1249
1224
 
1250
1225
  Returns:
1251
- CallbackManagerForRetrieverRun: The sync RunManager.
1226
+ The sync RunManager.
1252
1227
 
1253
1228
  """
1254
1229
  return CallbackManagerForRetrieverRun(
@@ -1269,8 +1244,8 @@ class AsyncCallbackManagerForRetrieverRun(
1269
1244
  """Run when the retriever ends running.
1270
1245
 
1271
1246
  Args:
1272
- documents (Sequence[Document]): The retrieved documents.
1273
- **kwargs (Any): Additional keyword arguments.
1247
+ documents: The retrieved documents.
1248
+ **kwargs: Additional keyword arguments.
1274
1249
 
1275
1250
  """
1276
1251
  if not self.handlers:
@@ -1295,8 +1270,8 @@ class AsyncCallbackManagerForRetrieverRun(
1295
1270
  """Run when retriever errors.
1296
1271
 
1297
1272
  Args:
1298
- error (BaseException): The error.
1299
- **kwargs (Any): Additional keyword arguments.
1273
+ error: The error.
1274
+ **kwargs: Additional keyword arguments.
1300
1275
 
1301
1276
  """
1302
1277
  if not self.handlers:
@@ -1320,20 +1295,19 @@ class CallbackManager(BaseCallbackManager):
1320
1295
  self,
1321
1296
  serialized: dict[str, Any],
1322
1297
  prompts: list[str],
1323
- run_id: Optional[UUID] = None,
1298
+ run_id: UUID | None = None,
1324
1299
  **kwargs: Any,
1325
1300
  ) -> list[CallbackManagerForLLMRun]:
1326
1301
  """Run when LLM starts running.
1327
1302
 
1328
1303
  Args:
1329
- serialized (dict[str, Any]): The serialized LLM.
1330
- prompts (list[str]): The list of prompts.
1331
- run_id (UUID, optional): The ID of the run. Defaults to None.
1332
- **kwargs (Any): Additional keyword arguments.
1304
+ serialized: The serialized LLM.
1305
+ prompts: The list of prompts.
1306
+ run_id: The ID of the run.
1307
+ **kwargs: Additional keyword arguments.
1333
1308
 
1334
1309
  Returns:
1335
- list[CallbackManagerForLLMRun]: A callback manager for each
1336
- prompt as an LLM run.
1310
+ A callback manager for each prompt as an LLM run.
1337
1311
 
1338
1312
  """
1339
1313
  managers = []
@@ -1372,20 +1346,19 @@ class CallbackManager(BaseCallbackManager):
1372
1346
  self,
1373
1347
  serialized: dict[str, Any],
1374
1348
  messages: list[list[BaseMessage]],
1375
- run_id: Optional[UUID] = None,
1349
+ run_id: UUID | None = None,
1376
1350
  **kwargs: Any,
1377
1351
  ) -> list[CallbackManagerForLLMRun]:
1378
1352
  """Run when chat model starts running.
1379
1353
 
1380
1354
  Args:
1381
- serialized (dict[str, Any]): The serialized LLM.
1382
- messages (list[list[BaseMessage]]): The list of messages.
1383
- run_id (UUID, optional): The ID of the run. Defaults to None.
1384
- **kwargs (Any): Additional keyword arguments.
1355
+ serialized: The serialized LLM.
1356
+ messages: The list of messages.
1357
+ run_id: The ID of the run.
1358
+ **kwargs: Additional keyword arguments.
1385
1359
 
1386
1360
  Returns:
1387
- list[CallbackManagerForLLMRun]: A callback manager for each
1388
- list of messages as an LLM run.
1361
+ A callback manager for each list of messages as an LLM run.
1389
1362
 
1390
1363
  """
1391
1364
  managers = []
@@ -1425,21 +1398,21 @@ class CallbackManager(BaseCallbackManager):
1425
1398
 
1426
1399
  def on_chain_start(
1427
1400
  self,
1428
- serialized: Optional[dict[str, Any]],
1429
- inputs: Union[dict[str, Any], Any],
1430
- run_id: Optional[UUID] = None,
1401
+ serialized: dict[str, Any] | None,
1402
+ inputs: dict[str, Any] | Any,
1403
+ run_id: UUID | None = None,
1431
1404
  **kwargs: Any,
1432
1405
  ) -> CallbackManagerForChainRun:
1433
1406
  """Run when chain starts running.
1434
1407
 
1435
1408
  Args:
1436
- serialized (Optional[dict[str, Any]]): The serialized chain.
1437
- inputs (Union[dict[str, Any], Any]): The inputs to the chain.
1438
- run_id (UUID, optional): The ID of the run. Defaults to None.
1439
- **kwargs (Any): Additional keyword arguments.
1409
+ serialized: The serialized chain.
1410
+ inputs: The inputs to the chain.
1411
+ run_id: The ID of the run.
1412
+ **kwargs: Additional keyword arguments.
1440
1413
 
1441
1414
  Returns:
1442
- CallbackManagerForChainRun: The callback manager for the chain run.
1415
+ The callback manager for the chain run.
1443
1416
 
1444
1417
  """
1445
1418
  if run_id is None:
@@ -1471,11 +1444,11 @@ class CallbackManager(BaseCallbackManager):
1471
1444
  @override
1472
1445
  def on_tool_start(
1473
1446
  self,
1474
- serialized: Optional[dict[str, Any]],
1447
+ serialized: dict[str, Any] | None,
1475
1448
  input_str: str,
1476
- run_id: Optional[UUID] = None,
1477
- parent_run_id: Optional[UUID] = None,
1478
- inputs: Optional[dict[str, Any]] = None,
1449
+ run_id: UUID | None = None,
1450
+ parent_run_id: UUID | None = None,
1451
+ inputs: dict[str, Any] | None = None,
1479
1452
  **kwargs: Any,
1480
1453
  ) -> CallbackManagerForToolRun:
1481
1454
  """Run when tool starts running.
@@ -1484,17 +1457,17 @@ class CallbackManager(BaseCallbackManager):
1484
1457
  serialized: Serialized representation of the tool.
1485
1458
  input_str: The input to the tool as a string.
1486
1459
  Non-string inputs are cast to strings.
1487
- run_id: ID for the run. Defaults to None.
1488
- parent_run_id: The ID of the parent run. Defaults to None.
1460
+ run_id: ID for the run.
1461
+ parent_run_id: The ID of the parent run.
1489
1462
  inputs: The original input to the tool if provided.
1490
1463
  Recommended for usage instead of input_str when the original
1491
1464
  input is needed.
1492
1465
  If provided, the inputs are expected to be formatted as a dict.
1493
1466
  The keys will correspond to the named-arguments in the tool.
1494
- **kwargs (Any): The keyword arguments to pass to the event handler
1467
+ **kwargs: The keyword arguments to pass to the event handler
1495
1468
 
1496
1469
  Returns:
1497
- CallbackManagerForToolRun: The callback manager for the tool run.
1470
+ The callback manager for the tool run.
1498
1471
 
1499
1472
  """
1500
1473
  if run_id is None:
@@ -1528,20 +1501,20 @@ class CallbackManager(BaseCallbackManager):
1528
1501
  @override
1529
1502
  def on_retriever_start(
1530
1503
  self,
1531
- serialized: Optional[dict[str, Any]],
1504
+ serialized: dict[str, Any] | None,
1532
1505
  query: str,
1533
- run_id: Optional[UUID] = None,
1534
- parent_run_id: Optional[UUID] = None,
1506
+ run_id: UUID | None = None,
1507
+ parent_run_id: UUID | None = None,
1535
1508
  **kwargs: Any,
1536
1509
  ) -> CallbackManagerForRetrieverRun:
1537
1510
  """Run when the retriever starts running.
1538
1511
 
1539
1512
  Args:
1540
- serialized (Optional[dict[str, Any]]): The serialized retriever.
1541
- query (str): The query.
1542
- run_id (UUID, optional): The ID of the run. Defaults to None.
1543
- parent_run_id (UUID, optional): The ID of the parent run. Defaults to None.
1544
- **kwargs (Any): Additional keyword arguments.
1513
+ serialized: The serialized retriever.
1514
+ query: The query.
1515
+ run_id: The ID of the run.
1516
+ parent_run_id: The ID of the parent run.
1517
+ **kwargs: Additional keyword arguments.
1545
1518
 
1546
1519
  Returns:
1547
1520
  The callback manager for the retriever run.
@@ -1577,7 +1550,7 @@ class CallbackManager(BaseCallbackManager):
1577
1550
  self,
1578
1551
  name: str,
1579
1552
  data: Any,
1580
- run_id: Optional[UUID] = None,
1553
+ run_id: UUID | None = None,
1581
1554
  **kwargs: Any,
1582
1555
  ) -> None:
1583
1556
  """Dispatch an adhoc event to the handlers (async version).
@@ -1589,12 +1562,12 @@ class CallbackManager(BaseCallbackManager):
1589
1562
  Args:
1590
1563
  name: The name of the adhoc event.
1591
1564
  data: The data for the adhoc event.
1592
- run_id: The ID of the run. Defaults to None.
1565
+ run_id: The ID of the run.
1593
1566
 
1594
1567
  Raises:
1595
1568
  ValueError: If additional keyword arguments are passed.
1596
1569
 
1597
- .. versionadded:: 0.2.14
1570
+ !!! version-added "Added in version 0.2.14"
1598
1571
 
1599
1572
  """
1600
1573
  if not self.handlers:
@@ -1626,31 +1599,24 @@ class CallbackManager(BaseCallbackManager):
1626
1599
  inheritable_callbacks: Callbacks = None,
1627
1600
  local_callbacks: Callbacks = None,
1628
1601
  verbose: bool = False, # noqa: FBT001,FBT002
1629
- inheritable_tags: Optional[list[str]] = None,
1630
- local_tags: Optional[list[str]] = None,
1631
- inheritable_metadata: Optional[dict[str, Any]] = None,
1632
- local_metadata: Optional[dict[str, Any]] = None,
1602
+ inheritable_tags: list[str] | None = None,
1603
+ local_tags: list[str] | None = None,
1604
+ inheritable_metadata: dict[str, Any] | None = None,
1605
+ local_metadata: dict[str, Any] | None = None,
1633
1606
  ) -> CallbackManager:
1634
1607
  """Configure the callback manager.
1635
1608
 
1636
1609
  Args:
1637
- inheritable_callbacks (Optional[Callbacks], optional): The inheritable
1638
- callbacks. Defaults to None.
1639
- local_callbacks (Optional[Callbacks], optional): The local callbacks.
1640
- Defaults to None.
1641
- verbose (bool, optional): Whether to enable verbose mode. Defaults to False.
1642
- inheritable_tags (Optional[list[str]], optional): The inheritable tags.
1643
- Defaults to None.
1644
- local_tags (Optional[list[str]], optional): The local tags.
1645
- Defaults to None.
1646
- inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
1647
- metadata. Defaults to None.
1648
- local_metadata (Optional[dict[str, Any]], optional): The local metadata.
1649
- Defaults to None.
1610
+ inheritable_callbacks: The inheritable callbacks.
1611
+ local_callbacks: The local callbacks.
1612
+ verbose: Whether to enable verbose mode.
1613
+ inheritable_tags: The inheritable tags.
1614
+ local_tags: The local tags.
1615
+ inheritable_metadata: The inheritable metadata.
1616
+ local_metadata: The local metadata.
1650
1617
 
1651
1618
  Returns:
1652
- CallbackManager: The configured callback manager.
1653
-
1619
+ The configured callback manager.
1654
1620
  """
1655
1621
  return _configure(
1656
1622
  cls,
@@ -1670,8 +1636,8 @@ class CallbackManagerForChainGroup(CallbackManager):
1670
1636
  def __init__(
1671
1637
  self,
1672
1638
  handlers: list[BaseCallbackHandler],
1673
- inheritable_handlers: Optional[list[BaseCallbackHandler]] = None,
1674
- parent_run_id: Optional[UUID] = None,
1639
+ inheritable_handlers: list[BaseCallbackHandler] | None = None,
1640
+ parent_run_id: UUID | None = None,
1675
1641
  *,
1676
1642
  parent_run_manager: CallbackManagerForChainRun,
1677
1643
  **kwargs: Any,
@@ -1679,12 +1645,11 @@ class CallbackManagerForChainGroup(CallbackManager):
1679
1645
  """Initialize the callback manager.
1680
1646
 
1681
1647
  Args:
1682
- handlers (list[BaseCallbackHandler]): The list of handlers.
1683
- inheritable_handlers (Optional[list[BaseCallbackHandler]]): The list of
1684
- inheritable handlers. Defaults to None.
1685
- parent_run_id (Optional[UUID]): The ID of the parent run. Defaults to None.
1686
- parent_run_manager (CallbackManagerForChainRun): The parent run manager.
1687
- **kwargs (Any): Additional keyword arguments.
1648
+ handlers: The list of handlers.
1649
+ inheritable_handlers: The list of inheritable handlers.
1650
+ parent_run_id: The ID of the parent run.
1651
+ parent_run_manager: The parent run manager.
1652
+ **kwargs: Additional keyword arguments.
1688
1653
 
1689
1654
  """
1690
1655
  super().__init__(
@@ -1719,38 +1684,33 @@ class CallbackManagerForChainGroup(CallbackManager):
1719
1684
  from the current object.
1720
1685
 
1721
1686
  Returns:
1722
- CallbackManagerForChainGroup: A copy of the current object with the
1723
- handlers, tags, and other attributes merged from the other object.
1687
+ A copy of the current object with the handlers, tags, and other attributes
1688
+ merged from the other object.
1724
1689
 
1725
1690
  Example: Merging two callback managers.
1726
1691
 
1727
- .. code-block:: python
1728
-
1729
- from langchain_core.callbacks.manager import (
1730
- CallbackManager,
1731
- trace_as_chain_group,
1732
- )
1733
- from langchain_core.callbacks.stdout import StdOutCallbackHandler
1734
-
1735
- manager = CallbackManager(
1736
- handlers=[StdOutCallbackHandler()], tags=["tag2"]
1737
- )
1738
- with trace_as_chain_group(
1739
- "My Group Name", tags=["tag1"]
1740
- ) as group_manager:
1741
- merged_manager = group_manager.merge(manager)
1742
- print(type(merged_manager))
1743
- # <class 'langchain_core.callbacks.manager.CallbackManagerForChainGroup'>
1744
-
1745
- print(merged_manager.handlers)
1746
- # [
1747
- # <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
1748
- # <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
1749
- # ]
1750
-
1751
- print(merged_manager.tags)
1752
- # ['tag2', 'tag1']
1753
-
1692
+ ```python
1693
+ from langchain_core.callbacks.manager import (
1694
+ CallbackManager,
1695
+ trace_as_chain_group,
1696
+ )
1697
+ from langchain_core.callbacks.stdout import StdOutCallbackHandler
1698
+
1699
+ manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
1700
+ with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
1701
+ merged_manager = group_manager.merge(manager)
1702
+ print(type(merged_manager))
1703
+ # <class 'langchain_core.callbacks.manager.CallbackManagerForChainGroup'>
1704
+
1705
+ print(merged_manager.handlers)
1706
+ # [
1707
+ # <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
1708
+ # <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
1709
+ # ]
1710
+
1711
+ print(merged_manager.tags)
1712
+ # ['tag2', 'tag1']
1713
+ ```
1754
1714
  """ # noqa: E501
1755
1715
  manager = self.__class__(
1756
1716
  parent_run_id=self.parent_run_id or other.parent_run_id,
@@ -1775,12 +1735,12 @@ class CallbackManagerForChainGroup(CallbackManager):
1775
1735
  manager.add_handler(handler, inherit=True)
1776
1736
  return manager
1777
1737
 
1778
- def on_chain_end(self, outputs: Union[dict[str, Any], Any], **kwargs: Any) -> None:
1738
+ def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
1779
1739
  """Run when traced chain group ends.
1780
1740
 
1781
1741
  Args:
1782
- outputs (Union[dict[str, Any], Any]): The outputs of the chain.
1783
- **kwargs (Any): Additional keyword arguments.
1742
+ outputs: The outputs of the chain.
1743
+ **kwargs: Additional keyword arguments.
1784
1744
 
1785
1745
  """
1786
1746
  self.ended = True
@@ -1794,8 +1754,8 @@ class CallbackManagerForChainGroup(CallbackManager):
1794
1754
  """Run when chain errors.
1795
1755
 
1796
1756
  Args:
1797
- error (Exception or KeyboardInterrupt): The error.
1798
- **kwargs (Any): Additional keyword arguments.
1757
+ error: The error.
1758
+ **kwargs: Additional keyword arguments.
1799
1759
 
1800
1760
  """
1801
1761
  self.ended = True
@@ -1814,21 +1774,20 @@ class AsyncCallbackManager(BaseCallbackManager):
1814
1774
  self,
1815
1775
  serialized: dict[str, Any],
1816
1776
  prompts: list[str],
1817
- run_id: Optional[UUID] = None,
1777
+ run_id: UUID | None = None,
1818
1778
  **kwargs: Any,
1819
1779
  ) -> list[AsyncCallbackManagerForLLMRun]:
1820
1780
  """Run when LLM starts running.
1821
1781
 
1822
1782
  Args:
1823
- serialized (dict[str, Any]): The serialized LLM.
1824
- prompts (list[str]): The list of prompts.
1825
- run_id (UUID, optional): The ID of the run. Defaults to None.
1826
- **kwargs (Any): Additional keyword arguments.
1783
+ serialized: The serialized LLM.
1784
+ prompts: The list of prompts.
1785
+ run_id: The ID of the run.
1786
+ **kwargs: Additional keyword arguments.
1827
1787
 
1828
1788
  Returns:
1829
- list[AsyncCallbackManagerForLLMRun]: The list of async
1830
- callback managers, one for each LLM Run corresponding
1831
- to each prompt.
1789
+ The list of async callback managers, one for each LLM Run corresponding to
1790
+ each prompt.
1832
1791
  """
1833
1792
  inline_tasks = []
1834
1793
  non_inline_tasks = []
@@ -1903,21 +1862,20 @@ class AsyncCallbackManager(BaseCallbackManager):
1903
1862
  self,
1904
1863
  serialized: dict[str, Any],
1905
1864
  messages: list[list[BaseMessage]],
1906
- run_id: Optional[UUID] = None,
1865
+ run_id: UUID | None = None,
1907
1866
  **kwargs: Any,
1908
1867
  ) -> list[AsyncCallbackManagerForLLMRun]:
1909
1868
  """Async run when LLM starts running.
1910
1869
 
1911
1870
  Args:
1912
- serialized (dict[str, Any]): The serialized LLM.
1913
- messages (list[list[BaseMessage]]): The list of messages.
1914
- run_id (UUID, optional): The ID of the run. Defaults to None.
1915
- **kwargs (Any): Additional keyword arguments.
1871
+ serialized: The serialized LLM.
1872
+ messages: The list of messages.
1873
+ run_id: The ID of the run.
1874
+ **kwargs: Additional keyword arguments.
1916
1875
 
1917
1876
  Returns:
1918
- list[AsyncCallbackManagerForLLMRun]: The list of
1919
- async callback managers, one for each LLM Run
1920
- corresponding to each inner message list.
1877
+ The list of async callback managers, one for each LLM Run corresponding to
1878
+ each inner message list.
1921
1879
  """
1922
1880
  inline_tasks = []
1923
1881
  non_inline_tasks = []
@@ -1973,22 +1931,21 @@ class AsyncCallbackManager(BaseCallbackManager):
1973
1931
 
1974
1932
  async def on_chain_start(
1975
1933
  self,
1976
- serialized: Optional[dict[str, Any]],
1977
- inputs: Union[dict[str, Any], Any],
1978
- run_id: Optional[UUID] = None,
1934
+ serialized: dict[str, Any] | None,
1935
+ inputs: dict[str, Any] | Any,
1936
+ run_id: UUID | None = None,
1979
1937
  **kwargs: Any,
1980
1938
  ) -> AsyncCallbackManagerForChainRun:
1981
1939
  """Async run when chain starts running.
1982
1940
 
1983
1941
  Args:
1984
- serialized (Optional[dict[str, Any]]): The serialized chain.
1985
- inputs (Union[dict[str, Any], Any]): The inputs to the chain.
1986
- run_id (UUID, optional): The ID of the run. Defaults to None.
1987
- **kwargs (Any): Additional keyword arguments.
1942
+ serialized: The serialized chain.
1943
+ inputs: The inputs to the chain.
1944
+ run_id: The ID of the run.
1945
+ **kwargs: Additional keyword arguments.
1988
1946
 
1989
1947
  Returns:
1990
- AsyncCallbackManagerForChainRun: The async callback manager
1991
- for the chain run.
1948
+ The async callback manager for the chain run.
1992
1949
  """
1993
1950
  if run_id is None:
1994
1951
  run_id = uuid.uuid4()
@@ -2020,25 +1977,23 @@ class AsyncCallbackManager(BaseCallbackManager):
2020
1977
  @override
2021
1978
  async def on_tool_start(
2022
1979
  self,
2023
- serialized: Optional[dict[str, Any]],
1980
+ serialized: dict[str, Any] | None,
2024
1981
  input_str: str,
2025
- run_id: Optional[UUID] = None,
2026
- parent_run_id: Optional[UUID] = None,
1982
+ run_id: UUID | None = None,
1983
+ parent_run_id: UUID | None = None,
2027
1984
  **kwargs: Any,
2028
1985
  ) -> AsyncCallbackManagerForToolRun:
2029
1986
  """Run when the tool starts running.
2030
1987
 
2031
1988
  Args:
2032
- serialized (Optional[dict[str, Any]]): The serialized tool.
2033
- input_str (str): The input to the tool.
2034
- run_id (UUID, optional): The ID of the run. Defaults to None.
2035
- parent_run_id (UUID, optional): The ID of the parent run.
2036
- Defaults to None.
2037
- **kwargs (Any): Additional keyword arguments.
1989
+ serialized: The serialized tool.
1990
+ input_str: The input to the tool.
1991
+ run_id: The ID of the run.
1992
+ parent_run_id: The ID of the parent run.
1993
+ **kwargs: Additional keyword arguments.
2038
1994
 
2039
1995
  Returns:
2040
- AsyncCallbackManagerForToolRun: The async callback manager
2041
- for the tool run.
1996
+ The async callback manager for the tool run.
2042
1997
  """
2043
1998
  if run_id is None:
2044
1999
  run_id = uuid.uuid4()
@@ -2071,7 +2026,7 @@ class AsyncCallbackManager(BaseCallbackManager):
2071
2026
  self,
2072
2027
  name: str,
2073
2028
  data: Any,
2074
- run_id: Optional[UUID] = None,
2029
+ run_id: UUID | None = None,
2075
2030
  **kwargs: Any,
2076
2031
  ) -> None:
2077
2032
  """Dispatch an adhoc event to the handlers (async version).
@@ -2083,12 +2038,12 @@ class AsyncCallbackManager(BaseCallbackManager):
2083
2038
  Args:
2084
2039
  name: The name of the adhoc event.
2085
2040
  data: The data for the adhoc event.
2086
- run_id: The ID of the run. Defaults to None.
2041
+ run_id: The ID of the run.
2087
2042
 
2088
2043
  Raises:
2089
2044
  ValueError: If additional keyword arguments are passed.
2090
2045
 
2091
- .. versionadded:: 0.2.14
2046
+ !!! version-added "Added in version 0.2.14"
2092
2047
  """
2093
2048
  if not self.handlers:
2094
2049
  return
@@ -2116,24 +2071,23 @@ class AsyncCallbackManager(BaseCallbackManager):
2116
2071
  @override
2117
2072
  async def on_retriever_start(
2118
2073
  self,
2119
- serialized: Optional[dict[str, Any]],
2074
+ serialized: dict[str, Any] | None,
2120
2075
  query: str,
2121
- run_id: Optional[UUID] = None,
2122
- parent_run_id: Optional[UUID] = None,
2076
+ run_id: UUID | None = None,
2077
+ parent_run_id: UUID | None = None,
2123
2078
  **kwargs: Any,
2124
2079
  ) -> AsyncCallbackManagerForRetrieverRun:
2125
2080
  """Run when the retriever starts running.
2126
2081
 
2127
2082
  Args:
2128
- serialized (Optional[dict[str, Any]]): The serialized retriever.
2129
- query (str): The query.
2130
- run_id (UUID, optional): The ID of the run. Defaults to None.
2131
- parent_run_id (UUID, optional): The ID of the parent run. Defaults to None.
2132
- **kwargs (Any): Additional keyword arguments.
2083
+ serialized: The serialized retriever.
2084
+ query: The query.
2085
+ run_id: The ID of the run.
2086
+ parent_run_id: The ID of the parent run.
2087
+ **kwargs: Additional keyword arguments.
2133
2088
 
2134
2089
  Returns:
2135
- AsyncCallbackManagerForRetrieverRun: The async callback manager
2136
- for the retriever run.
2090
+ The async callback manager for the retriever run.
2137
2091
  """
2138
2092
  if run_id is None:
2139
2093
  run_id = uuid.uuid4()
@@ -2168,30 +2122,24 @@ class AsyncCallbackManager(BaseCallbackManager):
2168
2122
  inheritable_callbacks: Callbacks = None,
2169
2123
  local_callbacks: Callbacks = None,
2170
2124
  verbose: bool = False, # noqa: FBT001,FBT002
2171
- inheritable_tags: Optional[list[str]] = None,
2172
- local_tags: Optional[list[str]] = None,
2173
- inheritable_metadata: Optional[dict[str, Any]] = None,
2174
- local_metadata: Optional[dict[str, Any]] = None,
2125
+ inheritable_tags: list[str] | None = None,
2126
+ local_tags: list[str] | None = None,
2127
+ inheritable_metadata: dict[str, Any] | None = None,
2128
+ local_metadata: dict[str, Any] | None = None,
2175
2129
  ) -> AsyncCallbackManager:
2176
2130
  """Configure the async callback manager.
2177
2131
 
2178
2132
  Args:
2179
- inheritable_callbacks (Optional[Callbacks], optional): The inheritable
2180
- callbacks. Defaults to None.
2181
- local_callbacks (Optional[Callbacks], optional): The local callbacks.
2182
- Defaults to None.
2183
- verbose (bool, optional): Whether to enable verbose mode. Defaults to False.
2184
- inheritable_tags (Optional[list[str]], optional): The inheritable tags.
2185
- Defaults to None.
2186
- local_tags (Optional[list[str]], optional): The local tags.
2187
- Defaults to None.
2188
- inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
2189
- metadata. Defaults to None.
2190
- local_metadata (Optional[dict[str, Any]], optional): The local metadata.
2191
- Defaults to None.
2133
+ inheritable_callbacks: The inheritable callbacks.
2134
+ local_callbacks: The local callbacks.
2135
+ verbose: Whether to enable verbose mode.
2136
+ inheritable_tags: The inheritable tags.
2137
+ local_tags: The local tags.
2138
+ inheritable_metadata: The inheritable metadata.
2139
+ local_metadata: The local metadata.
2192
2140
 
2193
2141
  Returns:
2194
- AsyncCallbackManager: The configured async callback manager.
2142
+ The configured async callback manager.
2195
2143
  """
2196
2144
  return _configure(
2197
2145
  cls,
@@ -2211,8 +2159,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2211
2159
  def __init__(
2212
2160
  self,
2213
2161
  handlers: list[BaseCallbackHandler],
2214
- inheritable_handlers: Optional[list[BaseCallbackHandler]] = None,
2215
- parent_run_id: Optional[UUID] = None,
2162
+ inheritable_handlers: list[BaseCallbackHandler] | None = None,
2163
+ parent_run_id: UUID | None = None,
2216
2164
  *,
2217
2165
  parent_run_manager: AsyncCallbackManagerForChainRun,
2218
2166
  **kwargs: Any,
@@ -2220,13 +2168,11 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2220
2168
  """Initialize the async callback manager.
2221
2169
 
2222
2170
  Args:
2223
- handlers (list[BaseCallbackHandler]): The list of handlers.
2224
- inheritable_handlers (Optional[list[BaseCallbackHandler]]): The list of
2225
- inheritable handlers. Defaults to None.
2226
- parent_run_id (Optional[UUID]): The ID of the parent run. Defaults to None.
2227
- parent_run_manager (AsyncCallbackManagerForChainRun):
2228
- The parent run manager.
2229
- **kwargs (Any): Additional keyword arguments.
2171
+ handlers: The list of handlers.
2172
+ inheritable_handlers: The list of inheritable handlers.
2173
+ parent_run_id: The ID of the parent run.
2174
+ parent_run_manager: The parent run manager.
2175
+ **kwargs: Additional keyword arguments.
2230
2176
  """
2231
2177
  super().__init__(
2232
2178
  handlers,
@@ -2265,33 +2211,30 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2265
2211
 
2266
2212
  Example: Merging two callback managers.
2267
2213
 
2268
- .. code-block:: python
2269
-
2270
- from langchain_core.callbacks.manager import (
2271
- CallbackManager,
2272
- atrace_as_chain_group,
2273
- )
2274
- from langchain_core.callbacks.stdout import StdOutCallbackHandler
2275
-
2276
- manager = CallbackManager(
2277
- handlers=[StdOutCallbackHandler()], tags=["tag2"]
2278
- )
2279
- async with atrace_as_chain_group(
2280
- "My Group Name", tags=["tag1"]
2281
- ) as group_manager:
2282
- merged_manager = group_manager.merge(manager)
2283
- print(type(merged_manager))
2284
- # <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
2285
-
2286
- print(merged_manager.handlers)
2287
- # [
2288
- # <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
2289
- # <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
2290
- # ]
2291
-
2292
- print(merged_manager.tags)
2293
- # ['tag2', 'tag1']
2214
+ ```python
2215
+ from langchain_core.callbacks.manager import (
2216
+ CallbackManager,
2217
+ atrace_as_chain_group,
2218
+ )
2219
+ from langchain_core.callbacks.stdout import StdOutCallbackHandler
2294
2220
 
2221
+ manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
2222
+ async with atrace_as_chain_group(
2223
+ "My Group Name", tags=["tag1"]
2224
+ ) as group_manager:
2225
+ merged_manager = group_manager.merge(manager)
2226
+ print(type(merged_manager))
2227
+ # <class 'langchain_core.callbacks.manager.AsyncCallbackManagerForChainGroup'>
2228
+
2229
+ print(merged_manager.handlers)
2230
+ # [
2231
+ # <langchain_core.callbacks.stdout.LangChainTracer object at ...>,
2232
+ # <langchain_core.callbacks.streaming_stdout.StdOutCallbackHandler object at ...>,
2233
+ # ]
2234
+
2235
+ print(merged_manager.tags)
2236
+ # ['tag2', 'tag1']
2237
+ ```
2295
2238
  """ # noqa: E501
2296
2239
  manager = self.__class__(
2297
2240
  parent_run_id=self.parent_run_id or other.parent_run_id,
@@ -2316,14 +2259,12 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2316
2259
  manager.add_handler(handler, inherit=True)
2317
2260
  return manager
2318
2261
 
2319
- async def on_chain_end(
2320
- self, outputs: Union[dict[str, Any], Any], **kwargs: Any
2321
- ) -> None:
2262
+ async def on_chain_end(self, outputs: dict[str, Any] | Any, **kwargs: Any) -> None:
2322
2263
  """Run when traced chain group ends.
2323
2264
 
2324
2265
  Args:
2325
- outputs (Union[dict[str, Any], Any]): The outputs of the chain.
2326
- **kwargs (Any): Additional keyword arguments.
2266
+ outputs: The outputs of the chain.
2267
+ **kwargs: Additional keyword arguments.
2327
2268
  """
2328
2269
  self.ended = True
2329
2270
  await self.parent_run_manager.on_chain_end(outputs, **kwargs)
@@ -2336,8 +2277,8 @@ class AsyncCallbackManagerForChainGroup(AsyncCallbackManager):
2336
2277
  """Run when chain errors.
2337
2278
 
2338
2279
  Args:
2339
- error (Exception or KeyboardInterrupt): The error.
2340
- **kwargs (Any): Additional keyword arguments.
2280
+ error: The error.
2281
+ **kwargs: Additional keyword arguments.
2341
2282
  """
2342
2283
  self.ended = True
2343
2284
  await self.parent_run_manager.on_chain_error(error, **kwargs)
@@ -2350,40 +2291,35 @@ def _configure(
2350
2291
  callback_manager_cls: type[T],
2351
2292
  inheritable_callbacks: Callbacks = None,
2352
2293
  local_callbacks: Callbacks = None,
2353
- inheritable_tags: Optional[list[str]] = None,
2354
- local_tags: Optional[list[str]] = None,
2355
- inheritable_metadata: Optional[dict[str, Any]] = None,
2356
- local_metadata: Optional[dict[str, Any]] = None,
2294
+ inheritable_tags: list[str] | None = None,
2295
+ local_tags: list[str] | None = None,
2296
+ inheritable_metadata: dict[str, Any] | None = None,
2297
+ local_metadata: dict[str, Any] | None = None,
2357
2298
  *,
2358
2299
  verbose: bool = False,
2359
2300
  ) -> T:
2360
2301
  """Configure the callback manager.
2361
2302
 
2362
2303
  Args:
2363
- callback_manager_cls (Type[T]): The callback manager class.
2364
- inheritable_callbacks (Optional[Callbacks], optional): The inheritable
2365
- callbacks. Defaults to None.
2366
- local_callbacks (Optional[Callbacks], optional): The local callbacks.
2367
- Defaults to None.
2368
- verbose (bool, optional): Whether to enable verbose mode. Defaults to False.
2369
- inheritable_tags (Optional[list[str]], optional): The inheritable tags.
2370
- Defaults to None.
2371
- local_tags (Optional[list[str]], optional): The local tags. Defaults to None.
2372
- inheritable_metadata (Optional[dict[str, Any]], optional): The inheritable
2373
- metadata. Defaults to None.
2374
- local_metadata (Optional[dict[str, Any]], optional): The local metadata.
2375
- Defaults to None.
2304
+ callback_manager_cls: The callback manager class.
2305
+ inheritable_callbacks: The inheritable callbacks.
2306
+ local_callbacks: The local callbacks.
2307
+ inheritable_tags: The inheritable tags.
2308
+ local_tags: The local tags.
2309
+ inheritable_metadata: The inheritable metadata.
2310
+ local_metadata: The local metadata.
2311
+ verbose: Whether to enable verbose mode.
2376
2312
 
2377
2313
  Raises:
2378
2314
  RuntimeError: If `LANGCHAIN_TRACING` is set but `LANGCHAIN_TRACING_V2` is not.
2379
2315
 
2380
2316
  Returns:
2381
- T: The configured callback manager.
2317
+ The configured callback manager.
2382
2318
  """
2383
2319
  tracing_context = get_tracing_context()
2384
2320
  tracing_metadata = tracing_context["metadata"]
2385
2321
  tracing_tags = tracing_context["tags"]
2386
- run_tree: Optional[Run] = tracing_context["parent"]
2322
+ run_tree: Run | None = tracing_context["parent"]
2387
2323
  parent_run_id = None if run_tree is None else run_tree.id
2388
2324
  callback_manager = callback_manager_cls(
2389
2325
  handlers=[],
@@ -2528,15 +2464,15 @@ def _configure(
2528
2464
 
2529
2465
 
2530
2466
  async def adispatch_custom_event(
2531
- name: str, data: Any, *, config: Optional[RunnableConfig] = None
2467
+ name: str, data: Any, *, config: RunnableConfig | None = None
2532
2468
  ) -> None:
2533
2469
  """Dispatch an adhoc event to the handlers.
2534
2470
 
2535
2471
  Args:
2536
2472
  name: The name of the adhoc event.
2537
2473
  data: The data for the adhoc event. Free form data. Ideally should be
2538
- JSON serializable to avoid serialization issues downstream, but
2539
- this is not enforced.
2474
+ JSON serializable to avoid serialization issues downstream, but
2475
+ this is not enforced.
2540
2476
  config: Optional config object. Mirrors the async API but not strictly needed.
2541
2477
 
2542
2478
  Raises:
@@ -2544,84 +2480,83 @@ async def adispatch_custom_event(
2544
2480
  the event with.
2545
2481
 
2546
2482
  Example:
2547
-
2548
- .. code-block:: python
2549
-
2550
- from langchain_core.callbacks import (
2551
- AsyncCallbackHandler,
2552
- adispatch_custom_event
2553
- )
2554
- from langchain_core.runnable import RunnableLambda
2555
-
2556
- class CustomCallbackManager(AsyncCallbackHandler):
2557
- async def on_custom_event(
2558
- self,
2559
- name: str,
2560
- data: Any,
2561
- *,
2562
- run_id: UUID,
2563
- tags: Optional[list[str]] = None,
2564
- metadata: Optional[dict[str, Any]] = None,
2565
- **kwargs: Any,
2566
- ) -> None:
2567
- print(f"Received custom event: {name} with data: {data}")
2568
-
2569
- callback = CustomCallbackManager()
2570
-
2571
- async def foo(inputs):
2572
- await adispatch_custom_event("my_event", {"bar": "buzz})
2573
- return inputs
2574
-
2575
- foo_ = RunnableLambda(foo)
2576
- await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
2483
+ ```python
2484
+ from langchain_core.callbacks import (
2485
+ AsyncCallbackHandler,
2486
+ adispatch_custom_event
2487
+ )
2488
+ from langchain_core.runnable import RunnableLambda
2489
+
2490
+ class CustomCallbackManager(AsyncCallbackHandler):
2491
+ async def on_custom_event(
2492
+ self,
2493
+ name: str,
2494
+ data: Any,
2495
+ *,
2496
+ run_id: UUID,
2497
+ tags: list[str] | None = None,
2498
+ metadata: dict[str, Any] | None = None,
2499
+ **kwargs: Any,
2500
+ ) -> None:
2501
+ print(f"Received custom event: {name} with data: {data}")
2502
+
2503
+ callback = CustomCallbackManager()
2504
+
2505
+ async def foo(inputs):
2506
+ await adispatch_custom_event("my_event", {"bar": "buzz})
2507
+ return inputs
2508
+
2509
+ foo_ = RunnableLambda(foo)
2510
+ await foo_.ainvoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
2511
+ ```
2577
2512
 
2578
2513
  Example: Use with astream events
2579
2514
 
2580
- .. code-block:: python
2581
-
2582
- from langchain_core.callbacks import (
2583
- AsyncCallbackHandler,
2584
- adispatch_custom_event
2585
- )
2586
- from langchain_core.runnable import RunnableLambda
2587
-
2588
- class CustomCallbackManager(AsyncCallbackHandler):
2589
- async def on_custom_event(
2590
- self,
2591
- name: str,
2592
- data: Any,
2593
- *,
2594
- run_id: UUID,
2595
- tags: Optional[list[str]] = None,
2596
- metadata: Optional[dict[str, Any]] = None,
2597
- **kwargs: Any,
2598
- ) -> None:
2599
- print(f"Received custom event: {name} with data: {data}")
2600
-
2601
- callback = CustomCallbackManager()
2602
-
2603
- async def foo(inputs):
2604
- await adispatch_custom_event("event_type_1", {"bar": "buzz})
2605
- await adispatch_custom_event("event_type_2", 5)
2606
- return inputs
2607
-
2608
- foo_ = RunnableLambda(foo)
2609
-
2610
- async for event in foo_.ainvoke_stream(
2611
- {"a": "1"},
2612
- version="v2",
2613
- config={"callbacks": [CustomCallbackManager()]}
2614
- ):
2615
- print(event)
2515
+ ```python
2516
+ from langchain_core.callbacks import (
2517
+ AsyncCallbackHandler,
2518
+ adispatch_custom_event
2519
+ )
2520
+ from langchain_core.runnable import RunnableLambda
2521
+
2522
+ class CustomCallbackManager(AsyncCallbackHandler):
2523
+ async def on_custom_event(
2524
+ self,
2525
+ name: str,
2526
+ data: Any,
2527
+ *,
2528
+ run_id: UUID,
2529
+ tags: list[str] | None = None,
2530
+ metadata: dict[str, Any] | None = None,
2531
+ **kwargs: Any,
2532
+ ) -> None:
2533
+ print(f"Received custom event: {name} with data: {data}")
2534
+
2535
+ callback = CustomCallbackManager()
2536
+
2537
+ async def foo(inputs):
2538
+ await adispatch_custom_event("event_type_1", {"bar": "buzz})
2539
+ await adispatch_custom_event("event_type_2", 5)
2540
+ return inputs
2541
+
2542
+ foo_ = RunnableLambda(foo)
2543
+
2544
+ async for event in foo_.ainvoke_stream(
2545
+ {"a": "1"},
2546
+ version="v2",
2547
+ config={"callbacks": [CustomCallbackManager()]}
2548
+ ):
2549
+ print(event)
2550
+ ```
2616
2551
 
2617
- .. warning::
2552
+ !!! warning
2618
2553
  If using python <= 3.10 and async, you MUST
2619
2554
  specify the `config` parameter or the function will raise an error.
2620
2555
  This is due to a limitation in asyncio for python <= 3.10 that prevents
2621
2556
  LangChain from automatically propagating the config object on the user's
2622
2557
  behalf.
2623
2558
 
2624
- .. versionadded:: 0.2.15
2559
+ !!! version-added "Added in version 0.2.15"
2625
2560
 
2626
2561
  """
2627
2562
  # Import locally to prevent circular imports.
@@ -2654,15 +2589,15 @@ async def adispatch_custom_event(
2654
2589
 
2655
2590
 
2656
2591
  def dispatch_custom_event(
2657
- name: str, data: Any, *, config: Optional[RunnableConfig] = None
2592
+ name: str, data: Any, *, config: RunnableConfig | None = None
2658
2593
  ) -> None:
2659
2594
  """Dispatch an adhoc event.
2660
2595
 
2661
2596
  Args:
2662
2597
  name: The name of the adhoc event.
2663
2598
  data: The data for the adhoc event. Free form data. Ideally should be
2664
- JSON serializable to avoid serialization issues downstream, but
2665
- this is not enforced.
2599
+ JSON serializable to avoid serialization issues downstream, but
2600
+ this is not enforced.
2666
2601
  config: Optional config object. Mirrors the async API but not strictly needed.
2667
2602
 
2668
2603
  Raises:
@@ -2670,34 +2605,33 @@ def dispatch_custom_event(
2670
2605
  the event with.
2671
2606
 
2672
2607
  Example:
2673
-
2674
- .. code-block:: python
2675
-
2676
- from langchain_core.callbacks import BaseCallbackHandler
2677
- from langchain_core.callbacks import dispatch_custom_event
2678
- from langchain_core.runnable import RunnableLambda
2679
-
2680
- class CustomCallbackManager(BaseCallbackHandler):
2681
- def on_custom_event(
2682
- self,
2683
- name: str,
2684
- data: Any,
2685
- *,
2686
- run_id: UUID,
2687
- tags: Optional[list[str]] = None,
2688
- metadata: Optional[dict[str, Any]] = None,
2689
- **kwargs: Any,
2690
- ) -> None:
2691
- print(f"Received custom event: {name} with data: {data}")
2692
-
2693
- def foo(inputs):
2694
- dispatch_custom_event("my_event", {"bar": "buzz})
2695
- return inputs
2696
-
2697
- foo_ = RunnableLambda(foo)
2698
- foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
2699
-
2700
- .. versionadded:: 0.2.15
2608
+ ```python
2609
+ from langchain_core.callbacks import BaseCallbackHandler
2610
+ from langchain_core.callbacks import dispatch_custom_event
2611
+ from langchain_core.runnable import RunnableLambda
2612
+
2613
+ class CustomCallbackManager(BaseCallbackHandler):
2614
+ def on_custom_event(
2615
+ self,
2616
+ name: str,
2617
+ data: Any,
2618
+ *,
2619
+ run_id: UUID,
2620
+ tags: list[str] | None = None,
2621
+ metadata: dict[str, Any] | None = None,
2622
+ **kwargs: Any,
2623
+ ) -> None:
2624
+ print(f"Received custom event: {name} with data: {data}")
2625
+
2626
+ def foo(inputs):
2627
+ dispatch_custom_event("my_event", {"bar": "buzz})
2628
+ return inputs
2629
+
2630
+ foo_ = RunnableLambda(foo)
2631
+ foo_.invoke({"a": "1"}, {"callbacks": [CustomCallbackManager()]})
2632
+ ```
2633
+
2634
+ !!! version-added "Added in version 0.2.15"
2701
2635
 
2702
2636
  """
2703
2637
  # Import locally to prevent circular imports.