langchain-core 0.4.0.dev0__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 (172) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +45 -70
  4. langchain_core/_api/deprecation.py +80 -80
  5. langchain_core/_api/path.py +22 -8
  6. langchain_core/_import_utils.py +10 -4
  7. langchain_core/agents.py +25 -21
  8. langchain_core/caches.py +53 -63
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +341 -348
  11. langchain_core/callbacks/file.py +55 -44
  12. langchain_core/callbacks/manager.py +546 -683
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +35 -36
  15. langchain_core/callbacks/usage.py +65 -70
  16. langchain_core/chat_history.py +48 -55
  17. langchain_core/document_loaders/base.py +46 -21
  18. langchain_core/document_loaders/langsmith.py +39 -36
  19. langchain_core/documents/__init__.py +0 -1
  20. langchain_core/documents/base.py +96 -74
  21. langchain_core/documents/compressor.py +12 -9
  22. langchain_core/documents/transformers.py +29 -28
  23. langchain_core/embeddings/fake.py +56 -57
  24. langchain_core/env.py +2 -3
  25. langchain_core/example_selectors/base.py +12 -0
  26. langchain_core/example_selectors/length_based.py +1 -1
  27. langchain_core/example_selectors/semantic_similarity.py +21 -25
  28. langchain_core/exceptions.py +15 -9
  29. langchain_core/globals.py +4 -163
  30. langchain_core/indexing/api.py +132 -125
  31. langchain_core/indexing/base.py +64 -67
  32. langchain_core/indexing/in_memory.py +26 -6
  33. langchain_core/language_models/__init__.py +15 -27
  34. langchain_core/language_models/_utils.py +267 -117
  35. langchain_core/language_models/base.py +92 -177
  36. langchain_core/language_models/chat_models.py +547 -407
  37. langchain_core/language_models/fake.py +11 -11
  38. langchain_core/language_models/fake_chat_models.py +72 -118
  39. langchain_core/language_models/llms.py +168 -242
  40. langchain_core/load/dump.py +8 -11
  41. langchain_core/load/load.py +32 -28
  42. langchain_core/load/mapping.py +2 -4
  43. langchain_core/load/serializable.py +50 -56
  44. langchain_core/messages/__init__.py +36 -51
  45. langchain_core/messages/ai.py +377 -150
  46. langchain_core/messages/base.py +239 -47
  47. langchain_core/messages/block_translators/__init__.py +111 -0
  48. langchain_core/messages/block_translators/anthropic.py +470 -0
  49. langchain_core/messages/block_translators/bedrock.py +94 -0
  50. langchain_core/messages/block_translators/bedrock_converse.py +297 -0
  51. langchain_core/messages/block_translators/google_genai.py +530 -0
  52. langchain_core/messages/block_translators/google_vertexai.py +21 -0
  53. langchain_core/messages/block_translators/groq.py +143 -0
  54. langchain_core/messages/block_translators/langchain_v0.py +301 -0
  55. langchain_core/messages/block_translators/openai.py +1010 -0
  56. langchain_core/messages/chat.py +2 -3
  57. langchain_core/messages/content.py +1423 -0
  58. langchain_core/messages/function.py +7 -7
  59. langchain_core/messages/human.py +44 -38
  60. langchain_core/messages/modifier.py +3 -2
  61. langchain_core/messages/system.py +40 -27
  62. langchain_core/messages/tool.py +160 -58
  63. langchain_core/messages/utils.py +527 -638
  64. langchain_core/output_parsers/__init__.py +1 -14
  65. langchain_core/output_parsers/base.py +68 -104
  66. langchain_core/output_parsers/json.py +13 -17
  67. langchain_core/output_parsers/list.py +11 -33
  68. langchain_core/output_parsers/openai_functions.py +56 -74
  69. langchain_core/output_parsers/openai_tools.py +68 -109
  70. langchain_core/output_parsers/pydantic.py +15 -13
  71. langchain_core/output_parsers/string.py +6 -2
  72. langchain_core/output_parsers/transform.py +17 -60
  73. langchain_core/output_parsers/xml.py +34 -44
  74. langchain_core/outputs/__init__.py +1 -1
  75. langchain_core/outputs/chat_generation.py +26 -11
  76. langchain_core/outputs/chat_result.py +1 -3
  77. langchain_core/outputs/generation.py +17 -6
  78. langchain_core/outputs/llm_result.py +15 -8
  79. langchain_core/prompt_values.py +29 -123
  80. langchain_core/prompts/__init__.py +3 -27
  81. langchain_core/prompts/base.py +48 -63
  82. langchain_core/prompts/chat.py +259 -288
  83. langchain_core/prompts/dict.py +19 -11
  84. langchain_core/prompts/few_shot.py +84 -90
  85. langchain_core/prompts/few_shot_with_templates.py +14 -12
  86. langchain_core/prompts/image.py +19 -14
  87. langchain_core/prompts/loading.py +6 -8
  88. langchain_core/prompts/message.py +7 -8
  89. langchain_core/prompts/prompt.py +42 -43
  90. langchain_core/prompts/string.py +37 -16
  91. langchain_core/prompts/structured.py +43 -46
  92. langchain_core/rate_limiters.py +51 -60
  93. langchain_core/retrievers.py +52 -192
  94. langchain_core/runnables/base.py +1727 -1683
  95. langchain_core/runnables/branch.py +52 -73
  96. langchain_core/runnables/config.py +89 -103
  97. langchain_core/runnables/configurable.py +128 -130
  98. langchain_core/runnables/fallbacks.py +93 -82
  99. langchain_core/runnables/graph.py +127 -127
  100. langchain_core/runnables/graph_ascii.py +63 -41
  101. langchain_core/runnables/graph_mermaid.py +87 -70
  102. langchain_core/runnables/graph_png.py +31 -36
  103. langchain_core/runnables/history.py +145 -161
  104. langchain_core/runnables/passthrough.py +141 -144
  105. langchain_core/runnables/retry.py +84 -68
  106. langchain_core/runnables/router.py +33 -37
  107. langchain_core/runnables/schema.py +79 -72
  108. langchain_core/runnables/utils.py +95 -139
  109. langchain_core/stores.py +85 -131
  110. langchain_core/structured_query.py +11 -15
  111. langchain_core/sys_info.py +31 -32
  112. langchain_core/tools/__init__.py +1 -14
  113. langchain_core/tools/base.py +221 -247
  114. langchain_core/tools/convert.py +144 -161
  115. langchain_core/tools/render.py +10 -10
  116. langchain_core/tools/retriever.py +12 -19
  117. langchain_core/tools/simple.py +52 -29
  118. langchain_core/tools/structured.py +56 -60
  119. langchain_core/tracers/__init__.py +1 -9
  120. langchain_core/tracers/_streaming.py +6 -7
  121. langchain_core/tracers/base.py +103 -112
  122. langchain_core/tracers/context.py +29 -48
  123. langchain_core/tracers/core.py +142 -105
  124. langchain_core/tracers/evaluation.py +30 -34
  125. langchain_core/tracers/event_stream.py +162 -117
  126. langchain_core/tracers/langchain.py +34 -36
  127. langchain_core/tracers/log_stream.py +87 -49
  128. langchain_core/tracers/memory_stream.py +3 -3
  129. langchain_core/tracers/root_listeners.py +18 -34
  130. langchain_core/tracers/run_collector.py +8 -20
  131. langchain_core/tracers/schemas.py +0 -125
  132. langchain_core/tracers/stdout.py +3 -3
  133. langchain_core/utils/__init__.py +1 -4
  134. langchain_core/utils/_merge.py +47 -9
  135. langchain_core/utils/aiter.py +70 -66
  136. langchain_core/utils/env.py +12 -9
  137. langchain_core/utils/function_calling.py +139 -206
  138. langchain_core/utils/html.py +7 -8
  139. langchain_core/utils/input.py +6 -6
  140. langchain_core/utils/interactive_env.py +6 -2
  141. langchain_core/utils/iter.py +48 -45
  142. langchain_core/utils/json.py +14 -4
  143. langchain_core/utils/json_schema.py +159 -43
  144. langchain_core/utils/mustache.py +32 -25
  145. langchain_core/utils/pydantic.py +67 -40
  146. langchain_core/utils/strings.py +5 -5
  147. langchain_core/utils/usage.py +1 -1
  148. langchain_core/utils/utils.py +104 -62
  149. langchain_core/vectorstores/base.py +131 -179
  150. langchain_core/vectorstores/in_memory.py +113 -182
  151. langchain_core/vectorstores/utils.py +23 -17
  152. langchain_core/version.py +1 -1
  153. langchain_core-1.0.0.dist-info/METADATA +68 -0
  154. langchain_core-1.0.0.dist-info/RECORD +172 -0
  155. {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
  156. langchain_core/beta/__init__.py +0 -1
  157. langchain_core/beta/runnables/__init__.py +0 -1
  158. langchain_core/beta/runnables/context.py +0 -448
  159. langchain_core/memory.py +0 -116
  160. langchain_core/messages/content_blocks.py +0 -1435
  161. langchain_core/prompts/pipeline.py +0 -133
  162. langchain_core/pydantic_v1/__init__.py +0 -30
  163. langchain_core/pydantic_v1/dataclasses.py +0 -23
  164. langchain_core/pydantic_v1/main.py +0 -23
  165. langchain_core/tracers/langchain_v1.py +0 -23
  166. langchain_core/utils/loading.py +0 -31
  167. langchain_core/v1/__init__.py +0 -1
  168. langchain_core/v1/chat_models.py +0 -1047
  169. langchain_core/v1/messages.py +0 -755
  170. langchain_core-0.4.0.dev0.dist-info/METADATA +0 -108
  171. langchain_core-0.4.0.dev0.dist-info/RECORD +0 -177
  172. langchain_core-0.4.0.dev0.dist-info/entry_points.txt +0 -4
@@ -3,12 +3,10 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
- from typing import TYPE_CHECKING, Any, Optional, Union
6
+ from typing import TYPE_CHECKING, Any
7
7
 
8
8
  from typing_extensions import Self
9
9
 
10
- from langchain_core.v1.messages import AIMessage, AIMessageChunk, MessageV1
11
-
12
10
  if TYPE_CHECKING:
13
11
  from collections.abc import Sequence
14
12
  from uuid import UUID
@@ -31,16 +29,16 @@ class RetrieverManagerMixin:
31
29
  error: BaseException,
32
30
  *,
33
31
  run_id: UUID,
34
- parent_run_id: Optional[UUID] = None,
32
+ parent_run_id: UUID | None = None,
35
33
  **kwargs: Any,
36
34
  ) -> Any:
37
35
  """Run when Retriever errors.
38
36
 
39
37
  Args:
40
- error (BaseException): The error that occurred.
41
- run_id (UUID): The run ID. This is the ID of the current run.
42
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
43
- kwargs (Any): Additional keyword arguments.
38
+ error: The error that occurred.
39
+ run_id: The run ID. This is the ID of the current run.
40
+ parent_run_id: The parent run ID. This is the ID of the parent run.
41
+ **kwargs: Additional keyword arguments.
44
42
  """
45
43
 
46
44
  def on_retriever_end(
@@ -48,16 +46,16 @@ class RetrieverManagerMixin:
48
46
  documents: Sequence[Document],
49
47
  *,
50
48
  run_id: UUID,
51
- parent_run_id: Optional[UUID] = None,
49
+ parent_run_id: UUID | None = None,
52
50
  **kwargs: Any,
53
51
  ) -> Any:
54
52
  """Run when Retriever ends running.
55
53
 
56
54
  Args:
57
- documents (Sequence[Document]): The documents retrieved.
58
- run_id (UUID): The run ID. This is the ID of the current run.
59
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
60
- kwargs (Any): Additional keyword arguments.
55
+ documents: The documents retrieved.
56
+ run_id: The run ID. This is the ID of the current run.
57
+ parent_run_id: The parent run ID. This is the ID of the parent run.
58
+ **kwargs: Additional keyword arguments.
61
59
  """
62
60
 
63
61
 
@@ -68,39 +66,38 @@ class LLMManagerMixin:
68
66
  self,
69
67
  token: str,
70
68
  *,
71
- chunk: Optional[
72
- Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
73
- ] = None,
69
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
74
70
  run_id: UUID,
75
- parent_run_id: Optional[UUID] = None,
71
+ parent_run_id: UUID | None = None,
76
72
  **kwargs: Any,
77
73
  ) -> Any:
78
- """Run on new LLM token. Only available when streaming is enabled.
74
+ """Run on new output token. Only available when streaming is enabled.
75
+
76
+ For both chat models and non-chat models (legacy LLMs).
79
77
 
80
78
  Args:
81
- token (str): The new token.
82
- chunk (GenerationChunk | ChatGenerationChunk | AIMessageChunk): The new
83
- generated chunk, containing content and other information.
84
- run_id (UUID): The run ID. This is the ID of the current run.
85
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
86
- kwargs (Any): Additional keyword arguments.
79
+ token: The new token.
80
+ chunk: The new generated chunk, containing content and other information.
81
+ run_id: The run ID. This is the ID of the current run.
82
+ parent_run_id: The parent run ID. This is the ID of the parent run.
83
+ **kwargs: Additional keyword arguments.
87
84
  """
88
85
 
89
86
  def on_llm_end(
90
87
  self,
91
- response: Union[LLMResult, AIMessage],
88
+ response: LLMResult,
92
89
  *,
93
90
  run_id: UUID,
94
- parent_run_id: Optional[UUID] = None,
91
+ parent_run_id: UUID | None = None,
95
92
  **kwargs: Any,
96
93
  ) -> Any:
97
94
  """Run when LLM ends running.
98
95
 
99
96
  Args:
100
- response (LLMResult | AIMessage): The response which was generated.
101
- run_id (UUID): The run ID. This is the ID of the current run.
102
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
103
- kwargs (Any): Additional keyword arguments.
97
+ response: The response which was generated.
98
+ run_id: The run ID. This is the ID of the current run.
99
+ parent_run_id: The parent run ID. This is the ID of the parent run.
100
+ **kwargs: Additional keyword arguments.
104
101
  """
105
102
 
106
103
  def on_llm_error(
@@ -108,16 +105,16 @@ class LLMManagerMixin:
108
105
  error: BaseException,
109
106
  *,
110
107
  run_id: UUID,
111
- parent_run_id: Optional[UUID] = None,
108
+ parent_run_id: UUID | None = None,
112
109
  **kwargs: Any,
113
110
  ) -> Any:
114
111
  """Run when LLM errors.
115
112
 
116
113
  Args:
117
- error (BaseException): The error that occurred.
118
- run_id (UUID): The run ID. This is the ID of the current run.
119
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
120
- kwargs (Any): Additional keyword arguments.
114
+ error: The error that occurred.
115
+ run_id: The run ID. This is the ID of the current run.
116
+ parent_run_id: The parent run ID. This is the ID of the parent run.
117
+ **kwargs: Additional keyword arguments.
121
118
  """
122
119
 
123
120
 
@@ -129,16 +126,16 @@ class ChainManagerMixin:
129
126
  outputs: dict[str, Any],
130
127
  *,
131
128
  run_id: UUID,
132
- parent_run_id: Optional[UUID] = None,
129
+ parent_run_id: UUID | None = None,
133
130
  **kwargs: Any,
134
131
  ) -> Any:
135
132
  """Run when chain ends running.
136
133
 
137
134
  Args:
138
- outputs (dict[str, Any]): The outputs of the chain.
139
- run_id (UUID): The run ID. This is the ID of the current run.
140
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
141
- kwargs (Any): Additional keyword arguments.
135
+ outputs: The outputs of the chain.
136
+ run_id: The run ID. This is the ID of the current run.
137
+ parent_run_id: The parent run ID. This is the ID of the parent run.
138
+ **kwargs: Additional keyword arguments.
142
139
  """
143
140
 
144
141
  def on_chain_error(
@@ -146,16 +143,16 @@ class ChainManagerMixin:
146
143
  error: BaseException,
147
144
  *,
148
145
  run_id: UUID,
149
- parent_run_id: Optional[UUID] = None,
146
+ parent_run_id: UUID | None = None,
150
147
  **kwargs: Any,
151
148
  ) -> Any:
152
149
  """Run when chain errors.
153
150
 
154
151
  Args:
155
- error (BaseException): The error that occurred.
156
- run_id (UUID): The run ID. This is the ID of the current run.
157
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
158
- kwargs (Any): Additional keyword arguments.
152
+ error: The error that occurred.
153
+ run_id: The run ID. This is the ID of the current run.
154
+ parent_run_id: The parent run ID. This is the ID of the parent run.
155
+ **kwargs: Additional keyword arguments.
159
156
  """
160
157
 
161
158
  def on_agent_action(
@@ -163,16 +160,16 @@ class ChainManagerMixin:
163
160
  action: AgentAction,
164
161
  *,
165
162
  run_id: UUID,
166
- parent_run_id: Optional[UUID] = None,
163
+ parent_run_id: UUID | None = None,
167
164
  **kwargs: Any,
168
165
  ) -> Any:
169
166
  """Run on agent action.
170
167
 
171
168
  Args:
172
- action (AgentAction): The agent action.
173
- run_id (UUID): The run ID. This is the ID of the current run.
174
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
175
- kwargs (Any): Additional keyword arguments.
169
+ action: The agent action.
170
+ run_id: The run ID. This is the ID of the current run.
171
+ parent_run_id: The parent run ID. This is the ID of the parent run.
172
+ **kwargs: Additional keyword arguments.
176
173
  """
177
174
 
178
175
  def on_agent_finish(
@@ -180,16 +177,16 @@ class ChainManagerMixin:
180
177
  finish: AgentFinish,
181
178
  *,
182
179
  run_id: UUID,
183
- parent_run_id: Optional[UUID] = None,
180
+ parent_run_id: UUID | None = None,
184
181
  **kwargs: Any,
185
182
  ) -> Any:
186
183
  """Run on the agent end.
187
184
 
188
185
  Args:
189
- finish (AgentFinish): The agent finish.
190
- run_id (UUID): The run ID. This is the ID of the current run.
191
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
192
- kwargs (Any): Additional keyword arguments.
186
+ finish: The agent finish.
187
+ run_id: The run ID. This is the ID of the current run.
188
+ parent_run_id: The parent run ID. This is the ID of the parent run.
189
+ **kwargs: Additional keyword arguments.
193
190
  """
194
191
 
195
192
 
@@ -201,16 +198,16 @@ class ToolManagerMixin:
201
198
  output: Any,
202
199
  *,
203
200
  run_id: UUID,
204
- parent_run_id: Optional[UUID] = None,
201
+ parent_run_id: UUID | None = None,
205
202
  **kwargs: Any,
206
203
  ) -> Any:
207
204
  """Run when the tool ends running.
208
205
 
209
206
  Args:
210
- output (Any): The output of the tool.
211
- run_id (UUID): The run ID. This is the ID of the current run.
212
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
213
- kwargs (Any): Additional keyword arguments.
207
+ output: The output of the tool.
208
+ run_id: The run ID. This is the ID of the current run.
209
+ parent_run_id: The parent run ID. This is the ID of the parent run.
210
+ **kwargs: Additional keyword arguments.
214
211
  """
215
212
 
216
213
  def on_tool_error(
@@ -218,16 +215,16 @@ class ToolManagerMixin:
218
215
  error: BaseException,
219
216
  *,
220
217
  run_id: UUID,
221
- parent_run_id: Optional[UUID] = None,
218
+ parent_run_id: UUID | None = None,
222
219
  **kwargs: Any,
223
220
  ) -> Any:
224
221
  """Run when tool errors.
225
222
 
226
223
  Args:
227
- error (BaseException): The error that occurred.
228
- run_id (UUID): The run ID. This is the ID of the current run.
229
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
230
- kwargs (Any): Additional keyword arguments.
224
+ error: The error that occurred.
225
+ run_id: The run ID. This is the ID of the current run.
226
+ parent_run_id: The parent run ID. This is the ID of the parent run.
227
+ **kwargs: Additional keyword arguments.
231
228
  """
232
229
 
233
230
 
@@ -240,52 +237,53 @@ class CallbackManagerMixin:
240
237
  prompts: list[str],
241
238
  *,
242
239
  run_id: UUID,
243
- parent_run_id: Optional[UUID] = None,
244
- tags: Optional[list[str]] = None,
245
- metadata: Optional[dict[str, Any]] = None,
240
+ parent_run_id: UUID | None = None,
241
+ tags: list[str] | None = None,
242
+ metadata: dict[str, Any] | None = None,
246
243
  **kwargs: Any,
247
244
  ) -> Any:
248
245
  """Run when LLM starts running.
249
246
 
250
- .. ATTENTION::
247
+ !!! warning
251
248
  This method is called for non-chat models (regular LLMs). If you're
252
249
  implementing a handler for a chat model, you should use
253
- ``on_chat_model_start`` instead.
250
+ `on_chat_model_start` instead.
254
251
 
255
252
  Args:
256
- serialized (dict[str, Any]): The serialized LLM.
257
- prompts (list[str]): The prompts.
258
- run_id (UUID): The run ID. This is the ID of the current run.
259
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
260
- tags (Optional[list[str]]): The tags.
261
- metadata (Optional[dict[str, Any]]): The metadata.
262
- kwargs (Any): Additional keyword arguments.
253
+ serialized: The serialized LLM.
254
+ prompts: The prompts.
255
+ run_id: The run ID. This is the ID of the current run.
256
+ parent_run_id: The parent run ID. This is the ID of the parent run.
257
+ tags: The tags.
258
+ metadata: The metadata.
259
+ **kwargs: Additional keyword arguments.
263
260
  """
264
261
 
265
262
  def on_chat_model_start(
266
263
  self,
267
264
  serialized: dict[str, Any],
268
- messages: Union[list[list[BaseMessage]], list[MessageV1]],
265
+ messages: list[list[BaseMessage]],
269
266
  *,
270
267
  run_id: UUID,
271
- parent_run_id: Optional[UUID] = None,
272
- tags: Optional[list[str]] = None,
273
- metadata: Optional[dict[str, Any]] = None,
268
+ parent_run_id: UUID | None = None,
269
+ tags: list[str] | None = None,
270
+ metadata: dict[str, Any] | None = None,
274
271
  **kwargs: Any,
275
272
  ) -> Any:
276
273
  """Run when a chat model starts running.
277
274
 
278
- **ATTENTION**: This method is called for chat models. If you're implementing
279
- a handler for a non-chat model, you should use ``on_llm_start`` instead.
275
+ !!! warning
276
+ This method is called for chat models. If you're implementing a handler for
277
+ a non-chat model, you should use `on_llm_start` instead.
280
278
 
281
279
  Args:
282
- serialized (dict[str, Any]): The serialized chat model.
283
- messages (list[list[BaseMessage]]): The messages.
284
- run_id (UUID): The run ID. This is the ID of the current run.
285
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
286
- tags (Optional[list[str]]): The tags.
287
- metadata (Optional[dict[str, Any]]): The metadata.
288
- kwargs (Any): Additional keyword arguments.
280
+ serialized: The serialized chat model.
281
+ messages: The messages.
282
+ run_id: The run ID. This is the ID of the current run.
283
+ parent_run_id: The parent run ID. This is the ID of the parent run.
284
+ tags: The tags.
285
+ metadata: The metadata.
286
+ **kwargs: Additional keyword arguments.
289
287
  """
290
288
  # NotImplementedError is thrown intentionally
291
289
  # Callback handler will fall back to on_llm_start if this is exception is thrown
@@ -298,21 +296,21 @@ class CallbackManagerMixin:
298
296
  query: str,
299
297
  *,
300
298
  run_id: UUID,
301
- parent_run_id: Optional[UUID] = None,
302
- tags: Optional[list[str]] = None,
303
- metadata: Optional[dict[str, Any]] = None,
299
+ parent_run_id: UUID | None = None,
300
+ tags: list[str] | None = None,
301
+ metadata: dict[str, Any] | None = None,
304
302
  **kwargs: Any,
305
303
  ) -> Any:
306
304
  """Run when the Retriever starts running.
307
305
 
308
306
  Args:
309
- serialized (dict[str, Any]): The serialized Retriever.
310
- query (str): The query.
311
- run_id (UUID): The run ID. This is the ID of the current run.
312
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
313
- tags (Optional[list[str]]): The tags.
314
- metadata (Optional[dict[str, Any]]): The metadata.
315
- kwargs (Any): Additional keyword arguments.
307
+ serialized: The serialized Retriever.
308
+ query: The query.
309
+ run_id: The run ID. This is the ID of the current run.
310
+ parent_run_id: The parent run ID. This is the ID of the parent run.
311
+ tags: The tags.
312
+ metadata: The metadata.
313
+ **kwargs: Additional keyword arguments.
316
314
  """
317
315
 
318
316
  def on_chain_start(
@@ -321,21 +319,21 @@ class CallbackManagerMixin:
321
319
  inputs: dict[str, Any],
322
320
  *,
323
321
  run_id: UUID,
324
- parent_run_id: Optional[UUID] = None,
325
- tags: Optional[list[str]] = None,
326
- metadata: Optional[dict[str, Any]] = None,
322
+ parent_run_id: UUID | None = None,
323
+ tags: list[str] | None = None,
324
+ metadata: dict[str, Any] | None = None,
327
325
  **kwargs: Any,
328
326
  ) -> Any:
329
327
  """Run when a chain starts running.
330
328
 
331
329
  Args:
332
- serialized (dict[str, Any]): The serialized chain.
333
- inputs (dict[str, Any]): The inputs.
334
- run_id (UUID): The run ID. This is the ID of the current run.
335
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
336
- tags (Optional[list[str]]): The tags.
337
- metadata (Optional[dict[str, Any]]): The metadata.
338
- kwargs (Any): Additional keyword arguments.
330
+ serialized: The serialized chain.
331
+ inputs: The inputs.
332
+ run_id: The run ID. This is the ID of the current run.
333
+ parent_run_id: The parent run ID. This is the ID of the parent run.
334
+ tags: The tags.
335
+ metadata: The metadata.
336
+ **kwargs: Additional keyword arguments.
339
337
  """
340
338
 
341
339
  def on_tool_start(
@@ -344,23 +342,23 @@ class CallbackManagerMixin:
344
342
  input_str: str,
345
343
  *,
346
344
  run_id: UUID,
347
- parent_run_id: Optional[UUID] = None,
348
- tags: Optional[list[str]] = None,
349
- metadata: Optional[dict[str, Any]] = None,
350
- inputs: Optional[dict[str, Any]] = None,
345
+ parent_run_id: UUID | None = None,
346
+ tags: list[str] | None = None,
347
+ metadata: dict[str, Any] | None = None,
348
+ inputs: dict[str, Any] | None = None,
351
349
  **kwargs: Any,
352
350
  ) -> Any:
353
351
  """Run when the tool starts running.
354
352
 
355
353
  Args:
356
- serialized (dict[str, Any]): The serialized tool.
357
- input_str (str): The input string.
358
- run_id (UUID): The run ID. This is the ID of the current run.
359
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
360
- tags (Optional[list[str]]): The tags.
361
- metadata (Optional[dict[str, Any]]): The metadata.
362
- inputs (Optional[dict[str, Any]]): The inputs.
363
- kwargs (Any): Additional keyword arguments.
354
+ serialized: The serialized chain.
355
+ input_str: The input string.
356
+ run_id: The run ID. This is the ID of the current run.
357
+ parent_run_id: The parent run ID. This is the ID of the parent run.
358
+ tags: The tags.
359
+ metadata: The metadata.
360
+ inputs: The inputs.
361
+ **kwargs: Additional keyword arguments.
364
362
  """
365
363
 
366
364
 
@@ -372,16 +370,16 @@ class RunManagerMixin:
372
370
  text: str,
373
371
  *,
374
372
  run_id: UUID,
375
- parent_run_id: Optional[UUID] = None,
373
+ parent_run_id: UUID | None = None,
376
374
  **kwargs: Any,
377
375
  ) -> Any:
378
376
  """Run on an arbitrary text.
379
377
 
380
378
  Args:
381
- text (str): The text.
382
- run_id (UUID): The run ID. This is the ID of the current run.
383
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
384
- kwargs (Any): Additional keyword arguments.
379
+ text: The text.
380
+ run_id: The run ID. This is the ID of the current run.
381
+ parent_run_id: The parent run ID. This is the ID of the parent run.
382
+ **kwargs: Additional keyword arguments.
385
383
  """
386
384
 
387
385
  def on_retry(
@@ -389,16 +387,16 @@ class RunManagerMixin:
389
387
  retry_state: RetryCallState,
390
388
  *,
391
389
  run_id: UUID,
392
- parent_run_id: Optional[UUID] = None,
390
+ parent_run_id: UUID | None = None,
393
391
  **kwargs: Any,
394
392
  ) -> Any:
395
393
  """Run on a retry event.
396
394
 
397
395
  Args:
398
- retry_state (RetryCallState): The retry state.
399
- run_id (UUID): The run ID. This is the ID of the current run.
400
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
401
- kwargs (Any): Additional keyword arguments.
396
+ retry_state: The retry state.
397
+ run_id: The run ID. This is the ID of the current run.
398
+ parent_run_id: The parent run ID. This is the ID of the parent run.
399
+ **kwargs: Additional keyword arguments.
402
400
  """
403
401
 
404
402
  def on_custom_event(
@@ -407,8 +405,8 @@ class RunManagerMixin:
407
405
  data: Any,
408
406
  *,
409
407
  run_id: UUID,
410
- tags: Optional[list[str]] = None,
411
- metadata: Optional[dict[str, Any]] = None,
408
+ tags: list[str] | None = None,
409
+ metadata: dict[str, Any] | None = None,
412
410
  **kwargs: Any,
413
411
  ) -> Any:
414
412
  """Override to define a handler for a custom event.
@@ -416,14 +414,14 @@ class RunManagerMixin:
416
414
  Args:
417
415
  name: The name of the custom event.
418
416
  data: The data for the custom event. Format will match
419
- the format specified by the user.
417
+ the format specified by the user.
420
418
  run_id: The ID of the run.
421
419
  tags: The tags associated with the custom event
422
420
  (includes inherited tags).
423
421
  metadata: The metadata associated with the custom event
424
422
  (includes inherited metadata).
425
423
 
426
- .. versionadded:: 0.2.15
424
+ !!! version-added "Added in version 0.2.15"
427
425
  """
428
426
 
429
427
 
@@ -443,9 +441,6 @@ class BaseCallbackHandler(
443
441
  run_inline: bool = False
444
442
  """Whether to run the callback inline."""
445
443
 
446
- accepts_new_messages: bool = False
447
- """Whether the callback accepts new message format."""
448
-
449
444
  @property
450
445
  def ignore_llm(self) -> bool:
451
446
  """Whether to ignore LLM callbacks."""
@@ -491,52 +486,53 @@ class AsyncCallbackHandler(BaseCallbackHandler):
491
486
  prompts: list[str],
492
487
  *,
493
488
  run_id: UUID,
494
- parent_run_id: Optional[UUID] = None,
495
- tags: Optional[list[str]] = None,
496
- metadata: Optional[dict[str, Any]] = None,
489
+ parent_run_id: UUID | None = None,
490
+ tags: list[str] | None = None,
491
+ metadata: dict[str, Any] | None = None,
497
492
  **kwargs: Any,
498
493
  ) -> None:
499
- """Run when LLM starts running.
494
+ """Run when the model starts running.
500
495
 
501
- .. ATTENTION::
496
+ !!! warning
502
497
  This method is called for non-chat models (regular LLMs). If you're
503
498
  implementing a handler for a chat model, you should use
504
- ``on_chat_model_start`` instead.
499
+ `on_chat_model_start` instead.
505
500
 
506
501
  Args:
507
- serialized (dict[str, Any]): The serialized LLM.
508
- prompts (list[str]): The prompts.
509
- run_id (UUID): The run ID. This is the ID of the current run.
510
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
511
- tags (Optional[list[str]]): The tags.
512
- metadata (Optional[dict[str, Any]]): The metadata.
513
- kwargs (Any): Additional keyword arguments.
502
+ serialized: The serialized LLM.
503
+ prompts: The prompts.
504
+ run_id: The run ID. This is the ID of the current run.
505
+ parent_run_id: The parent run ID. This is the ID of the parent run.
506
+ tags: The tags.
507
+ metadata: The metadata.
508
+ **kwargs: Additional keyword arguments.
514
509
  """
515
510
 
516
511
  async def on_chat_model_start(
517
512
  self,
518
513
  serialized: dict[str, Any],
519
- messages: Union[list[list[BaseMessage]], list[MessageV1]],
514
+ messages: list[list[BaseMessage]],
520
515
  *,
521
516
  run_id: UUID,
522
- parent_run_id: Optional[UUID] = None,
523
- tags: Optional[list[str]] = None,
524
- metadata: Optional[dict[str, Any]] = None,
517
+ parent_run_id: UUID | None = None,
518
+ tags: list[str] | None = None,
519
+ metadata: dict[str, Any] | None = None,
525
520
  **kwargs: Any,
526
521
  ) -> Any:
527
522
  """Run when a chat model starts running.
528
523
 
529
- **ATTENTION**: This method is called for chat models. If you're implementing
530
- a handler for a non-chat model, you should use ``on_llm_start`` instead.
524
+ !!! warning
525
+ This method is called for chat models. If you're implementing a handler for
526
+ a non-chat model, you should use `on_llm_start` instead.
531
527
 
532
528
  Args:
533
- serialized (dict[str, Any]): The serialized chat model.
534
- messages (list[list[BaseMessage]]): The messages.
535
- run_id (UUID): The run ID. This is the ID of the current run.
536
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
537
- tags (Optional[list[str]]): The tags.
538
- metadata (Optional[dict[str, Any]]): The metadata.
539
- kwargs (Any): Additional keyword arguments.
529
+ serialized: The serialized chat model.
530
+ messages: The messages.
531
+ run_id: The run ID. This is the ID of the current run.
532
+ parent_run_id: The parent run ID. This is the ID of the parent run.
533
+ tags: The tags.
534
+ metadata: The metadata.
535
+ **kwargs: Additional keyword arguments.
540
536
  """
541
537
  # NotImplementedError is thrown intentionally
542
538
  # Callback handler will fall back to on_llm_start if this is exception is thrown
@@ -547,43 +543,42 @@ class AsyncCallbackHandler(BaseCallbackHandler):
547
543
  self,
548
544
  token: str,
549
545
  *,
550
- chunk: Optional[
551
- Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
552
- ] = None,
546
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
553
547
  run_id: UUID,
554
- parent_run_id: Optional[UUID] = None,
555
- tags: Optional[list[str]] = None,
548
+ parent_run_id: UUID | None = None,
549
+ tags: list[str] | None = None,
556
550
  **kwargs: Any,
557
551
  ) -> None:
558
- """Run on new LLM token. Only available when streaming is enabled.
552
+ """Run on new output token. Only available when streaming is enabled.
553
+
554
+ For both chat models and non-chat models (legacy LLMs).
559
555
 
560
556
  Args:
561
- token (str): The new token.
562
- chunk (GenerationChunk | ChatGenerationChunk | AIMessageChunk): The new
563
- generated chunk, containing content and other information.
564
- run_id (UUID): The run ID. This is the ID of the current run.
565
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
566
- tags (Optional[list[str]]): The tags.
567
- kwargs (Any): Additional keyword arguments.
557
+ token: The new token.
558
+ chunk: The new generated chunk, containing content and other information.
559
+ run_id: The run ID. This is the ID of the current run.
560
+ parent_run_id: The parent run ID. This is the ID of the parent run.
561
+ tags: The tags.
562
+ **kwargs: Additional keyword arguments.
568
563
  """
569
564
 
570
565
  async def on_llm_end(
571
566
  self,
572
- response: Union[LLMResult, AIMessage],
567
+ response: LLMResult,
573
568
  *,
574
569
  run_id: UUID,
575
- parent_run_id: Optional[UUID] = None,
576
- tags: Optional[list[str]] = None,
570
+ parent_run_id: UUID | None = None,
571
+ tags: list[str] | None = None,
577
572
  **kwargs: Any,
578
573
  ) -> None:
579
- """Run when LLM ends running.
574
+ """Run when the model ends running.
580
575
 
581
576
  Args:
582
- response (LLMResult | AIMessage): The response which was generated.
583
- run_id (UUID): The run ID. This is the ID of the current run.
584
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
585
- tags (Optional[list[str]]): The tags.
586
- kwargs (Any): Additional keyword arguments.
577
+ response: The response which was generated.
578
+ run_id: The run ID. This is the ID of the current run.
579
+ parent_run_id: The parent run ID. This is the ID of the parent run.
580
+ tags: The tags.
581
+ **kwargs: Additional keyword arguments.
587
582
  """
588
583
 
589
584
  async def on_llm_error(
@@ -591,8 +586,8 @@ class AsyncCallbackHandler(BaseCallbackHandler):
591
586
  error: BaseException,
592
587
  *,
593
588
  run_id: UUID,
594
- parent_run_id: Optional[UUID] = None,
595
- tags: Optional[list[str]] = None,
589
+ parent_run_id: UUID | None = None,
590
+ tags: list[str] | None = None,
596
591
  **kwargs: Any,
597
592
  ) -> None:
598
593
  """Run when LLM errors.
@@ -602,9 +597,9 @@ class AsyncCallbackHandler(BaseCallbackHandler):
602
597
  run_id: The run ID. This is the ID of the current run.
603
598
  parent_run_id: The parent run ID. This is the ID of the parent run.
604
599
  tags: The tags.
605
- kwargs (Any): Additional keyword arguments.
606
- - response (LLMResult | AIMessage): The response which was generated
607
- before the error occurred.
600
+ **kwargs: Additional keyword arguments.
601
+ - response (LLMResult): The response which was generated before
602
+ the error occurred.
608
603
  """
609
604
 
610
605
  async def on_chain_start(
@@ -613,21 +608,21 @@ class AsyncCallbackHandler(BaseCallbackHandler):
613
608
  inputs: dict[str, Any],
614
609
  *,
615
610
  run_id: UUID,
616
- parent_run_id: Optional[UUID] = None,
617
- tags: Optional[list[str]] = None,
618
- metadata: Optional[dict[str, Any]] = None,
611
+ parent_run_id: UUID | None = None,
612
+ tags: list[str] | None = None,
613
+ metadata: dict[str, Any] | None = None,
619
614
  **kwargs: Any,
620
615
  ) -> None:
621
616
  """Run when a chain starts running.
622
617
 
623
618
  Args:
624
- serialized (dict[str, Any]): The serialized chain.
625
- inputs (dict[str, Any]): The inputs.
626
- run_id (UUID): The run ID. This is the ID of the current run.
627
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
628
- tags (Optional[list[str]]): The tags.
629
- metadata (Optional[dict[str, Any]]): The metadata.
630
- kwargs (Any): Additional keyword arguments.
619
+ serialized: The serialized chain.
620
+ inputs: The inputs.
621
+ run_id: The run ID. This is the ID of the current run.
622
+ parent_run_id: The parent run ID. This is the ID of the parent run.
623
+ tags: The tags.
624
+ metadata: The metadata.
625
+ **kwargs: Additional keyword arguments.
631
626
  """
632
627
 
633
628
  async def on_chain_end(
@@ -635,18 +630,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
635
630
  outputs: dict[str, Any],
636
631
  *,
637
632
  run_id: UUID,
638
- parent_run_id: Optional[UUID] = None,
639
- tags: Optional[list[str]] = None,
633
+ parent_run_id: UUID | None = None,
634
+ tags: list[str] | None = None,
640
635
  **kwargs: Any,
641
636
  ) -> None:
642
637
  """Run when a chain ends running.
643
638
 
644
639
  Args:
645
- outputs (dict[str, Any]): The outputs of the chain.
646
- run_id (UUID): The run ID. This is the ID of the current run.
647
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
648
- tags (Optional[list[str]]): The tags.
649
- kwargs (Any): Additional keyword arguments.
640
+ outputs: The outputs of the chain.
641
+ run_id: The run ID. This is the ID of the current run.
642
+ parent_run_id: The parent run ID. This is the ID of the parent run.
643
+ tags: The tags.
644
+ **kwargs: Additional keyword arguments.
650
645
  """
651
646
 
652
647
  async def on_chain_error(
@@ -654,18 +649,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
654
649
  error: BaseException,
655
650
  *,
656
651
  run_id: UUID,
657
- parent_run_id: Optional[UUID] = None,
658
- tags: Optional[list[str]] = None,
652
+ parent_run_id: UUID | None = None,
653
+ tags: list[str] | None = None,
659
654
  **kwargs: Any,
660
655
  ) -> None:
661
656
  """Run when chain errors.
662
657
 
663
658
  Args:
664
- error (BaseException): The error that occurred.
665
- run_id (UUID): The run ID. This is the ID of the current run.
666
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
667
- tags (Optional[list[str]]): The tags.
668
- kwargs (Any): Additional keyword arguments.
659
+ error: The error that occurred.
660
+ run_id: The run ID. This is the ID of the current run.
661
+ parent_run_id: The parent run ID. This is the ID of the parent run.
662
+ tags: The tags.
663
+ **kwargs: Additional keyword arguments.
669
664
  """
670
665
 
671
666
  async def on_tool_start(
@@ -674,23 +669,23 @@ class AsyncCallbackHandler(BaseCallbackHandler):
674
669
  input_str: str,
675
670
  *,
676
671
  run_id: UUID,
677
- parent_run_id: Optional[UUID] = None,
678
- tags: Optional[list[str]] = None,
679
- metadata: Optional[dict[str, Any]] = None,
680
- inputs: Optional[dict[str, Any]] = None,
672
+ parent_run_id: UUID | None = None,
673
+ tags: list[str] | None = None,
674
+ metadata: dict[str, Any] | None = None,
675
+ inputs: dict[str, Any] | None = None,
681
676
  **kwargs: Any,
682
677
  ) -> None:
683
678
  """Run when the tool starts running.
684
679
 
685
680
  Args:
686
- serialized (dict[str, Any]): The serialized tool.
687
- input_str (str): The input string.
688
- run_id (UUID): The run ID. This is the ID of the current run.
689
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
690
- tags (Optional[list[str]]): The tags.
691
- metadata (Optional[dict[str, Any]]): The metadata.
692
- inputs (Optional[dict[str, Any]]): The inputs.
693
- kwargs (Any): Additional keyword arguments.
681
+ serialized: The serialized tool.
682
+ input_str: The input string.
683
+ run_id: The run ID. This is the ID of the current run.
684
+ parent_run_id: The parent run ID. This is the ID of the parent run.
685
+ tags: The tags.
686
+ metadata: The metadata.
687
+ inputs: The inputs.
688
+ **kwargs: Additional keyword arguments.
694
689
  """
695
690
 
696
691
  async def on_tool_end(
@@ -698,18 +693,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
698
693
  output: Any,
699
694
  *,
700
695
  run_id: UUID,
701
- parent_run_id: Optional[UUID] = None,
702
- tags: Optional[list[str]] = None,
696
+ parent_run_id: UUID | None = None,
697
+ tags: list[str] | None = None,
703
698
  **kwargs: Any,
704
699
  ) -> None:
705
700
  """Run when the tool ends running.
706
701
 
707
702
  Args:
708
- output (Any): The output of the tool.
709
- run_id (UUID): The run ID. This is the ID of the current run.
710
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
711
- tags (Optional[list[str]]): The tags.
712
- kwargs (Any): Additional keyword arguments.
703
+ output: The output of the tool.
704
+ run_id: The run ID. This is the ID of the current run.
705
+ parent_run_id: The parent run ID. This is the ID of the parent run.
706
+ tags: The tags.
707
+ **kwargs: Additional keyword arguments.
713
708
  """
714
709
 
715
710
  async def on_tool_error(
@@ -717,18 +712,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
717
712
  error: BaseException,
718
713
  *,
719
714
  run_id: UUID,
720
- parent_run_id: Optional[UUID] = None,
721
- tags: Optional[list[str]] = None,
715
+ parent_run_id: UUID | None = None,
716
+ tags: list[str] | None = None,
722
717
  **kwargs: Any,
723
718
  ) -> None:
724
719
  """Run when tool errors.
725
720
 
726
721
  Args:
727
- error (BaseException): The error that occurred.
728
- run_id (UUID): The run ID. This is the ID of the current run.
729
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
730
- tags (Optional[list[str]]): The tags.
731
- kwargs (Any): Additional keyword arguments.
722
+ error: The error that occurred.
723
+ run_id: The run ID. This is the ID of the current run.
724
+ parent_run_id: The parent run ID. This is the ID of the parent run.
725
+ tags: The tags.
726
+ **kwargs: Additional keyword arguments.
732
727
  """
733
728
 
734
729
  async def on_text(
@@ -736,18 +731,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
736
731
  text: str,
737
732
  *,
738
733
  run_id: UUID,
739
- parent_run_id: Optional[UUID] = None,
740
- tags: Optional[list[str]] = None,
734
+ parent_run_id: UUID | None = None,
735
+ tags: list[str] | None = None,
741
736
  **kwargs: Any,
742
737
  ) -> None:
743
738
  """Run on an arbitrary text.
744
739
 
745
740
  Args:
746
- text (str): The text.
747
- run_id (UUID): The run ID. This is the ID of the current run.
748
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
749
- tags (Optional[list[str]]): The tags.
750
- kwargs (Any): Additional keyword arguments.
741
+ text: The text.
742
+ run_id: The run ID. This is the ID of the current run.
743
+ parent_run_id: The parent run ID. This is the ID of the parent run.
744
+ tags: The tags.
745
+ **kwargs: Additional keyword arguments.
751
746
  """
752
747
 
753
748
  async def on_retry(
@@ -755,16 +750,16 @@ class AsyncCallbackHandler(BaseCallbackHandler):
755
750
  retry_state: RetryCallState,
756
751
  *,
757
752
  run_id: UUID,
758
- parent_run_id: Optional[UUID] = None,
753
+ parent_run_id: UUID | None = None,
759
754
  **kwargs: Any,
760
755
  ) -> Any:
761
756
  """Run on a retry event.
762
757
 
763
758
  Args:
764
- retry_state (RetryCallState): The retry state.
765
- run_id (UUID): The run ID. This is the ID of the current run.
766
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
767
- kwargs (Any): Additional keyword arguments.
759
+ retry_state: The retry state.
760
+ run_id: The run ID. This is the ID of the current run.
761
+ parent_run_id: The parent run ID. This is the ID of the parent run.
762
+ **kwargs: Additional keyword arguments.
768
763
  """
769
764
 
770
765
  async def on_agent_action(
@@ -772,18 +767,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
772
767
  action: AgentAction,
773
768
  *,
774
769
  run_id: UUID,
775
- parent_run_id: Optional[UUID] = None,
776
- tags: Optional[list[str]] = None,
770
+ parent_run_id: UUID | None = None,
771
+ tags: list[str] | None = None,
777
772
  **kwargs: Any,
778
773
  ) -> None:
779
774
  """Run on agent action.
780
775
 
781
776
  Args:
782
- action (AgentAction): The agent action.
783
- run_id (UUID): The run ID. This is the ID of the current run.
784
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
785
- tags (Optional[list[str]]): The tags.
786
- kwargs (Any): Additional keyword arguments.
777
+ action: The agent action.
778
+ run_id: The run ID. This is the ID of the current run.
779
+ parent_run_id: The parent run ID. This is the ID of the parent run.
780
+ tags: The tags.
781
+ **kwargs: Additional keyword arguments.
787
782
  """
788
783
 
789
784
  async def on_agent_finish(
@@ -791,18 +786,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
791
786
  finish: AgentFinish,
792
787
  *,
793
788
  run_id: UUID,
794
- parent_run_id: Optional[UUID] = None,
795
- tags: Optional[list[str]] = None,
789
+ parent_run_id: UUID | None = None,
790
+ tags: list[str] | None = None,
796
791
  **kwargs: Any,
797
792
  ) -> None:
798
793
  """Run on the agent end.
799
794
 
800
795
  Args:
801
- finish (AgentFinish): The agent finish.
802
- run_id (UUID): The run ID. This is the ID of the current run.
803
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
804
- tags (Optional[list[str]]): The tags.
805
- kwargs (Any): Additional keyword arguments.
796
+ finish: The agent finish.
797
+ run_id: The run ID. This is the ID of the current run.
798
+ parent_run_id: The parent run ID. This is the ID of the parent run.
799
+ tags: The tags.
800
+ **kwargs: Additional keyword arguments.
806
801
  """
807
802
 
808
803
  async def on_retriever_start(
@@ -811,21 +806,21 @@ class AsyncCallbackHandler(BaseCallbackHandler):
811
806
  query: str,
812
807
  *,
813
808
  run_id: UUID,
814
- parent_run_id: Optional[UUID] = None,
815
- tags: Optional[list[str]] = None,
816
- metadata: Optional[dict[str, Any]] = None,
809
+ parent_run_id: UUID | None = None,
810
+ tags: list[str] | None = None,
811
+ metadata: dict[str, Any] | None = None,
817
812
  **kwargs: Any,
818
813
  ) -> None:
819
814
  """Run on the retriever start.
820
815
 
821
816
  Args:
822
- serialized (dict[str, Any]): The serialized retriever.
823
- query (str): The query.
824
- run_id (UUID): The run ID. This is the ID of the current run.
825
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
826
- tags (Optional[list[str]]): The tags.
827
- metadata (Optional[dict[str, Any]]): The metadata.
828
- kwargs (Any): Additional keyword arguments.
817
+ serialized: The serialized retriever.
818
+ query: The query.
819
+ run_id: The run ID. This is the ID of the current run.
820
+ parent_run_id: The parent run ID. This is the ID of the parent run.
821
+ tags: The tags.
822
+ metadata: The metadata.
823
+ **kwargs: Additional keyword arguments.
829
824
  """
830
825
 
831
826
  async def on_retriever_end(
@@ -833,18 +828,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
833
828
  documents: Sequence[Document],
834
829
  *,
835
830
  run_id: UUID,
836
- parent_run_id: Optional[UUID] = None,
837
- tags: Optional[list[str]] = None,
831
+ parent_run_id: UUID | None = None,
832
+ tags: list[str] | None = None,
838
833
  **kwargs: Any,
839
834
  ) -> None:
840
835
  """Run on the retriever end.
841
836
 
842
837
  Args:
843
- documents (Sequence[Document]): The documents retrieved.
844
- run_id (UUID): The run ID. This is the ID of the current run.
845
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
846
- tags (Optional[list[str]]): The tags.
847
- kwargs (Any): Additional keyword arguments.
838
+ documents: The documents retrieved.
839
+ run_id: The run ID. This is the ID of the current run.
840
+ parent_run_id: The parent run ID. This is the ID of the parent run.
841
+ tags: The tags.
842
+ **kwargs: Additional keyword arguments.
848
843
  """
849
844
 
850
845
  async def on_retriever_error(
@@ -852,18 +847,18 @@ class AsyncCallbackHandler(BaseCallbackHandler):
852
847
  error: BaseException,
853
848
  *,
854
849
  run_id: UUID,
855
- parent_run_id: Optional[UUID] = None,
856
- tags: Optional[list[str]] = None,
850
+ parent_run_id: UUID | None = None,
851
+ tags: list[str] | None = None,
857
852
  **kwargs: Any,
858
853
  ) -> None:
859
854
  """Run on retriever error.
860
855
 
861
856
  Args:
862
- error (BaseException): The error that occurred.
863
- run_id (UUID): The run ID. This is the ID of the current run.
864
- parent_run_id (UUID): The parent run ID. This is the ID of the parent run.
865
- tags (Optional[list[str]]): The tags.
866
- kwargs (Any): Additional keyword arguments.
857
+ error: The error that occurred.
858
+ run_id: The run ID. This is the ID of the current run.
859
+ parent_run_id: The parent run ID. This is the ID of the parent run.
860
+ tags: The tags.
861
+ **kwargs: Additional keyword arguments.
867
862
  """
868
863
 
869
864
  async def on_custom_event(
@@ -872,23 +867,23 @@ class AsyncCallbackHandler(BaseCallbackHandler):
872
867
  data: Any,
873
868
  *,
874
869
  run_id: UUID,
875
- tags: Optional[list[str]] = None,
876
- metadata: Optional[dict[str, Any]] = None,
870
+ tags: list[str] | None = None,
871
+ metadata: dict[str, Any] | None = None,
877
872
  **kwargs: Any,
878
873
  ) -> None:
879
- """Override to define a handler for a custom event.
874
+ """Override to define a handler for custom events.
880
875
 
881
876
  Args:
882
877
  name: The name of the custom event.
883
878
  data: The data for the custom event. Format will match
884
- the format specified by the user.
879
+ the format specified by the user.
885
880
  run_id: The ID of the run.
886
881
  tags: The tags associated with the custom event
887
882
  (includes inherited tags).
888
883
  metadata: The metadata associated with the custom event
889
884
  (includes inherited metadata).
890
885
 
891
- .. versionadded:: 0.2.15
886
+ !!! version-added "Added in version 0.2.15"
892
887
  """
893
888
 
894
889
 
@@ -898,40 +893,37 @@ class BaseCallbackManager(CallbackManagerMixin):
898
893
  def __init__(
899
894
  self,
900
895
  handlers: list[BaseCallbackHandler],
901
- inheritable_handlers: Optional[list[BaseCallbackHandler]] = None,
902
- parent_run_id: Optional[UUID] = None,
896
+ inheritable_handlers: list[BaseCallbackHandler] | None = None,
897
+ parent_run_id: UUID | None = None,
903
898
  *,
904
- tags: Optional[list[str]] = None,
905
- inheritable_tags: Optional[list[str]] = None,
906
- metadata: Optional[dict[str, Any]] = None,
907
- inheritable_metadata: Optional[dict[str, Any]] = None,
899
+ tags: list[str] | None = None,
900
+ inheritable_tags: list[str] | None = None,
901
+ metadata: dict[str, Any] | None = None,
902
+ inheritable_metadata: dict[str, Any] | None = None,
908
903
  ) -> None:
909
904
  """Initialize callback manager.
910
905
 
911
906
  Args:
912
- handlers (list[BaseCallbackHandler]): The handlers.
913
- inheritable_handlers (Optional[list[BaseCallbackHandler]]):
914
- The inheritable handlers. Default is None.
915
- parent_run_id (Optional[UUID]): The parent run ID. Default is None.
916
- tags (Optional[list[str]]): The tags. Default is None.
917
- inheritable_tags (Optional[list[str]]): The inheritable tags.
918
- Default is None.
919
- metadata (Optional[dict[str, Any]]): The metadata. Default is None.
920
- inheritable_metadata (Optional[dict[str, Any]]): The inheritable metadata.
921
- Default is None.
907
+ handlers: The handlers.
908
+ inheritable_handlers: The inheritable handlers.
909
+ parent_run_id: The parent run ID.
910
+ tags: The tags.
911
+ inheritable_tags: The inheritable tags.
912
+ metadata: The metadata.
913
+ inheritable_metadata: The inheritable metadata.
922
914
  """
923
915
  self.handlers: list[BaseCallbackHandler] = handlers
924
916
  self.inheritable_handlers: list[BaseCallbackHandler] = (
925
917
  inheritable_handlers or []
926
918
  )
927
- self.parent_run_id: Optional[UUID] = parent_run_id
919
+ self.parent_run_id: UUID | None = parent_run_id
928
920
  self.tags = tags or []
929
921
  self.inheritable_tags = inheritable_tags or []
930
922
  self.metadata = metadata or {}
931
923
  self.inheritable_metadata = inheritable_metadata or {}
932
924
 
933
925
  def copy(self) -> Self:
934
- """Copy the callback manager."""
926
+ """Return a copy of the callback manager."""
935
927
  return self.__class__(
936
928
  handlers=self.handlers.copy(),
937
929
  inheritable_handlers=self.inheritable_handlers.copy(),
@@ -949,28 +941,29 @@ class BaseCallbackManager(CallbackManagerMixin):
949
941
  within merge_configs.
950
942
 
951
943
  Returns:
952
- BaseCallbackManager: The merged callback manager of the same type
953
- as the current object.
944
+ The merged callback manager of the same type as the current object.
954
945
 
955
946
  Example: Merging two callback managers.
956
947
 
957
- .. code-block:: python
958
-
959
- from langchain_core.callbacks.manager import CallbackManager, trace_as_chain_group
960
- from langchain_core.callbacks.stdout import StdOutCallbackHandler
961
-
962
- manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
963
- with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
964
- merged_manager = group_manager.merge(manager)
965
- print(merged_manager.handlers)
966
- # [
967
- # <langchain_core.callbacks.stdout.StdOutCallbackHandler object at ...>,
968
- # <langchain_core.callbacks.streaming_stdout.StreamingStdOutCallbackHandler object at ...>,
969
- # ]
970
-
971
- print(merged_manager.tags)
972
- # ['tag2', 'tag1']
973
-
948
+ ```python
949
+ from langchain_core.callbacks.manager import (
950
+ CallbackManager,
951
+ trace_as_chain_group,
952
+ )
953
+ from langchain_core.callbacks.stdout import StdOutCallbackHandler
954
+
955
+ manager = CallbackManager(handlers=[StdOutCallbackHandler()], tags=["tag2"])
956
+ with trace_as_chain_group("My Group Name", tags=["tag1"]) as group_manager:
957
+ merged_manager = group_manager.merge(manager)
958
+ print(merged_manager.handlers)
959
+ # [
960
+ # <langchain_core.callbacks.stdout.StdOutCallbackHandler object at ...>,
961
+ # <langchain_core.callbacks.streaming_stdout.StreamingStdOutCallbackHandler object at ...>,
962
+ # ]
963
+
964
+ print(merged_manager.tags)
965
+ # ['tag2', 'tag1']
966
+ ```
974
967
  """ # noqa: E501
975
968
  manager = self.__class__(
976
969
  parent_run_id=self.parent_run_id or other.parent_run_id,
@@ -1007,8 +1000,8 @@ class BaseCallbackManager(CallbackManagerMixin):
1007
1000
  """Add a handler to the callback manager.
1008
1001
 
1009
1002
  Args:
1010
- handler (BaseCallbackHandler): The handler to add.
1011
- inherit (bool): Whether to inherit the handler. Default is True.
1003
+ handler: The handler to add.
1004
+ inherit: Whether to inherit the handler.
1012
1005
  """
1013
1006
  if handler not in self.handlers:
1014
1007
  self.handlers.append(handler)
@@ -1019,7 +1012,7 @@ class BaseCallbackManager(CallbackManagerMixin):
1019
1012
  """Remove a handler from the callback manager.
1020
1013
 
1021
1014
  Args:
1022
- handler (BaseCallbackHandler): The handler to remove.
1015
+ handler: The handler to remove.
1023
1016
  """
1024
1017
  if handler in self.handlers:
1025
1018
  self.handlers.remove(handler)
@@ -1034,8 +1027,8 @@ class BaseCallbackManager(CallbackManagerMixin):
1034
1027
  """Set handlers as the only handlers on the callback manager.
1035
1028
 
1036
1029
  Args:
1037
- handlers (list[BaseCallbackHandler]): The handlers to set.
1038
- inherit (bool): Whether to inherit the handlers. Default is True.
1030
+ handlers: The handlers to set.
1031
+ inherit: Whether to inherit the handlers.
1039
1032
  """
1040
1033
  self.handlers = []
1041
1034
  self.inheritable_handlers = []
@@ -1050,8 +1043,8 @@ class BaseCallbackManager(CallbackManagerMixin):
1050
1043
  """Set handler as the only handler on the callback manager.
1051
1044
 
1052
1045
  Args:
1053
- handler (BaseCallbackHandler): The handler to set.
1054
- inherit (bool): Whether to inherit the handler. Default is True.
1046
+ handler: The handler to set.
1047
+ inherit: Whether to inherit the handler.
1055
1048
  """
1056
1049
  self.set_handlers([handler], inherit=inherit)
1057
1050
 
@@ -1063,8 +1056,8 @@ class BaseCallbackManager(CallbackManagerMixin):
1063
1056
  """Add tags to the callback manager.
1064
1057
 
1065
1058
  Args:
1066
- tags (list[str]): The tags to add.
1067
- inherit (bool): Whether to inherit the tags. Default is True.
1059
+ tags: The tags to add.
1060
+ inherit: Whether to inherit the tags.
1068
1061
  """
1069
1062
  for tag in tags:
1070
1063
  if tag in self.tags:
@@ -1077,7 +1070,7 @@ class BaseCallbackManager(CallbackManagerMixin):
1077
1070
  """Remove tags from the callback manager.
1078
1071
 
1079
1072
  Args:
1080
- tags (list[str]): The tags to remove.
1073
+ tags: The tags to remove.
1081
1074
  """
1082
1075
  for tag in tags:
1083
1076
  if tag in self.tags:
@@ -1093,8 +1086,8 @@ class BaseCallbackManager(CallbackManagerMixin):
1093
1086
  """Add metadata to the callback manager.
1094
1087
 
1095
1088
  Args:
1096
- metadata (dict[str, Any]): The metadata to add.
1097
- inherit (bool): Whether to inherit the metadata. Default is True.
1089
+ metadata: The metadata to add.
1090
+ inherit: Whether to inherit the metadata.
1098
1091
  """
1099
1092
  self.metadata.update(metadata)
1100
1093
  if inherit:
@@ -1104,11 +1097,11 @@ class BaseCallbackManager(CallbackManagerMixin):
1104
1097
  """Remove metadata from the callback manager.
1105
1098
 
1106
1099
  Args:
1107
- keys (list[str]): The keys to remove.
1100
+ keys: The keys to remove.
1108
1101
  """
1109
1102
  for key in keys:
1110
1103
  self.metadata.pop(key, None)
1111
1104
  self.inheritable_metadata.pop(key, None)
1112
1105
 
1113
1106
 
1114
- Callbacks = Optional[Union[list[BaseCallbackHandler], BaseCallbackManager]]
1107
+ Callbacks = list[BaseCallbackHandler] | BaseCallbackManager | None