langchain-core 0.3.79__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of langchain-core might be problematic. Click here for more details.

Files changed (165) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +23 -26
  4. langchain_core/_api/deprecation.py +52 -65
  5. langchain_core/_api/path.py +3 -6
  6. langchain_core/_import_utils.py +3 -4
  7. langchain_core/agents.py +19 -19
  8. langchain_core/caches.py +53 -63
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +323 -334
  11. langchain_core/callbacks/file.py +44 -44
  12. langchain_core/callbacks/manager.py +441 -507
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +32 -32
  15. langchain_core/callbacks/usage.py +60 -57
  16. langchain_core/chat_history.py +48 -63
  17. langchain_core/document_loaders/base.py +23 -23
  18. langchain_core/document_loaders/langsmith.py +37 -37
  19. langchain_core/documents/__init__.py +0 -1
  20. langchain_core/documents/base.py +62 -65
  21. langchain_core/documents/compressor.py +4 -4
  22. langchain_core/documents/transformers.py +28 -29
  23. langchain_core/embeddings/fake.py +50 -54
  24. langchain_core/example_selectors/length_based.py +1 -1
  25. langchain_core/example_selectors/semantic_similarity.py +21 -25
  26. langchain_core/exceptions.py +10 -11
  27. langchain_core/globals.py +3 -151
  28. langchain_core/indexing/api.py +61 -66
  29. langchain_core/indexing/base.py +58 -58
  30. langchain_core/indexing/in_memory.py +3 -3
  31. langchain_core/language_models/__init__.py +14 -27
  32. langchain_core/language_models/_utils.py +270 -84
  33. langchain_core/language_models/base.py +55 -162
  34. langchain_core/language_models/chat_models.py +442 -402
  35. langchain_core/language_models/fake.py +11 -11
  36. langchain_core/language_models/fake_chat_models.py +61 -39
  37. langchain_core/language_models/llms.py +123 -231
  38. langchain_core/load/dump.py +4 -5
  39. langchain_core/load/load.py +18 -28
  40. langchain_core/load/mapping.py +2 -4
  41. langchain_core/load/serializable.py +39 -40
  42. langchain_core/messages/__init__.py +61 -22
  43. langchain_core/messages/ai.py +368 -163
  44. langchain_core/messages/base.py +214 -43
  45. langchain_core/messages/block_translators/__init__.py +111 -0
  46. langchain_core/messages/block_translators/anthropic.py +470 -0
  47. langchain_core/messages/block_translators/bedrock.py +94 -0
  48. langchain_core/messages/block_translators/bedrock_converse.py +297 -0
  49. langchain_core/messages/block_translators/google_genai.py +530 -0
  50. langchain_core/messages/block_translators/google_vertexai.py +21 -0
  51. langchain_core/messages/block_translators/groq.py +143 -0
  52. langchain_core/messages/block_translators/langchain_v0.py +301 -0
  53. langchain_core/messages/block_translators/openai.py +1010 -0
  54. langchain_core/messages/chat.py +2 -6
  55. langchain_core/messages/content.py +1423 -0
  56. langchain_core/messages/function.py +6 -10
  57. langchain_core/messages/human.py +41 -38
  58. langchain_core/messages/modifier.py +2 -2
  59. langchain_core/messages/system.py +38 -28
  60. langchain_core/messages/tool.py +96 -103
  61. langchain_core/messages/utils.py +478 -504
  62. langchain_core/output_parsers/__init__.py +1 -14
  63. langchain_core/output_parsers/base.py +58 -61
  64. langchain_core/output_parsers/json.py +7 -8
  65. langchain_core/output_parsers/list.py +5 -7
  66. langchain_core/output_parsers/openai_functions.py +49 -47
  67. langchain_core/output_parsers/openai_tools.py +14 -19
  68. langchain_core/output_parsers/pydantic.py +12 -13
  69. langchain_core/output_parsers/string.py +2 -2
  70. langchain_core/output_parsers/transform.py +15 -17
  71. langchain_core/output_parsers/xml.py +8 -10
  72. langchain_core/outputs/__init__.py +1 -1
  73. langchain_core/outputs/chat_generation.py +18 -18
  74. langchain_core/outputs/chat_result.py +1 -3
  75. langchain_core/outputs/generation.py +8 -8
  76. langchain_core/outputs/llm_result.py +10 -10
  77. langchain_core/prompt_values.py +12 -12
  78. langchain_core/prompts/__init__.py +3 -27
  79. langchain_core/prompts/base.py +45 -55
  80. langchain_core/prompts/chat.py +254 -313
  81. langchain_core/prompts/dict.py +5 -5
  82. langchain_core/prompts/few_shot.py +81 -88
  83. langchain_core/prompts/few_shot_with_templates.py +11 -13
  84. langchain_core/prompts/image.py +12 -14
  85. langchain_core/prompts/loading.py +6 -8
  86. langchain_core/prompts/message.py +3 -3
  87. langchain_core/prompts/prompt.py +24 -39
  88. langchain_core/prompts/string.py +4 -4
  89. langchain_core/prompts/structured.py +42 -50
  90. langchain_core/rate_limiters.py +51 -60
  91. langchain_core/retrievers.py +49 -190
  92. langchain_core/runnables/base.py +1484 -1709
  93. langchain_core/runnables/branch.py +45 -61
  94. langchain_core/runnables/config.py +80 -88
  95. langchain_core/runnables/configurable.py +117 -134
  96. langchain_core/runnables/fallbacks.py +83 -79
  97. langchain_core/runnables/graph.py +85 -95
  98. langchain_core/runnables/graph_ascii.py +27 -28
  99. langchain_core/runnables/graph_mermaid.py +38 -50
  100. langchain_core/runnables/graph_png.py +15 -16
  101. langchain_core/runnables/history.py +135 -148
  102. langchain_core/runnables/passthrough.py +124 -150
  103. langchain_core/runnables/retry.py +46 -51
  104. langchain_core/runnables/router.py +25 -30
  105. langchain_core/runnables/schema.py +79 -74
  106. langchain_core/runnables/utils.py +62 -68
  107. langchain_core/stores.py +81 -115
  108. langchain_core/structured_query.py +8 -8
  109. langchain_core/sys_info.py +27 -29
  110. langchain_core/tools/__init__.py +1 -14
  111. langchain_core/tools/base.py +179 -187
  112. langchain_core/tools/convert.py +131 -139
  113. langchain_core/tools/render.py +10 -10
  114. langchain_core/tools/retriever.py +11 -11
  115. langchain_core/tools/simple.py +19 -24
  116. langchain_core/tools/structured.py +30 -39
  117. langchain_core/tracers/__init__.py +1 -9
  118. langchain_core/tracers/base.py +97 -99
  119. langchain_core/tracers/context.py +29 -52
  120. langchain_core/tracers/core.py +50 -60
  121. langchain_core/tracers/evaluation.py +11 -11
  122. langchain_core/tracers/event_stream.py +115 -70
  123. langchain_core/tracers/langchain.py +21 -21
  124. langchain_core/tracers/log_stream.py +43 -43
  125. langchain_core/tracers/memory_stream.py +3 -3
  126. langchain_core/tracers/root_listeners.py +16 -16
  127. langchain_core/tracers/run_collector.py +2 -4
  128. langchain_core/tracers/schemas.py +0 -129
  129. langchain_core/tracers/stdout.py +3 -3
  130. langchain_core/utils/__init__.py +1 -4
  131. langchain_core/utils/_merge.py +46 -8
  132. langchain_core/utils/aiter.py +57 -61
  133. langchain_core/utils/env.py +9 -9
  134. langchain_core/utils/function_calling.py +89 -191
  135. langchain_core/utils/html.py +7 -8
  136. langchain_core/utils/input.py +6 -6
  137. langchain_core/utils/interactive_env.py +1 -1
  138. langchain_core/utils/iter.py +37 -42
  139. langchain_core/utils/json.py +4 -3
  140. langchain_core/utils/json_schema.py +8 -8
  141. langchain_core/utils/mustache.py +9 -11
  142. langchain_core/utils/pydantic.py +33 -35
  143. langchain_core/utils/strings.py +5 -5
  144. langchain_core/utils/usage.py +1 -1
  145. langchain_core/utils/utils.py +80 -54
  146. langchain_core/vectorstores/base.py +129 -164
  147. langchain_core/vectorstores/in_memory.py +99 -174
  148. langchain_core/vectorstores/utils.py +5 -5
  149. langchain_core/version.py +1 -1
  150. {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/METADATA +28 -27
  151. langchain_core-1.0.0.dist-info/RECORD +172 -0
  152. {langchain_core-0.3.79.dist-info → langchain_core-1.0.0.dist-info}/WHEEL +1 -1
  153. langchain_core/beta/__init__.py +0 -1
  154. langchain_core/beta/runnables/__init__.py +0 -1
  155. langchain_core/beta/runnables/context.py +0 -447
  156. langchain_core/memory.py +0 -120
  157. langchain_core/messages/content_blocks.py +0 -176
  158. langchain_core/prompts/pipeline.py +0 -138
  159. langchain_core/pydantic_v1/__init__.py +0 -30
  160. langchain_core/pydantic_v1/dataclasses.py +0 -23
  161. langchain_core/pydantic_v1/main.py +0 -23
  162. langchain_core/tracers/langchain_v1.py +0 -31
  163. langchain_core/utils/loading.py +0 -35
  164. langchain_core-0.3.79.dist-info/RECORD +0 -174
  165. langchain_core-0.3.79.dist-info/entry_points.txt +0 -4
@@ -8,10 +8,8 @@ import logging
8
8
  from typing import (
9
9
  TYPE_CHECKING,
10
10
  Any,
11
- Optional,
12
11
  TypedDict,
13
12
  TypeVar,
14
- Union,
15
13
  cast,
16
14
  )
17
15
  from uuid import UUID, uuid4
@@ -72,11 +70,11 @@ class RunInfo(TypedDict):
72
70
  """The type of the run."""
73
71
  inputs: NotRequired[Any]
74
72
  """The inputs to the run."""
75
- parent_run_id: Optional[UUID]
73
+ parent_run_id: UUID | None
76
74
  """The ID of the parent run."""
77
75
 
78
76
 
79
- def _assign_name(name: Optional[str], serialized: Optional[dict[str, Any]]) -> str:
77
+ def _assign_name(name: str | None, serialized: dict[str, Any] | None) -> str:
80
78
  """Assign a name to a run."""
81
79
  if name is not None:
82
80
  return name
@@ -97,12 +95,12 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
97
95
  def __init__(
98
96
  self,
99
97
  *args: Any,
100
- include_names: Optional[Sequence[str]] = None,
101
- include_types: Optional[Sequence[str]] = None,
102
- include_tags: Optional[Sequence[str]] = None,
103
- exclude_names: Optional[Sequence[str]] = None,
104
- exclude_types: Optional[Sequence[str]] = None,
105
- exclude_tags: Optional[Sequence[str]] = None,
98
+ include_names: Sequence[str] | None = None,
99
+ include_types: Sequence[str] | None = None,
100
+ include_tags: Sequence[str] | None = None,
101
+ exclude_names: Sequence[str] | None = None,
102
+ exclude_types: Sequence[str] | None = None,
103
+ exclude_tags: Sequence[str] | None = None,
106
104
  **kwargs: Any,
107
105
  ) -> None:
108
106
  """Initialize the tracer."""
@@ -116,7 +114,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
116
114
  # of a child run, which results in clean up of run_map.
117
115
  # So we keep track of the mapping between children and parent run IDs
118
116
  # in a separate container. This container is GCed when the tracer is GCed.
119
- self.parent_map: dict[UUID, Optional[UUID]] = {}
117
+ self.parent_map: dict[UUID, UUID | None] = {}
120
118
 
121
119
  self.is_tapped: dict[UUID, Any] = {}
122
120
 
@@ -130,7 +128,10 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
130
128
  exclude_tags=exclude_tags,
131
129
  )
132
130
 
133
- loop = asyncio.get_event_loop()
131
+ try:
132
+ loop = asyncio.get_event_loop()
133
+ except RuntimeError:
134
+ loop = asyncio.new_event_loop()
134
135
  memory_stream = _MemoryStream[StreamEvent](loop)
135
136
  self.send_stream = memory_stream.get_send_stream()
136
137
  self.receive_stream = memory_stream.get_receive_stream()
@@ -181,7 +182,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
181
182
  output: The output of the Runnable.
182
183
 
183
184
  Yields:
184
- T: The output of the Runnable.
185
+ The output of the Runnable.
185
186
  """
186
187
  sentinel = object()
187
188
  # atomic check and set
@@ -231,7 +232,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
231
232
  output: The output of the Runnable.
232
233
 
233
234
  Yields:
234
- T: The output of the Runnable.
235
+ The output of the Runnable.
235
236
  """
236
237
  sentinel = object()
237
238
  # atomic check and set
@@ -277,9 +278,9 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
277
278
  self,
278
279
  run_id: UUID,
279
280
  *,
280
- tags: Optional[list[str]],
281
- metadata: Optional[dict[str, Any]],
282
- parent_run_id: Optional[UUID],
281
+ tags: list[str] | None,
282
+ metadata: dict[str, Any] | None,
283
+ parent_run_id: UUID | None,
283
284
  name_: str,
284
285
  run_type: str,
285
286
  **kwargs: Any,
@@ -309,10 +310,10 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
309
310
  messages: list[list[BaseMessage]],
310
311
  *,
311
312
  run_id: UUID,
312
- tags: Optional[list[str]] = None,
313
- parent_run_id: Optional[UUID] = None,
314
- metadata: Optional[dict[str, Any]] = None,
315
- name: Optional[str] = None,
313
+ tags: list[str] | None = None,
314
+ parent_run_id: UUID | None = None,
315
+ metadata: dict[str, Any] | None = None,
316
+ name: str | None = None,
316
317
  **kwargs: Any,
317
318
  ) -> None:
318
319
  """Start a trace for a chat model run."""
@@ -351,10 +352,10 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
351
352
  prompts: list[str],
352
353
  *,
353
354
  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,
355
+ tags: list[str] | None = None,
356
+ parent_run_id: UUID | None = None,
357
+ metadata: dict[str, Any] | None = None,
358
+ name: str | None = None,
358
359
  **kwargs: Any,
359
360
  ) -> None:
360
361
  """Start a trace for a (non-chat model) LLM run."""
@@ -395,8 +396,8 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
395
396
  data: Any,
396
397
  *,
397
398
  run_id: UUID,
398
- tags: Optional[list[str]] = None,
399
- metadata: Optional[dict[str, Any]] = None,
399
+ tags: list[str] | None = None,
400
+ metadata: dict[str, Any] | None = None,
400
401
  **kwargs: Any,
401
402
  ) -> None:
402
403
  """Generate a custom astream event."""
@@ -416,9 +417,9 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
416
417
  self,
417
418
  token: str,
418
419
  *,
419
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
420
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
420
421
  run_id: UUID,
421
- parent_run_id: Optional[UUID] = None,
422
+ parent_run_id: UUID | None = None,
422
423
  **kwargs: Any,
423
424
  ) -> None:
424
425
  """Run on new output token. Only available when streaming is enabled.
@@ -426,7 +427,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
426
427
  For both chat models and non-chat models (legacy LLMs).
427
428
  """
428
429
  run_info = self.run_map.get(run_id)
429
- chunk_: Union[GenerationChunk, BaseMessageChunk]
430
+ chunk_: GenerationChunk | BaseMessageChunk
430
431
 
431
432
  if run_info is None:
432
433
  msg = f"Run ID {run_id} not found in run map."
@@ -475,13 +476,13 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
475
476
  For both chat models and non-chat models (legacy LLMs).
476
477
 
477
478
  Raises:
478
- ValueError: If the run type is not ``'llm'`` or ``'chat_model'``.
479
+ ValueError: If the run type is not `'llm'` or `'chat_model'`.
479
480
  """
480
481
  run_info = self.run_map.pop(run_id)
481
482
  inputs_ = run_info.get("inputs")
482
483
 
483
- generations: Union[list[list[GenerationChunk]], list[list[ChatGenerationChunk]]]
484
- output: Union[dict, BaseMessage] = {}
484
+ generations: list[list[GenerationChunk]] | list[list[ChatGenerationChunk]]
485
+ output: dict | BaseMessage = {}
485
486
 
486
487
  if run_info["run_type"] == "chat_model":
487
488
  generations = cast("list[list[ChatGenerationChunk]]", response.generations)
@@ -533,11 +534,11 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
533
534
  inputs: dict[str, Any],
534
535
  *,
535
536
  run_id: UUID,
536
- tags: Optional[list[str]] = None,
537
- parent_run_id: Optional[UUID] = None,
538
- metadata: Optional[dict[str, Any]] = None,
539
- run_type: Optional[str] = None,
540
- name: Optional[str] = None,
537
+ tags: list[str] | None = None,
538
+ parent_run_id: UUID | None = None,
539
+ metadata: dict[str, Any] | None = None,
540
+ run_type: str | None = None,
541
+ name: str | None = None,
541
542
  **kwargs: Any,
542
543
  ) -> None:
543
544
  """Start a trace for a chain run."""
@@ -581,7 +582,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
581
582
  outputs: dict[str, Any],
582
583
  *,
583
584
  run_id: UUID,
584
- inputs: Optional[dict[str, Any]] = None,
585
+ inputs: dict[str, Any] | None = None,
585
586
  **kwargs: Any,
586
587
  ) -> None:
587
588
  """End a trace for a chain run."""
@@ -610,6 +611,28 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
610
611
  run_type,
611
612
  )
612
613
 
614
+ def _get_tool_run_info_with_inputs(self, run_id: UUID) -> tuple[RunInfo, Any]:
615
+ """Get run info for a tool and extract inputs, with validation.
616
+
617
+ Args:
618
+ run_id: The run ID of the tool.
619
+
620
+ Returns:
621
+ A tuple of (run_info, inputs).
622
+
623
+ Raises:
624
+ AssertionError: If the run ID is a tool call and does not have inputs.
625
+ """
626
+ run_info = self.run_map.pop(run_id)
627
+ if "inputs" not in run_info:
628
+ msg = (
629
+ f"Run ID {run_id} is a tool call and is expected to have "
630
+ f"inputs associated with it."
631
+ )
632
+ raise AssertionError(msg)
633
+ inputs = run_info["inputs"]
634
+ return run_info, inputs
635
+
613
636
  @override
614
637
  async def on_tool_start(
615
638
  self,
@@ -617,11 +640,11 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
617
640
  input_str: str,
618
641
  *,
619
642
  run_id: UUID,
620
- tags: Optional[list[str]] = None,
621
- parent_run_id: Optional[UUID] = None,
622
- metadata: Optional[dict[str, Any]] = None,
623
- name: Optional[str] = None,
624
- inputs: Optional[dict[str, Any]] = None,
643
+ tags: list[str] | None = None,
644
+ parent_run_id: UUID | None = None,
645
+ metadata: dict[str, Any] | None = None,
646
+ name: str | None = None,
647
+ inputs: dict[str, Any] | None = None,
625
648
  **kwargs: Any,
626
649
  ) -> None:
627
650
  """Start a trace for a tool run."""
@@ -652,6 +675,35 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
652
675
  "tool",
653
676
  )
654
677
 
678
+ @override
679
+ async def on_tool_error(
680
+ self,
681
+ error: BaseException,
682
+ *,
683
+ run_id: UUID,
684
+ parent_run_id: UUID | None = None,
685
+ tags: list[str] | None = None,
686
+ **kwargs: Any,
687
+ ) -> None:
688
+ """Run when tool errors."""
689
+ run_info, inputs = self._get_tool_run_info_with_inputs(run_id)
690
+
691
+ self._send(
692
+ {
693
+ "event": "on_tool_error",
694
+ "data": {
695
+ "error": error,
696
+ "input": inputs,
697
+ },
698
+ "run_id": str(run_id),
699
+ "name": run_info["name"],
700
+ "tags": run_info["tags"],
701
+ "metadata": run_info["metadata"],
702
+ "parent_ids": self._get_parent_ids(run_id),
703
+ },
704
+ "tool",
705
+ )
706
+
655
707
  @override
656
708
  async def on_tool_end(self, output: Any, *, run_id: UUID, **kwargs: Any) -> None:
657
709
  """End a trace for a tool run.
@@ -659,14 +711,7 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
659
711
  Raises:
660
712
  AssertionError: If the run ID is a tool call and does not have inputs
661
713
  """
662
- run_info = self.run_map.pop(run_id)
663
- if "inputs" not in run_info:
664
- msg = (
665
- f"Run ID {run_id} is a tool call and is expected to have "
666
- f"inputs associated with it."
667
- )
668
- raise AssertionError(msg)
669
- inputs = run_info["inputs"]
714
+ run_info, inputs = self._get_tool_run_info_with_inputs(run_id)
670
715
 
671
716
  self._send(
672
717
  {
@@ -691,10 +736,10 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
691
736
  query: str,
692
737
  *,
693
738
  run_id: UUID,
694
- parent_run_id: Optional[UUID] = None,
695
- tags: Optional[list[str]] = None,
696
- metadata: Optional[dict[str, Any]] = None,
697
- name: Optional[str] = None,
739
+ parent_run_id: UUID | None = None,
740
+ tags: list[str] | None = None,
741
+ metadata: dict[str, Any] | None = None,
742
+ name: str | None = None,
698
743
  **kwargs: Any,
699
744
  ) -> None:
700
745
  """Run when Retriever starts running."""
@@ -763,14 +808,14 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
763
808
  async def _astream_events_implementation_v1(
764
809
  runnable: Runnable[Input, Output],
765
810
  value: Any,
766
- config: Optional[RunnableConfig] = None,
811
+ config: RunnableConfig | None = None,
767
812
  *,
768
- include_names: Optional[Sequence[str]] = None,
769
- include_types: Optional[Sequence[str]] = None,
770
- include_tags: Optional[Sequence[str]] = None,
771
- exclude_names: Optional[Sequence[str]] = None,
772
- exclude_types: Optional[Sequence[str]] = None,
773
- exclude_tags: Optional[Sequence[str]] = None,
813
+ include_names: Sequence[str] | None = None,
814
+ include_types: Sequence[str] | None = None,
815
+ include_tags: Sequence[str] | None = None,
816
+ exclude_names: Sequence[str] | None = None,
817
+ exclude_types: Sequence[str] | None = None,
818
+ exclude_tags: Sequence[str] | None = None,
774
819
  **kwargs: Any,
775
820
  ) -> AsyncIterator[StandardStreamEvent]:
776
821
  stream = LogStreamCallbackHandler(
@@ -939,14 +984,14 @@ async def _astream_events_implementation_v1(
939
984
  async def _astream_events_implementation_v2(
940
985
  runnable: Runnable[Input, Output],
941
986
  value: Any,
942
- config: Optional[RunnableConfig] = None,
987
+ config: RunnableConfig | None = None,
943
988
  *,
944
- include_names: Optional[Sequence[str]] = None,
945
- include_types: Optional[Sequence[str]] = None,
946
- include_tags: Optional[Sequence[str]] = None,
947
- exclude_names: Optional[Sequence[str]] = None,
948
- exclude_types: Optional[Sequence[str]] = None,
949
- exclude_tags: Optional[Sequence[str]] = None,
989
+ include_names: Sequence[str] | None = None,
990
+ include_types: Sequence[str] | None = None,
991
+ include_tags: Sequence[str] | None = None,
992
+ exclude_names: Sequence[str] | None = None,
993
+ exclude_types: Sequence[str] | None = None,
994
+ exclude_tags: Sequence[str] | None = None,
950
995
  **kwargs: Any,
951
996
  ) -> AsyncIterator[StandardStreamEvent]:
952
997
  """Implementation of the astream events API for V2 runnables."""
@@ -5,7 +5,7 @@ from __future__ import annotations
5
5
  import logging
6
6
  from concurrent.futures import ThreadPoolExecutor
7
7
  from datetime import datetime, timezone
8
- from typing import TYPE_CHECKING, Any, Optional, Union
8
+ from typing import TYPE_CHECKING, Any
9
9
  from uuid import UUID
10
10
 
11
11
  from langsmith import Client, get_tracing_context
@@ -30,7 +30,7 @@ if TYPE_CHECKING:
30
30
 
31
31
  logger = logging.getLogger(__name__)
32
32
  _LOGGED = set()
33
- _EXECUTOR: Optional[ThreadPoolExecutor] = None
33
+ _EXECUTOR: ThreadPoolExecutor | None = None
34
34
 
35
35
 
36
36
  def log_error_once(method: str, exception: Exception) -> None:
@@ -76,10 +76,10 @@ class LangChainTracer(BaseTracer):
76
76
 
77
77
  def __init__(
78
78
  self,
79
- example_id: Optional[Union[UUID, str]] = None,
80
- project_name: Optional[str] = None,
81
- client: Optional[Client] = None,
82
- tags: Optional[list[str]] = None,
79
+ example_id: UUID | str | None = None,
80
+ project_name: str | None = None,
81
+ client: Client | None = None,
82
+ tags: list[str] | None = None,
83
83
  **kwargs: Any,
84
84
  ) -> None:
85
85
  """Initialize the LangChain tracer.
@@ -89,7 +89,7 @@ class LangChainTracer(BaseTracer):
89
89
  project_name: The project name. Defaults to the tracer project.
90
90
  client: The client. Defaults to the global client.
91
91
  tags: The tags. Defaults to an empty list.
92
- kwargs: Additional keyword arguments.
92
+ **kwargs: Additional keyword arguments.
93
93
  """
94
94
  super().__init__(**kwargs)
95
95
  self.example_id = (
@@ -98,7 +98,7 @@ class LangChainTracer(BaseTracer):
98
98
  self.project_name = project_name or ls_utils.get_tracer_project()
99
99
  self.client = client or get_client()
100
100
  self.tags = tags or []
101
- self.latest_run: Optional[Run] = None
101
+ self.latest_run: Run | None = None
102
102
  self.run_has_token_event_map: dict[str, bool] = {}
103
103
 
104
104
  def _start_trace(self, run: Run) -> None:
@@ -122,10 +122,10 @@ class LangChainTracer(BaseTracer):
122
122
  messages: list[list[BaseMessage]],
123
123
  *,
124
124
  run_id: UUID,
125
- tags: Optional[list[str]] = None,
126
- parent_run_id: Optional[UUID] = None,
127
- metadata: Optional[dict[str, Any]] = None,
128
- name: Optional[str] = None,
125
+ tags: list[str] | None = None,
126
+ parent_run_id: UUID | None = None,
127
+ metadata: dict[str, Any] | None = None,
128
+ name: str | None = None,
129
129
  **kwargs: Any,
130
130
  ) -> Run:
131
131
  """Start a trace for an LLM run.
@@ -134,14 +134,14 @@ class LangChainTracer(BaseTracer):
134
134
  serialized: The serialized model.
135
135
  messages: The messages.
136
136
  run_id: The run ID.
137
- tags: The tags. Defaults to None.
138
- parent_run_id: The parent run ID. Defaults to None.
139
- metadata: The metadata. Defaults to None.
140
- name: The name. Defaults to None.
141
- kwargs: Additional keyword arguments.
137
+ tags: The tags.
138
+ parent_run_id: The parent run ID.
139
+ metadata: The metadata.
140
+ name: The name.
141
+ **kwargs: Additional keyword arguments.
142
142
 
143
143
  Returns:
144
- Run: The run.
144
+ The run.
145
145
  """
146
146
  start_time = datetime.now(timezone.utc)
147
147
  if metadata:
@@ -175,7 +175,7 @@ class LangChainTracer(BaseTracer):
175
175
  """Get the LangSmith root run URL.
176
176
 
177
177
  Returns:
178
- str: The LangSmith root run URL.
178
+ The LangSmith root run URL.
179
179
 
180
180
  Raises:
181
181
  ValueError: If no traced run is found.
@@ -242,8 +242,8 @@ class LangChainTracer(BaseTracer):
242
242
  self,
243
243
  token: str,
244
244
  run_id: UUID,
245
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
246
- parent_run_id: Optional[UUID] = None,
245
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
246
+ parent_run_id: UUID | None = None,
247
247
  ) -> Run:
248
248
  run_id_str = str(run_id)
249
249
  if run_id_str not in self.run_has_token_event_map: