langchain-core 1.0.0a6__py3-none-any.whl → 1.0.4__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 +55 -48
  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 +454 -514
  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 +102 -94
  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 +2 -2
  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 +82 -172
  36. langchain_core/language_models/chat_models.py +329 -402
  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 +189 -269
  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 +53 -51
  47. langchain_core/messages/block_translators/__init__.py +19 -22
  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 +10 -7
  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 +339 -330
  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 +484 -510
  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 +30 -23
  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 +16 -16
  78. langchain_core/outputs/llm_result.py +10 -10
  79. langchain_core/prompt_values.py +13 -19
  80. langchain_core/prompts/__init__.py +3 -27
  81. langchain_core/prompts/base.py +81 -86
  82. langchain_core/prompts/chat.py +308 -351
  83. langchain_core/prompts/dict.py +6 -6
  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 +7 -7
  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 +1551 -1656
  95. langchain_core/runnables/branch.py +68 -70
  96. langchain_core/runnables/config.py +72 -89
  97. langchain_core/runnables/configurable.py +145 -161
  98. langchain_core/runnables/fallbacks.py +102 -96
  99. langchain_core/runnables/graph.py +91 -97
  100. langchain_core/runnables/graph_ascii.py +27 -28
  101. langchain_core/runnables/graph_mermaid.py +42 -51
  102. langchain_core/runnables/graph_png.py +43 -16
  103. langchain_core/runnables/history.py +175 -177
  104. langchain_core/runnables/passthrough.py +151 -167
  105. langchain_core/runnables/retry.py +46 -51
  106. langchain_core/runnables/router.py +30 -35
  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 +29 -29
  112. langchain_core/tools/__init__.py +1 -14
  113. langchain_core/tools/base.py +306 -245
  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 +94 -188
  137. langchain_core/utils/html.py +7 -8
  138. langchain_core/utils/input.py +9 -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 +35 -37
  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.4.dist-info/METADATA +69 -0
  153. langchain_core-1.0.4.dist-info/RECORD +172 -0
  154. {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.4.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.0a6.dist-info/METADATA +0 -67
  164. langchain_core-1.0.0a6.dist-info/RECORD +0 -181
  165. langchain_core-1.0.0a6.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
@@ -57,10 +55,10 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
57
55
  messages: list[list[BaseMessage]],
58
56
  *,
59
57
  run_id: UUID,
60
- tags: Optional[list[str]] = None,
61
- parent_run_id: Optional[UUID] = None,
62
- metadata: Optional[dict[str, Any]] = None,
63
- 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,
64
62
  **kwargs: Any,
65
63
  ) -> Run:
66
64
  """Start a trace for an LLM run.
@@ -69,11 +67,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
69
67
  serialized: The serialized model.
70
68
  messages: The messages to start the chat with.
71
69
  run_id: The run ID.
72
- tags: The tags for the run. Defaults to None.
73
- parent_run_id: The parent run ID. Defaults to None.
74
- 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.
75
73
  name: The name of the run.
76
- kwargs: Additional arguments.
74
+ **kwargs: Additional arguments.
77
75
 
78
76
  Returns:
79
77
  The run.
@@ -98,10 +96,10 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
98
96
  prompts: list[str],
99
97
  *,
100
98
  run_id: UUID,
101
- tags: Optional[list[str]] = None,
102
- parent_run_id: Optional[UUID] = None,
103
- metadata: Optional[dict[str, Any]] = None,
104
- 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,
105
103
  **kwargs: Any,
106
104
  ) -> Run:
107
105
  """Start a trace for an LLM run.
@@ -110,11 +108,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
110
108
  serialized: The serialized model.
111
109
  prompts: The prompts to start the LLM with.
112
110
  run_id: The run ID.
113
- tags: The tags for the run. Defaults to None.
114
- parent_run_id: The parent run ID. Defaults to None.
115
- 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.
116
114
  name: The name of the run.
117
- kwargs: Additional arguments.
115
+ **kwargs: Additional arguments.
118
116
 
119
117
  Returns:
120
118
  The run.
@@ -138,19 +136,19 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
138
136
  self,
139
137
  token: str,
140
138
  *,
141
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
139
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
142
140
  run_id: UUID,
143
- parent_run_id: Optional[UUID] = None,
141
+ parent_run_id: UUID | None = None,
144
142
  **kwargs: Any,
145
143
  ) -> Run:
146
144
  """Run on new LLM token. Only available when streaming is enabled.
147
145
 
148
146
  Args:
149
147
  token: The token.
150
- chunk: The chunk. Defaults to None.
148
+ chunk: The chunk.
151
149
  run_id: The run ID.
152
- parent_run_id: The parent run ID. Defaults to None.
153
- kwargs: Additional arguments.
150
+ parent_run_id: The parent run ID.
151
+ **kwargs: Additional arguments.
154
152
 
155
153
  Returns:
156
154
  The run.
@@ -179,7 +177,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
179
177
  Args:
180
178
  retry_state: The retry state.
181
179
  run_id: The run ID.
182
- kwargs: Additional arguments.
180
+ **kwargs: Additional arguments.
183
181
 
184
182
  Returns:
185
183
  The run.
@@ -196,7 +194,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
196
194
  Args:
197
195
  response: The response.
198
196
  run_id: The run ID.
199
- kwargs: Additional arguments.
197
+ **kwargs: Additional arguments.
200
198
 
201
199
  Returns:
202
200
  The run.
@@ -223,7 +221,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
223
221
  Args:
224
222
  error: The error.
225
223
  run_id: The run ID.
226
- kwargs: Additional arguments.
224
+ **kwargs: Additional arguments.
227
225
 
228
226
  Returns:
229
227
  The run.
@@ -244,11 +242,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
244
242
  inputs: dict[str, Any],
245
243
  *,
246
244
  run_id: UUID,
247
- tags: Optional[list[str]] = None,
248
- parent_run_id: Optional[UUID] = None,
249
- metadata: Optional[dict[str, Any]] = None,
250
- run_type: Optional[str] = None,
251
- 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,
252
250
  **kwargs: Any,
253
251
  ) -> Run:
254
252
  """Start a trace for a chain run.
@@ -257,12 +255,12 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
257
255
  serialized: The serialized chain.
258
256
  inputs: The inputs for the chain.
259
257
  run_id: The run ID.
260
- tags: The tags for the run. Defaults to None.
261
- parent_run_id: The parent run ID. Defaults to None.
262
- metadata: The metadata for the run. Defaults to None.
263
- 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.
264
262
  name: The name of the run.
265
- kwargs: Additional arguments.
263
+ **kwargs: Additional arguments.
266
264
 
267
265
  Returns:
268
266
  The run.
@@ -288,7 +286,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
288
286
  outputs: dict[str, Any],
289
287
  *,
290
288
  run_id: UUID,
291
- inputs: Optional[dict[str, Any]] = None,
289
+ inputs: dict[str, Any] | None = None,
292
290
  **kwargs: Any,
293
291
  ) -> Run:
294
292
  """End a trace for a chain run.
@@ -296,8 +294,8 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
296
294
  Args:
297
295
  outputs: The outputs for the chain.
298
296
  run_id: The run ID.
299
- inputs: The inputs for the chain. Defaults to None.
300
- kwargs: Additional arguments.
297
+ inputs: The inputs for the chain.
298
+ **kwargs: Additional arguments.
301
299
 
302
300
  Returns:
303
301
  The run.
@@ -316,7 +314,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
316
314
  self,
317
315
  error: BaseException,
318
316
  *,
319
- inputs: Optional[dict[str, Any]] = None,
317
+ inputs: dict[str, Any] | None = None,
320
318
  run_id: UUID,
321
319
  **kwargs: Any,
322
320
  ) -> Run:
@@ -324,9 +322,9 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
324
322
 
325
323
  Args:
326
324
  error: The error.
327
- inputs: The inputs for the chain. Defaults to None.
325
+ inputs: The inputs for the chain.
328
326
  run_id: The run ID.
329
- kwargs: Additional arguments.
327
+ **kwargs: Additional arguments.
330
328
 
331
329
  Returns:
332
330
  The run.
@@ -346,11 +344,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
346
344
  input_str: str,
347
345
  *,
348
346
  run_id: UUID,
349
- tags: Optional[list[str]] = None,
350
- parent_run_id: Optional[UUID] = None,
351
- metadata: Optional[dict[str, Any]] = None,
352
- name: Optional[str] = None,
353
- 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,
354
352
  **kwargs: Any,
355
353
  ) -> Run:
356
354
  """Start a trace for a tool run.
@@ -359,12 +357,12 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
359
357
  serialized: The serialized tool.
360
358
  input_str: The input string.
361
359
  run_id: The run ID.
362
- tags: The tags for the run. Defaults to None.
363
- parent_run_id: The parent run ID. Defaults to None.
364
- 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.
365
363
  name: The name of the run.
366
364
  inputs: The inputs for the tool.
367
- kwargs: Additional arguments.
365
+ **kwargs: Additional arguments.
368
366
 
369
367
  Returns:
370
368
  The run.
@@ -391,7 +389,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
391
389
  Args:
392
390
  output: The output for the tool.
393
391
  run_id: The run ID.
394
- kwargs: Additional arguments.
392
+ **kwargs: Additional arguments.
395
393
 
396
394
  Returns:
397
395
  The run.
@@ -417,7 +415,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
417
415
  Args:
418
416
  error: The error.
419
417
  run_id: The run ID.
420
- kwargs: Additional arguments.
418
+ **kwargs: Additional arguments.
421
419
 
422
420
  Returns:
423
421
  The run.
@@ -436,10 +434,10 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
436
434
  query: str,
437
435
  *,
438
436
  run_id: UUID,
439
- parent_run_id: Optional[UUID] = None,
440
- tags: Optional[list[str]] = None,
441
- metadata: Optional[dict[str, Any]] = None,
442
- 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,
443
441
  **kwargs: Any,
444
442
  ) -> Run:
445
443
  """Run when the Retriever starts running.
@@ -448,11 +446,11 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
448
446
  serialized: The serialized retriever.
449
447
  query: The query.
450
448
  run_id: The run ID.
451
- parent_run_id: The parent run ID. Defaults to None.
452
- tags: The tags for the run. Defaults to None.
453
- 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.
454
452
  name: The name of the run.
455
- kwargs: Additional arguments.
453
+ **kwargs: Additional arguments.
456
454
 
457
455
  Returns:
458
456
  The run.
@@ -484,7 +482,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
484
482
  Args:
485
483
  error: The error.
486
484
  run_id: The run ID.
487
- kwargs: Additional arguments.
485
+ **kwargs: Additional arguments.
488
486
 
489
487
  Returns:
490
488
  The run.
@@ -506,7 +504,7 @@ class BaseTracer(_TracerCore, BaseCallbackHandler, ABC):
506
504
  Args:
507
505
  documents: The documents.
508
506
  run_id: The run ID.
509
- kwargs: Additional arguments.
507
+ **kwargs: Additional arguments.
510
508
 
511
509
  Returns:
512
510
  The run.
@@ -565,10 +563,10 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
565
563
  messages: list[list[BaseMessage]],
566
564
  *,
567
565
  run_id: UUID,
568
- parent_run_id: Optional[UUID] = None,
569
- tags: Optional[list[str]] = None,
570
- metadata: Optional[dict[str, Any]] = None,
571
- 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,
572
570
  **kwargs: Any,
573
571
  ) -> Any:
574
572
  chat_model_run = self._create_chat_model_run(
@@ -595,9 +593,9 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
595
593
  prompts: list[str],
596
594
  *,
597
595
  run_id: UUID,
598
- parent_run_id: Optional[UUID] = None,
599
- tags: Optional[list[str]] = None,
600
- 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,
601
599
  **kwargs: Any,
602
600
  ) -> None:
603
601
  llm_run = self._create_llm_run(
@@ -617,9 +615,9 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
617
615
  self,
618
616
  token: str,
619
617
  *,
620
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
618
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
621
619
  run_id: UUID,
622
- parent_run_id: Optional[UUID] = None,
620
+ parent_run_id: UUID | None = None,
623
621
  **kwargs: Any,
624
622
  ) -> None:
625
623
  llm_run = self._llm_run_with_token_event(
@@ -649,8 +647,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
649
647
  response: LLMResult,
650
648
  *,
651
649
  run_id: UUID,
652
- parent_run_id: Optional[UUID] = None,
653
- tags: Optional[list[str]] = None,
650
+ parent_run_id: UUID | None = None,
651
+ tags: list[str] | None = None,
654
652
  **kwargs: Any,
655
653
  ) -> None:
656
654
  llm_run = self._complete_llm_run(
@@ -666,8 +664,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
666
664
  error: BaseException,
667
665
  *,
668
666
  run_id: UUID,
669
- parent_run_id: Optional[UUID] = None,
670
- tags: Optional[list[str]] = None,
667
+ parent_run_id: UUID | None = None,
668
+ tags: list[str] | None = None,
671
669
  **kwargs: Any,
672
670
  ) -> None:
673
671
  llm_run = self._errored_llm_run(
@@ -684,11 +682,11 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
684
682
  inputs: dict[str, Any],
685
683
  *,
686
684
  run_id: UUID,
687
- tags: Optional[list[str]] = None,
688
- parent_run_id: Optional[UUID] = None,
689
- metadata: Optional[dict[str, Any]] = None,
690
- run_type: Optional[str] = None,
691
- 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,
692
690
  **kwargs: Any,
693
691
  ) -> None:
694
692
  chain_run = self._create_chain_run(
@@ -711,7 +709,7 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
711
709
  outputs: dict[str, Any],
712
710
  *,
713
711
  run_id: UUID,
714
- inputs: Optional[dict[str, Any]] = None,
712
+ inputs: dict[str, Any] | None = None,
715
713
  **kwargs: Any,
716
714
  ) -> None:
717
715
  chain_run = self._complete_chain_run(
@@ -727,7 +725,7 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
727
725
  self,
728
726
  error: BaseException,
729
727
  *,
730
- inputs: Optional[dict[str, Any]] = None,
728
+ inputs: dict[str, Any] | None = None,
731
729
  run_id: UUID,
732
730
  **kwargs: Any,
733
731
  ) -> None:
@@ -746,11 +744,11 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
746
744
  input_str: str,
747
745
  *,
748
746
  run_id: UUID,
749
- tags: Optional[list[str]] = None,
750
- parent_run_id: Optional[UUID] = None,
751
- metadata: Optional[dict[str, Any]] = None,
752
- name: Optional[str] = None,
753
- 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,
754
752
  **kwargs: Any,
755
753
  ) -> None:
756
754
  tool_run = self._create_tool_run(
@@ -787,8 +785,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
787
785
  error: BaseException,
788
786
  *,
789
787
  run_id: UUID,
790
- parent_run_id: Optional[UUID] = None,
791
- tags: Optional[list[str]] = None,
788
+ parent_run_id: UUID | None = None,
789
+ tags: list[str] | None = None,
792
790
  **kwargs: Any,
793
791
  ) -> None:
794
792
  tool_run = self._errored_tool_run(
@@ -805,10 +803,10 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
805
803
  query: str,
806
804
  *,
807
805
  run_id: UUID,
808
- parent_run_id: Optional[UUID] = None,
809
- tags: Optional[list[str]] = None,
810
- metadata: Optional[dict[str, Any]] = None,
811
- 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,
812
810
  **kwargs: Any,
813
811
  ) -> None:
814
812
  retriever_run = self._create_retrieval_run(
@@ -832,8 +830,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
832
830
  error: BaseException,
833
831
  *,
834
832
  run_id: UUID,
835
- parent_run_id: Optional[UUID] = None,
836
- tags: Optional[list[str]] = None,
833
+ parent_run_id: UUID | None = None,
834
+ tags: list[str] | None = None,
837
835
  **kwargs: Any,
838
836
  ) -> None:
839
837
  retrieval_run = self._errored_retrieval_run(
@@ -852,8 +850,8 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
852
850
  documents: Sequence[Document],
853
851
  *,
854
852
  run_id: UUID,
855
- parent_run_id: Optional[UUID] = None,
856
- tags: Optional[list[str]] = None,
853
+ parent_run_id: UUID | None = None,
854
+ tags: list[str] | None = None,
857
855
  **kwargs: Any,
858
856
  ) -> None:
859
857
  retrieval_run = self._complete_retrieval_run(
@@ -882,7 +880,7 @@ class AsyncBaseTracer(_TracerCore, AsyncCallbackHandler, ABC):
882
880
  self,
883
881
  run: Run,
884
882
  token: str,
885
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]],
883
+ chunk: GenerationChunk | ChatGenerationChunk | None,
886
884
  ) -> None:
887
885
  """Process new LLM token."""
888
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,55 +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
-
48
- Raises:
49
- RuntimeError: Always, because this function is deprecated.
50
- """
51
- msg = (
52
- "tracing_enabled is no longer supported. Please use tracing_enabled_v2 instead."
53
- )
54
- raise RuntimeError(msg)
55
-
56
-
57
39
  @contextmanager
58
40
  def tracing_v2_enabled(
59
- project_name: Optional[str] = None,
41
+ project_name: str | None = None,
60
42
  *,
61
- example_id: Optional[Union[str, UUID]] = None,
62
- tags: Optional[list[str]] = None,
63
- client: Optional[LangSmithClient] = None,
43
+ example_id: str | UUID | None = None,
44
+ tags: list[str] | None = None,
45
+ client: LangSmithClient | None = None,
64
46
  ) -> Generator[LangChainTracer, None, None]:
65
47
  """Instruct LangChain to log all runs in context to LangSmith.
66
48
 
67
49
  Args:
68
- project_name (str, optional): The name of the project.
69
- Defaults to ``'default'``.
70
- example_id (str or UUID, optional): The ID of the example.
71
- Defaults to None.
72
- tags (list[str], optional): The tags to add to the run.
73
- Defaults to None.
74
- client (LangSmithClient, optional): The client of the langsmith.
75
- 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.
76
54
 
77
55
  Yields:
78
- LangChainTracer: The LangChain tracer.
56
+ The LangChain tracer.
79
57
 
80
58
  Example:
81
59
  >>> with tracing_v2_enabled():
@@ -107,7 +85,7 @@ def collect_runs() -> Generator[RunCollectorCallbackHandler, None, None]:
107
85
  """Collect all run traces in context.
108
86
 
109
87
  Yields:
110
- run_collector.RunCollectorCallbackHandler: The run collector callback handler.
88
+ The run collector callback handler.
111
89
 
112
90
  Example:
113
91
  >>> with collect_runs() as runs_cb:
@@ -123,9 +101,9 @@ def collect_runs() -> Generator[RunCollectorCallbackHandler, None, None]:
123
101
 
124
102
 
125
103
  def _get_trace_callbacks(
126
- project_name: Optional[str] = None,
127
- example_id: Optional[Union[str, UUID]] = None,
128
- 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,
129
107
  ) -> Callbacks:
130
108
  if _tracing_v2_is_enabled():
131
109
  project_name_ = project_name or _get_tracer_project()
@@ -149,7 +127,7 @@ def _get_trace_callbacks(
149
127
  return cb
150
128
 
151
129
 
152
- def _tracing_v2_is_enabled() -> Union[bool, Literal["local"]]:
130
+ def _tracing_v2_is_enabled() -> bool | Literal["local"]:
153
131
  if tracing_v2_callback_var.get() is not None:
154
132
  return True
155
133
  return ls_utils.tracing_is_enabled()
@@ -180,32 +158,31 @@ def _get_tracer_project() -> str:
180
158
 
181
159
  _configure_hooks: list[
182
160
  tuple[
183
- ContextVar[Optional[BaseCallbackHandler]],
161
+ ContextVar[BaseCallbackHandler | None],
184
162
  bool,
185
- Optional[type[BaseCallbackHandler]],
186
- Optional[str],
163
+ type[BaseCallbackHandler] | None,
164
+ str | None,
187
165
  ]
188
166
  ] = []
189
167
 
190
168
 
191
169
  def register_configure_hook(
192
- context_var: ContextVar[Optional[Any]],
170
+ context_var: ContextVar[Any | None],
193
171
  inheritable: bool, # noqa: FBT001
194
- handle_class: Optional[type[BaseCallbackHandler]] = None,
195
- env_var: Optional[str] = None,
172
+ handle_class: type[BaseCallbackHandler] | None = None,
173
+ env_var: str | None = None,
196
174
  ) -> None:
197
175
  """Register a configure hook.
198
176
 
199
177
  Args:
200
- context_var (ContextVar[Optional[Any]]): The context variable.
201
- inheritable (bool): Whether the context variable is inheritable.
202
- handle_class (Optional[Type[BaseCallbackHandler]], optional):
203
- The callback handler class. Defaults to None.
204
- 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.
205
182
 
206
183
  Raises:
207
- ValueError: If env_var is set, handle_class must also be set
208
- to a non-None value.
184
+ ValueError: If env_var is set, handle_class must also be set to a non-None
185
+ value.
209
186
  """
210
187
  if env_var is not None and handle_class is None:
211
188
  msg = "If env_var is set, handle_class must also be set to a non-None value."
@@ -215,7 +192,7 @@ def register_configure_hook(
215
192
  (
216
193
  # the typings of ContextVar do not have the generic arg set as covariant
217
194
  # so we have to cast it
218
- cast("ContextVar[Optional[BaseCallbackHandler]]", context_var),
195
+ cast("ContextVar[BaseCallbackHandler | None]", context_var),
219
196
  inheritable,
220
197
  handle_class,
221
198
  env_var,