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
@@ -8,8 +8,6 @@ from abc import ABC, abstractmethod
8
8
  from typing import (
9
9
  TYPE_CHECKING,
10
10
  Any,
11
- Optional,
12
- Union,
13
11
  )
14
12
 
15
13
  from typing_extensions import override
@@ -17,7 +15,6 @@ from typing_extensions import override
17
15
  from langchain_core.callbacks.base import AsyncCallbackHandler, BaseCallbackHandler
18
16
  from langchain_core.exceptions import TracerException # noqa: F401
19
17
  from langchain_core.tracers.core import _TracerCore
20
- from langchain_core.v1.messages import AIMessage, AIMessageChunk, MessageV1
21
18
 
22
19
  if TYPE_CHECKING:
23
20
  from collections.abc import Sequence
@@ -55,13 +52,13 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
55
52
  def on_chat_model_start(
56
53
  self,
57
54
  serialized: dict[str, Any],
58
- messages: Union[list[list[BaseMessage]], list[MessageV1]],
55
+ messages: list[list[BaseMessage]],
59
56
  *,
60
57
  run_id: UUID,
61
- tags: Optional[list[str]] = None,
62
- parent_run_id: Optional[UUID] = None,
63
- metadata: Optional[dict[str, Any]] = None,
64
- name: Optional[str] = None,
58
+ tags: list[str] | None = None,
59
+ parent_run_id: UUID | None = None,
60
+ metadata: dict[str, Any] | None = None,
61
+ name: str | None = None,
65
62
  **kwargs: Any,
66
63
  ) -> Run:
67
64
  """Start a trace for an LLM run.
@@ -70,11 +67,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
70
67
  serialized: The serialized model.
71
68
  messages: The messages to start the chat with.
72
69
  run_id: The run ID.
73
- tags: The tags for the run. Defaults to None.
74
- parent_run_id: The parent run ID. Defaults to None.
75
- metadata: The metadata for the run. Defaults to None.
70
+ tags: The tags for the run.
71
+ parent_run_id: The parent run ID.
72
+ metadata: The metadata for the run.
76
73
  name: The name of the run.
77
- kwargs: Additional arguments.
74
+ **kwargs: Additional arguments.
78
75
 
79
76
  Returns:
80
77
  The run.
@@ -99,10 +96,10 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
99
96
  prompts: list[str],
100
97
  *,
101
98
  run_id: UUID,
102
- tags: Optional[list[str]] = None,
103
- parent_run_id: Optional[UUID] = None,
104
- metadata: Optional[dict[str, Any]] = None,
105
- name: Optional[str] = None,
99
+ tags: list[str] | None = None,
100
+ parent_run_id: UUID | None = None,
101
+ metadata: dict[str, Any] | None = None,
102
+ name: str | None = None,
106
103
  **kwargs: Any,
107
104
  ) -> Run:
108
105
  """Start a trace for an LLM run.
@@ -111,11 +108,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
111
108
  serialized: The serialized model.
112
109
  prompts: The prompts to start the LLM with.
113
110
  run_id: The run ID.
114
- tags: The tags for the run. Defaults to None.
115
- parent_run_id: The parent run ID. Defaults to None.
116
- metadata: The metadata for the run. Defaults to None.
111
+ tags: The tags for the run.
112
+ parent_run_id: The parent run ID.
113
+ metadata: The metadata for the run.
117
114
  name: The name of the run.
118
- kwargs: Additional arguments.
115
+ **kwargs: Additional arguments.
119
116
 
120
117
  Returns:
121
118
  The run.
@@ -139,21 +136,19 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
139
136
  self,
140
137
  token: str,
141
138
  *,
142
- chunk: Optional[
143
- Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
144
- ] = None,
139
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
145
140
  run_id: UUID,
146
- parent_run_id: Optional[UUID] = None,
141
+ parent_run_id: UUID | None = None,
147
142
  **kwargs: Any,
148
143
  ) -> Run:
149
144
  """Run on new LLM token. Only available when streaming is enabled.
150
145
 
151
146
  Args:
152
147
  token: The token.
153
- chunk: The chunk. Defaults to None.
148
+ chunk: The chunk.
154
149
  run_id: The run ID.
155
- parent_run_id: The parent run ID. Defaults to None.
156
- kwargs: Additional arguments.
150
+ parent_run_id: The parent run ID.
151
+ **kwargs: Additional arguments.
157
152
 
158
153
  Returns:
159
154
  The run.
@@ -182,7 +177,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
182
177
  Args:
183
178
  retry_state: The retry state.
184
179
  run_id: The run ID.
185
- kwargs: Additional arguments.
180
+ **kwargs: Additional arguments.
186
181
 
187
182
  Returns:
188
183
  The run.
@@ -193,15 +188,13 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
193
188
  )
194
189
 
195
190
  @override
196
- def on_llm_end(
197
- self, response: Union[LLMResult, AIMessage], *, run_id: UUID, **kwargs: Any
198
- ) -> Run:
191
+ def on_llm_end(self, response: LLMResult, *, run_id: UUID, **kwargs: Any) -> Run:
199
192
  """End a trace for an LLM run.
200
193
 
201
194
  Args:
202
195
  response: The response.
203
196
  run_id: The run ID.
204
- kwargs: Additional arguments.
197
+ **kwargs: Additional arguments.
205
198
 
206
199
  Returns:
207
200
  The run.
@@ -228,7 +221,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
228
221
  Args:
229
222
  error: The error.
230
223
  run_id: The run ID.
231
- kwargs: Additional arguments.
224
+ **kwargs: Additional arguments.
232
225
 
233
226
  Returns:
234
227
  The run.
@@ -249,11 +242,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
249
242
  inputs: dict[str, Any],
250
243
  *,
251
244
  run_id: UUID,
252
- tags: Optional[list[str]] = None,
253
- parent_run_id: Optional[UUID] = None,
254
- metadata: Optional[dict[str, Any]] = None,
255
- run_type: Optional[str] = None,
256
- name: Optional[str] = None,
245
+ tags: list[str] | None = None,
246
+ parent_run_id: UUID | None = None,
247
+ metadata: dict[str, Any] | None = None,
248
+ run_type: str | None = None,
249
+ name: str | None = None,
257
250
  **kwargs: Any,
258
251
  ) -> Run:
259
252
  """Start a trace for a chain run.
@@ -262,12 +255,12 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
262
255
  serialized: The serialized chain.
263
256
  inputs: The inputs for the chain.
264
257
  run_id: The run ID.
265
- tags: The tags for the run. Defaults to None.
266
- parent_run_id: The parent run ID. Defaults to None.
267
- metadata: The metadata for the run. Defaults to None.
268
- run_type: The type of the run. Defaults to None.
258
+ tags: The tags for the run.
259
+ parent_run_id: The parent run ID.
260
+ metadata: The metadata for the run.
261
+ run_type: The type of the run.
269
262
  name: The name of the run.
270
- kwargs: Additional arguments.
263
+ **kwargs: Additional arguments.
271
264
 
272
265
  Returns:
273
266
  The run.
@@ -293,7 +286,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
293
286
  outputs: dict[str, Any],
294
287
  *,
295
288
  run_id: UUID,
296
- inputs: Optional[dict[str, Any]] = None,
289
+ inputs: dict[str, Any] | None = None,
297
290
  **kwargs: Any,
298
291
  ) -> Run:
299
292
  """End a trace for a chain run.
@@ -301,8 +294,8 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
301
294
  Args:
302
295
  outputs: The outputs for the chain.
303
296
  run_id: The run ID.
304
- inputs: The inputs for the chain. Defaults to None.
305
- kwargs: Additional arguments.
297
+ inputs: The inputs for the chain.
298
+ **kwargs: Additional arguments.
306
299
 
307
300
  Returns:
308
301
  The run.
@@ -321,7 +314,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
321
314
  self,
322
315
  error: BaseException,
323
316
  *,
324
- inputs: Optional[dict[str, Any]] = None,
317
+ inputs: dict[str, Any] | None = None,
325
318
  run_id: UUID,
326
319
  **kwargs: Any,
327
320
  ) -> Run:
@@ -329,9 +322,9 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
329
322
 
330
323
  Args:
331
324
  error: The error.
332
- inputs: The inputs for the chain. Defaults to None.
325
+ inputs: The inputs for the chain.
333
326
  run_id: The run ID.
334
- kwargs: Additional arguments.
327
+ **kwargs: Additional arguments.
335
328
 
336
329
  Returns:
337
330
  The run.
@@ -351,11 +344,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
351
344
  input_str: str,
352
345
  *,
353
346
  run_id: UUID,
354
- tags: Optional[list[str]] = None,
355
- parent_run_id: Optional[UUID] = None,
356
- metadata: Optional[dict[str, Any]] = None,
357
- name: Optional[str] = None,
358
- inputs: Optional[dict[str, Any]] = None,
347
+ tags: list[str] | None = None,
348
+ parent_run_id: UUID | None = None,
349
+ metadata: dict[str, Any] | None = None,
350
+ name: str | None = None,
351
+ inputs: dict[str, Any] | None = None,
359
352
  **kwargs: Any,
360
353
  ) -> Run:
361
354
  """Start a trace for a tool run.
@@ -364,12 +357,12 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
364
357
  serialized: The serialized tool.
365
358
  input_str: The input string.
366
359
  run_id: The run ID.
367
- tags: The tags for the run. Defaults to None.
368
- parent_run_id: The parent run ID. Defaults to None.
369
- metadata: The metadata for the run. Defaults to None.
360
+ tags: The tags for the run.
361
+ parent_run_id: The parent run ID.
362
+ metadata: The metadata for the run.
370
363
  name: The name of the run.
371
364
  inputs: The inputs for the tool.
372
- kwargs: Additional arguments.
365
+ **kwargs: Additional arguments.
373
366
 
374
367
  Returns:
375
368
  The run.
@@ -396,7 +389,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
396
389
  Args:
397
390
  output: The output for the tool.
398
391
  run_id: The run ID.
399
- kwargs: Additional arguments.
392
+ **kwargs: Additional arguments.
400
393
 
401
394
  Returns:
402
395
  The run.
@@ -422,7 +415,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
422
415
  Args:
423
416
  error: The error.
424
417
  run_id: The run ID.
425
- kwargs: Additional arguments.
418
+ **kwargs: Additional arguments.
426
419
 
427
420
  Returns:
428
421
  The run.
@@ -441,10 +434,10 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
441
434
  query: str,
442
435
  *,
443
436
  run_id: UUID,
444
- parent_run_id: Optional[UUID] = None,
445
- tags: Optional[list[str]] = None,
446
- metadata: Optional[dict[str, Any]] = None,
447
- name: Optional[str] = None,
437
+ parent_run_id: UUID | None = None,
438
+ tags: list[str] | None = None,
439
+ metadata: dict[str, Any] | None = None,
440
+ name: str | None = None,
448
441
  **kwargs: Any,
449
442
  ) -> Run:
450
443
  """Run when the Retriever starts running.
@@ -453,11 +446,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
453
446
  serialized: The serialized retriever.
454
447
  query: The query.
455
448
  run_id: The run ID.
456
- parent_run_id: The parent run ID. Defaults to None.
457
- tags: The tags for the run. Defaults to None.
458
- metadata: The metadata for the run. Defaults to None.
449
+ parent_run_id: The parent run ID.
450
+ tags: The tags for the run.
451
+ metadata: The metadata for the run.
459
452
  name: The name of the run.
460
- kwargs: Additional arguments.
453
+ **kwargs: Additional arguments.
461
454
 
462
455
  Returns:
463
456
  The run.
@@ -489,7 +482,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
489
482
  Args:
490
483
  error: The error.
491
484
  run_id: The run ID.
492
- kwargs: Additional arguments.
485
+ **kwargs: Additional arguments.
493
486
 
494
487
  Returns:
495
488
  The run.
@@ -511,7 +504,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
511
504
  Args:
512
505
  documents: The documents.
513
506
  run_id: The run ID.
514
- kwargs: Additional arguments.
507
+ **kwargs: Additional arguments.
515
508
 
516
509
  Returns:
517
510
  The run.
@@ -525,11 +518,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
525
518
  return retrieval_run
526
519
 
527
520
  def __deepcopy__(self, memo: dict) -> BaseTracer:
528
- """Deepcopy the tracer."""
521
+ """Return self."""
529
522
  return self
530
523
 
531
524
  def __copy__(self) -> BaseTracer:
532
- """Copy the tracer."""
525
+ """Return self."""
533
526
  return self
534
527
 
535
528
 
@@ -567,13 +560,13 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
567
560
  async def on_chat_model_start(
568
561
  self,
569
562
  serialized: dict[str, Any],
570
- messages: Union[list[list[BaseMessage]], list[MessageV1]],
563
+ messages: list[list[BaseMessage]],
571
564
  *,
572
565
  run_id: UUID,
573
- parent_run_id: Optional[UUID] = None,
574
- tags: Optional[list[str]] = None,
575
- metadata: Optional[dict[str, Any]] = None,
576
- name: Optional[str] = None,
566
+ parent_run_id: UUID | None = None,
567
+ tags: list[str] | None = None,
568
+ metadata: dict[str, Any] | None = None,
569
+ name: str | None = None,
577
570
  **kwargs: Any,
578
571
  ) -> Any:
579
572
  chat_model_run = self._create_chat_model_run(
@@ -600,9 +593,9 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
600
593
  prompts: list[str],
601
594
  *,
602
595
  run_id: UUID,
603
- parent_run_id: Optional[UUID] = None,
604
- tags: Optional[list[str]] = None,
605
- metadata: Optional[dict[str, Any]] = None,
596
+ parent_run_id: UUID | None = None,
597
+ tags: list[str] | None = None,
598
+ metadata: dict[str, Any] | None = None,
606
599
  **kwargs: Any,
607
600
  ) -> None:
608
601
  llm_run = self._create_llm_run(
@@ -622,11 +615,9 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
622
615
  self,
623
616
  token: str,
624
617
  *,
625
- chunk: Optional[
626
- Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]
627
- ] = None,
618
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
628
619
  run_id: UUID,
629
- parent_run_id: Optional[UUID] = None,
620
+ parent_run_id: UUID | None = None,
630
621
  **kwargs: Any,
631
622
  ) -> None:
632
623
  llm_run = self._llm_run_with_token_event(
@@ -653,11 +644,11 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
653
644
  @override
654
645
  async def on_llm_end(
655
646
  self,
656
- response: Union[LLMResult, AIMessage],
647
+ response: LLMResult,
657
648
  *,
658
649
  run_id: UUID,
659
- parent_run_id: Optional[UUID] = None,
660
- tags: Optional[list[str]] = None,
650
+ parent_run_id: UUID | None = None,
651
+ tags: list[str] | None = None,
661
652
  **kwargs: Any,
662
653
  ) -> None:
663
654
  llm_run = self._complete_llm_run(
@@ -673,8 +664,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
673
664
  error: BaseException,
674
665
  *,
675
666
  run_id: UUID,
676
- parent_run_id: Optional[UUID] = None,
677
- tags: Optional[list[str]] = None,
667
+ parent_run_id: UUID | None = None,
668
+ tags: list[str] | None = None,
678
669
  **kwargs: Any,
679
670
  ) -> None:
680
671
  llm_run = self._errored_llm_run(
@@ -691,11 +682,11 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
691
682
  inputs: dict[str, Any],
692
683
  *,
693
684
  run_id: UUID,
694
- tags: Optional[list[str]] = None,
695
- parent_run_id: Optional[UUID] = None,
696
- metadata: Optional[dict[str, Any]] = None,
697
- run_type: Optional[str] = None,
698
- name: Optional[str] = None,
685
+ tags: list[str] | None = None,
686
+ parent_run_id: UUID | None = None,
687
+ metadata: dict[str, Any] | None = None,
688
+ run_type: str | None = None,
689
+ name: str | None = None,
699
690
  **kwargs: Any,
700
691
  ) -> None:
701
692
  chain_run = self._create_chain_run(
@@ -718,7 +709,7 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
718
709
  outputs: dict[str, Any],
719
710
  *,
720
711
  run_id: UUID,
721
- inputs: Optional[dict[str, Any]] = None,
712
+ inputs: dict[str, Any] | None = None,
722
713
  **kwargs: Any,
723
714
  ) -> None:
724
715
  chain_run = self._complete_chain_run(
@@ -734,7 +725,7 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
734
725
  self,
735
726
  error: BaseException,
736
727
  *,
737
- inputs: Optional[dict[str, Any]] = None,
728
+ inputs: dict[str, Any] | None = None,
738
729
  run_id: UUID,
739
730
  **kwargs: Any,
740
731
  ) -> None:
@@ -753,11 +744,11 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
753
744
  input_str: str,
754
745
  *,
755
746
  run_id: UUID,
756
- tags: Optional[list[str]] = None,
757
- parent_run_id: Optional[UUID] = None,
758
- metadata: Optional[dict[str, Any]] = None,
759
- name: Optional[str] = None,
760
- inputs: Optional[dict[str, Any]] = None,
747
+ tags: list[str] | None = None,
748
+ parent_run_id: UUID | None = None,
749
+ metadata: dict[str, Any] | None = None,
750
+ name: str | None = None,
751
+ inputs: dict[str, Any] | None = None,
761
752
  **kwargs: Any,
762
753
  ) -> None:
763
754
  tool_run = self._create_tool_run(
@@ -794,8 +785,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
794
785
  error: BaseException,
795
786
  *,
796
787
  run_id: UUID,
797
- parent_run_id: Optional[UUID] = None,
798
- tags: Optional[list[str]] = None,
788
+ parent_run_id: UUID | None = None,
789
+ tags: list[str] | None = None,
799
790
  **kwargs: Any,
800
791
  ) -> None:
801
792
  tool_run = self._errored_tool_run(
@@ -812,10 +803,10 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
812
803
  query: str,
813
804
  *,
814
805
  run_id: UUID,
815
- parent_run_id: Optional[UUID] = None,
816
- tags: Optional[list[str]] = None,
817
- metadata: Optional[dict[str, Any]] = None,
818
- name: Optional[str] = None,
806
+ parent_run_id: UUID | None = None,
807
+ tags: list[str] | None = None,
808
+ metadata: dict[str, Any] | None = None,
809
+ name: str | None = None,
819
810
  **kwargs: Any,
820
811
  ) -> None:
821
812
  retriever_run = self._create_retrieval_run(
@@ -839,8 +830,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
839
830
  error: BaseException,
840
831
  *,
841
832
  run_id: UUID,
842
- parent_run_id: Optional[UUID] = None,
843
- tags: Optional[list[str]] = None,
833
+ parent_run_id: UUID | None = None,
834
+ tags: list[str] | None = None,
844
835
  **kwargs: Any,
845
836
  ) -> None:
846
837
  retrieval_run = self._errored_retrieval_run(
@@ -859,8 +850,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
859
850
  documents: Sequence[Document],
860
851
  *,
861
852
  run_id: UUID,
862
- parent_run_id: Optional[UUID] = None,
863
- tags: Optional[list[str]] = None,
853
+ parent_run_id: UUID | None = None,
854
+ tags: list[str] | None = None,
864
855
  **kwargs: Any,
865
856
  ) -> None:
866
857
  retrieval_run = self._complete_retrieval_run(
@@ -889,7 +880,7 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
889
880
  self,
890
881
  run: Run,
891
882
  token: str,
892
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk, AIMessageChunk]],
883
+ chunk: GenerationChunk | ChatGenerationChunk | None,
893
884
  ) -> None:
894
885
  """Process new LLM token."""
895
886
 
@@ -8,8 +8,6 @@ from typing import (
8
8
  TYPE_CHECKING,
9
9
  Any,
10
10
  Literal,
11
- Optional,
12
- Union,
13
11
  cast,
14
12
  )
15
13
  from uuid import UUID
@@ -27,51 +25,35 @@ if TYPE_CHECKING:
27
25
 
28
26
  from langchain_core.callbacks.base import BaseCallbackHandler, Callbacks
29
27
  from langchain_core.callbacks.manager import AsyncCallbackManager, CallbackManager
30
- from langchain_core.tracers.schemas import TracerSessionV1
31
28
 
32
29
  # for backwards partial compatibility if this is imported by users but unused
33
30
  tracing_callback_var: Any = None
34
- tracing_v2_callback_var: ContextVar[Optional[LangChainTracer]] = ContextVar(
31
+ tracing_v2_callback_var: ContextVar[LangChainTracer | None] = ContextVar(
35
32
  "tracing_callback_v2", default=None
36
33
  )
37
- run_collector_var: ContextVar[Optional[RunCollectorCallbackHandler]] = ContextVar(
34
+ run_collector_var: ContextVar[RunCollectorCallbackHandler | None] = ContextVar(
38
35
  "run_collector", default=None
39
36
  )
40
37
 
41
38
 
42
- @contextmanager
43
- def tracing_enabled(
44
- session_name: str = "default", # noqa: ARG001
45
- ) -> Generator[TracerSessionV1, None, None]:
46
- """Throw an error because this has been replaced by tracing_v2_enabled."""
47
- msg = (
48
- "tracing_enabled is no longer supported. Please use tracing_enabled_v2 instead."
49
- )
50
- raise RuntimeError(msg)
51
-
52
-
53
39
  @contextmanager
54
40
  def tracing_v2_enabled(
55
- project_name: Optional[str] = None,
41
+ project_name: str | None = None,
56
42
  *,
57
- example_id: Optional[Union[str, UUID]] = None,
58
- tags: Optional[list[str]] = None,
59
- client: Optional[LangSmithClient] = None,
43
+ example_id: str | UUID | None = None,
44
+ tags: list[str] | None = None,
45
+ client: LangSmithClient | None = None,
60
46
  ) -> Generator[LangChainTracer, None, None]:
61
47
  """Instruct LangChain to log all runs in context to LangSmith.
62
48
 
63
49
  Args:
64
- project_name (str, optional): The name of the project.
65
- Defaults to ``'default'``.
66
- example_id (str or UUID, optional): The ID of the example.
67
- Defaults to None.
68
- tags (list[str], optional): The tags to add to the run.
69
- Defaults to None.
70
- client (LangSmithClient, optional): The client of the langsmith.
71
- Defaults to None.
50
+ project_name: The name of the project. Defaults to `'default'`.
51
+ example_id: The ID of the example.
52
+ tags: The tags to add to the run.
53
+ client: The client of the langsmith.
72
54
 
73
55
  Yields:
74
- LangChainTracer: The LangChain tracer.
56
+ The LangChain tracer.
75
57
 
76
58
  Example:
77
59
  >>> with tracing_v2_enabled():
@@ -103,7 +85,7 @@ def collect_runs() -> Generator[RunCollectorCallbackHandler, None, None]:
103
85
  """Collect all run traces in context.
104
86
 
105
87
  Yields:
106
- run_collector.RunCollectorCallbackHandler: The run collector callback handler.
88
+ The run collector callback handler.
107
89
 
108
90
  Example:
109
91
  >>> with collect_runs() as runs_cb:
@@ -119,9 +101,9 @@ def collect_runs() -> Generator[RunCollectorCallbackHandler, None, None]:
119
101
 
120
102
 
121
103
  def _get_trace_callbacks(
122
- project_name: Optional[str] = None,
123
- example_id: Optional[Union[str, UUID]] = None,
124
- callback_manager: Optional[Union[CallbackManager, AsyncCallbackManager]] = None,
104
+ project_name: str | None = None,
105
+ example_id: str | UUID | None = None,
106
+ callback_manager: CallbackManager | AsyncCallbackManager | None = None,
125
107
  ) -> Callbacks:
126
108
  if _tracing_v2_is_enabled():
127
109
  project_name_ = project_name or _get_tracer_project()
@@ -145,7 +127,7 @@ def _get_trace_callbacks(
145
127
  return cb
146
128
 
147
129
 
148
- def _tracing_v2_is_enabled() -> Union[bool, Literal["local"]]:
130
+ def _tracing_v2_is_enabled() -> bool | Literal["local"]:
149
131
  if tracing_v2_callback_var.get() is not None:
150
132
  return True
151
133
  return ls_utils.tracing_is_enabled()
@@ -176,32 +158,31 @@ def _get_tracer_project() -> str:
176
158
 
177
159
  _configure_hooks: list[
178
160
  tuple[
179
- ContextVar[Optional[BaseCallbackHandler]],
161
+ ContextVar[BaseCallbackHandler | None],
180
162
  bool,
181
- Optional[type[BaseCallbackHandler]],
182
- Optional[str],
163
+ type[BaseCallbackHandler] | None,
164
+ str | None,
183
165
  ]
184
166
  ] = []
185
167
 
186
168
 
187
169
  def register_configure_hook(
188
- context_var: ContextVar[Optional[Any]],
170
+ context_var: ContextVar[Any | None],
189
171
  inheritable: bool, # noqa: FBT001
190
- handle_class: Optional[type[BaseCallbackHandler]] = None,
191
- env_var: Optional[str] = None,
172
+ handle_class: type[BaseCallbackHandler] | None = None,
173
+ env_var: str | None = None,
192
174
  ) -> None:
193
175
  """Register a configure hook.
194
176
 
195
177
  Args:
196
- context_var (ContextVar[Optional[Any]]): The context variable.
197
- inheritable (bool): Whether the context variable is inheritable.
198
- handle_class (Optional[Type[BaseCallbackHandler]], optional):
199
- The callback handler class. Defaults to None.
200
- env_var (Optional[str], optional): The environment variable. Defaults to None.
178
+ context_var: The context variable.
179
+ inheritable: Whether the context variable is inheritable.
180
+ handle_class: The callback handler class.
181
+ env_var: The environment variable.
201
182
 
202
183
  Raises:
203
- ValueError: If env_var is set, handle_class must also be set
204
- to a non-None value.
184
+ ValueError: If env_var is set, handle_class must also be set to a non-None
185
+ value.
205
186
  """
206
187
  if env_var is not None and handle_class is None:
207
188
  msg = "If env_var is set, handle_class must also be set to a non-None value."
@@ -211,7 +192,7 @@ def register_configure_hook(
211
192
  (
212
193
  # the typings of ContextVar do not have the generic arg set as covariant
213
194
  # so we have to cast it
214
- cast("ContextVar[Optional[BaseCallbackHandler]]", context_var),
195
+ cast("ContextVar[BaseCallbackHandler | None]", context_var),
215
196
  inheritable,
216
197
  handle_class,
217
198
  env_var,