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