langchain-core 1.0.0a5__py3-none-any.whl → 1.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. langchain_core/__init__.py +1 -1
  2. langchain_core/_api/__init__.py +3 -4
  3. langchain_core/_api/beta_decorator.py +23 -26
  4. langchain_core/_api/deprecation.py +51 -64
  5. langchain_core/_api/path.py +3 -6
  6. langchain_core/_import_utils.py +3 -4
  7. langchain_core/agents.py +20 -22
  8. langchain_core/caches.py +65 -66
  9. langchain_core/callbacks/__init__.py +1 -8
  10. langchain_core/callbacks/base.py +321 -336
  11. langchain_core/callbacks/file.py +44 -44
  12. langchain_core/callbacks/manager.py +436 -513
  13. langchain_core/callbacks/stdout.py +29 -30
  14. langchain_core/callbacks/streaming_stdout.py +32 -32
  15. langchain_core/callbacks/usage.py +60 -57
  16. langchain_core/chat_history.py +53 -68
  17. langchain_core/document_loaders/base.py +27 -25
  18. langchain_core/document_loaders/blob_loaders.py +1 -1
  19. langchain_core/document_loaders/langsmith.py +44 -48
  20. langchain_core/documents/__init__.py +23 -3
  21. langchain_core/documents/base.py +98 -90
  22. langchain_core/documents/compressor.py +10 -10
  23. langchain_core/documents/transformers.py +34 -35
  24. langchain_core/embeddings/fake.py +50 -54
  25. langchain_core/example_selectors/length_based.py +1 -1
  26. langchain_core/example_selectors/semantic_similarity.py +28 -32
  27. langchain_core/exceptions.py +21 -20
  28. langchain_core/globals.py +3 -151
  29. langchain_core/indexing/__init__.py +1 -1
  30. langchain_core/indexing/api.py +121 -126
  31. langchain_core/indexing/base.py +73 -75
  32. langchain_core/indexing/in_memory.py +4 -6
  33. langchain_core/language_models/__init__.py +14 -29
  34. langchain_core/language_models/_utils.py +58 -61
  35. langchain_core/language_models/base.py +53 -162
  36. langchain_core/language_models/chat_models.py +298 -387
  37. langchain_core/language_models/fake.py +11 -11
  38. langchain_core/language_models/fake_chat_models.py +42 -36
  39. langchain_core/language_models/llms.py +125 -235
  40. langchain_core/load/dump.py +9 -12
  41. langchain_core/load/load.py +18 -28
  42. langchain_core/load/mapping.py +2 -4
  43. langchain_core/load/serializable.py +42 -40
  44. langchain_core/messages/__init__.py +10 -16
  45. langchain_core/messages/ai.py +148 -148
  46. langchain_core/messages/base.py +58 -52
  47. langchain_core/messages/block_translators/__init__.py +27 -17
  48. langchain_core/messages/block_translators/anthropic.py +6 -6
  49. langchain_core/messages/block_translators/bedrock_converse.py +5 -5
  50. langchain_core/messages/block_translators/google_genai.py +505 -20
  51. langchain_core/messages/block_translators/google_vertexai.py +4 -32
  52. langchain_core/messages/block_translators/groq.py +117 -21
  53. langchain_core/messages/block_translators/langchain_v0.py +5 -5
  54. langchain_core/messages/block_translators/openai.py +11 -11
  55. langchain_core/messages/chat.py +2 -6
  56. langchain_core/messages/content.py +337 -328
  57. langchain_core/messages/function.py +6 -10
  58. langchain_core/messages/human.py +24 -31
  59. langchain_core/messages/modifier.py +2 -2
  60. langchain_core/messages/system.py +19 -29
  61. langchain_core/messages/tool.py +74 -90
  62. langchain_core/messages/utils.py +474 -504
  63. langchain_core/output_parsers/__init__.py +13 -10
  64. langchain_core/output_parsers/base.py +61 -61
  65. langchain_core/output_parsers/format_instructions.py +9 -4
  66. langchain_core/output_parsers/json.py +12 -10
  67. langchain_core/output_parsers/list.py +21 -23
  68. langchain_core/output_parsers/openai_functions.py +49 -47
  69. langchain_core/output_parsers/openai_tools.py +16 -21
  70. langchain_core/output_parsers/pydantic.py +13 -14
  71. langchain_core/output_parsers/string.py +5 -5
  72. langchain_core/output_parsers/transform.py +15 -17
  73. langchain_core/output_parsers/xml.py +35 -34
  74. langchain_core/outputs/__init__.py +1 -1
  75. langchain_core/outputs/chat_generation.py +18 -18
  76. langchain_core/outputs/chat_result.py +1 -3
  77. langchain_core/outputs/generation.py +10 -11
  78. langchain_core/outputs/llm_result.py +10 -10
  79. langchain_core/prompt_values.py +11 -17
  80. langchain_core/prompts/__init__.py +3 -27
  81. langchain_core/prompts/base.py +48 -56
  82. langchain_core/prompts/chat.py +275 -325
  83. langchain_core/prompts/dict.py +5 -5
  84. langchain_core/prompts/few_shot.py +81 -88
  85. langchain_core/prompts/few_shot_with_templates.py +11 -13
  86. langchain_core/prompts/image.py +12 -14
  87. langchain_core/prompts/loading.py +4 -6
  88. langchain_core/prompts/message.py +3 -3
  89. langchain_core/prompts/prompt.py +24 -39
  90. langchain_core/prompts/string.py +26 -10
  91. langchain_core/prompts/structured.py +49 -53
  92. langchain_core/rate_limiters.py +51 -60
  93. langchain_core/retrievers.py +61 -198
  94. langchain_core/runnables/base.py +1478 -1630
  95. langchain_core/runnables/branch.py +53 -57
  96. langchain_core/runnables/config.py +72 -89
  97. langchain_core/runnables/configurable.py +120 -137
  98. langchain_core/runnables/fallbacks.py +83 -79
  99. langchain_core/runnables/graph.py +91 -97
  100. langchain_core/runnables/graph_ascii.py +27 -28
  101. langchain_core/runnables/graph_mermaid.py +38 -50
  102. langchain_core/runnables/graph_png.py +15 -16
  103. langchain_core/runnables/history.py +135 -148
  104. langchain_core/runnables/passthrough.py +124 -150
  105. langchain_core/runnables/retry.py +46 -51
  106. langchain_core/runnables/router.py +25 -30
  107. langchain_core/runnables/schema.py +75 -80
  108. langchain_core/runnables/utils.py +60 -67
  109. langchain_core/stores.py +85 -121
  110. langchain_core/structured_query.py +8 -8
  111. langchain_core/sys_info.py +27 -29
  112. langchain_core/tools/__init__.py +1 -14
  113. langchain_core/tools/base.py +285 -229
  114. langchain_core/tools/convert.py +160 -155
  115. langchain_core/tools/render.py +10 -10
  116. langchain_core/tools/retriever.py +12 -11
  117. langchain_core/tools/simple.py +19 -24
  118. langchain_core/tools/structured.py +32 -39
  119. langchain_core/tracers/__init__.py +1 -9
  120. langchain_core/tracers/base.py +97 -99
  121. langchain_core/tracers/context.py +29 -52
  122. langchain_core/tracers/core.py +49 -53
  123. langchain_core/tracers/evaluation.py +11 -11
  124. langchain_core/tracers/event_stream.py +65 -64
  125. langchain_core/tracers/langchain.py +21 -21
  126. langchain_core/tracers/log_stream.py +45 -45
  127. langchain_core/tracers/memory_stream.py +3 -3
  128. langchain_core/tracers/root_listeners.py +16 -16
  129. langchain_core/tracers/run_collector.py +2 -4
  130. langchain_core/tracers/schemas.py +0 -129
  131. langchain_core/tracers/stdout.py +3 -3
  132. langchain_core/utils/__init__.py +1 -4
  133. langchain_core/utils/_merge.py +2 -2
  134. langchain_core/utils/aiter.py +57 -61
  135. langchain_core/utils/env.py +9 -9
  136. langchain_core/utils/function_calling.py +89 -186
  137. langchain_core/utils/html.py +7 -8
  138. langchain_core/utils/input.py +6 -6
  139. langchain_core/utils/interactive_env.py +1 -1
  140. langchain_core/utils/iter.py +36 -40
  141. langchain_core/utils/json.py +4 -3
  142. langchain_core/utils/json_schema.py +9 -9
  143. langchain_core/utils/mustache.py +8 -10
  144. langchain_core/utils/pydantic.py +33 -35
  145. langchain_core/utils/strings.py +6 -9
  146. langchain_core/utils/usage.py +1 -1
  147. langchain_core/utils/utils.py +66 -62
  148. langchain_core/vectorstores/base.py +182 -216
  149. langchain_core/vectorstores/in_memory.py +101 -176
  150. langchain_core/vectorstores/utils.py +5 -5
  151. langchain_core/version.py +1 -1
  152. langchain_core-1.0.3.dist-info/METADATA +69 -0
  153. langchain_core-1.0.3.dist-info/RECORD +172 -0
  154. {langchain_core-1.0.0a5.dist-info → langchain_core-1.0.3.dist-info}/WHEEL +1 -1
  155. langchain_core/memory.py +0 -120
  156. langchain_core/messages/block_translators/ollama.py +0 -47
  157. langchain_core/prompts/pipeline.py +0 -138
  158. langchain_core/pydantic_v1/__init__.py +0 -30
  159. langchain_core/pydantic_v1/dataclasses.py +0 -23
  160. langchain_core/pydantic_v1/main.py +0 -23
  161. langchain_core/tracers/langchain_v1.py +0 -31
  162. langchain_core/utils/loading.py +0 -35
  163. langchain_core-1.0.0a5.dist-info/METADATA +0 -77
  164. langchain_core-1.0.0a5.dist-info/RECORD +0 -181
  165. langchain_core-1.0.0a5.dist-info/entry_points.txt +0 -4
@@ -10,8 +10,6 @@ from typing import (
10
10
  TYPE_CHECKING,
11
11
  Any,
12
12
  Literal,
13
- Optional,
14
- Union,
15
13
  cast,
16
14
  )
17
15
 
@@ -81,7 +79,7 @@ class _TracerCore(ABC):
81
79
  """Map of run ID to (trace_id, dotted_order). Cleared when tracer GCed."""
82
80
 
83
81
  @abstractmethod
84
- def _persist_run(self, run: Run) -> Union[Coroutine[Any, Any, None], None]:
82
+ def _persist_run(self, run: Run) -> Coroutine[Any, Any, None] | None:
85
83
  """Persist a run."""
86
84
 
87
85
  @staticmethod
@@ -102,7 +100,7 @@ class _TracerCore(ABC):
102
100
  except: # noqa: E722
103
101
  return msg
104
102
 
105
- def _start_trace(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # type: ignore[return]
103
+ def _start_trace(self, run: Run) -> Coroutine[Any, Any, None] | None: # type: ignore[return]
106
104
  current_dotted_order = run.start_time.strftime("%Y%m%dT%H%M%S%fZ") + str(run.id)
107
105
  if run.parent_run_id:
108
106
  if parent := self.order_map.get(run.parent_run_id):
@@ -126,9 +124,7 @@ class _TracerCore(ABC):
126
124
  self.order_map[run.id] = (run.trace_id, run.dotted_order)
127
125
  self.run_map[str(run.id)] = run
128
126
 
129
- def _get_run(
130
- self, run_id: UUID, run_type: Union[str, set[str], None] = None
131
- ) -> Run:
127
+ def _get_run(self, run_id: UUID, run_type: str | set[str] | None = None) -> Run:
132
128
  try:
133
129
  run = self.run_map[str(run_id)]
134
130
  except KeyError as exc:
@@ -136,7 +132,7 @@ class _TracerCore(ABC):
136
132
  raise TracerException(msg) from exc
137
133
 
138
134
  if isinstance(run_type, str):
139
- run_types: Union[set[str], None] = {run_type}
135
+ run_types: set[str] | None = {run_type}
140
136
  else:
141
137
  run_types = run_type
142
138
  if run_types is not None and run.run_type not in run_types:
@@ -152,10 +148,10 @@ class _TracerCore(ABC):
152
148
  serialized: dict[str, Any],
153
149
  messages: list[list[BaseMessage]],
154
150
  run_id: UUID,
155
- tags: Optional[list[str]] = None,
156
- parent_run_id: Optional[UUID] = None,
157
- metadata: Optional[dict[str, Any]] = None,
158
- name: Optional[str] = None,
151
+ tags: list[str] | None = None,
152
+ parent_run_id: UUID | None = None,
153
+ metadata: dict[str, Any] | None = None,
154
+ name: str | None = None,
159
155
  **kwargs: Any,
160
156
  ) -> Run:
161
157
  """Create a chat model run."""
@@ -196,10 +192,10 @@ class _TracerCore(ABC):
196
192
  serialized: dict[str, Any],
197
193
  prompts: list[str],
198
194
  run_id: UUID,
199
- tags: Optional[list[str]] = None,
200
- parent_run_id: Optional[UUID] = None,
201
- metadata: Optional[dict[str, Any]] = None,
202
- name: Optional[str] = None,
195
+ tags: list[str] | None = None,
196
+ parent_run_id: UUID | None = None,
197
+ metadata: dict[str, Any] | None = None,
198
+ name: str | None = None,
203
199
  **kwargs: Any,
204
200
  ) -> Run:
205
201
  """Create a llm run."""
@@ -224,8 +220,8 @@ class _TracerCore(ABC):
224
220
  self,
225
221
  token: str,
226
222
  run_id: UUID,
227
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
228
- parent_run_id: Optional[UUID] = None, # noqa: ARG002
223
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
224
+ parent_run_id: UUID | None = None, # noqa: ARG002
229
225
  ) -> Run:
230
226
  """Append token event to LLM run and return the run."""
231
227
  llm_run = self._get_run(run_id, run_type={"llm", "chat_model"})
@@ -291,7 +287,7 @@ class _TracerCore(ABC):
291
287
  return llm_run
292
288
 
293
289
  def _errored_llm_run(
294
- self, error: BaseException, run_id: UUID, response: Optional[LLMResult] = None
290
+ self, error: BaseException, run_id: UUID, response: LLMResult | None = None
295
291
  ) -> Run:
296
292
  llm_run = self._get_run(run_id, run_type={"llm", "chat_model"})
297
293
  llm_run.error = self._get_stacktrace(error)
@@ -319,11 +315,11 @@ class _TracerCore(ABC):
319
315
  serialized: dict[str, Any],
320
316
  inputs: dict[str, Any],
321
317
  run_id: UUID,
322
- tags: Optional[list[str]] = None,
323
- parent_run_id: Optional[UUID] = None,
324
- metadata: Optional[dict[str, Any]] = None,
325
- run_type: Optional[str] = None,
326
- name: Optional[str] = None,
318
+ tags: list[str] | None = None,
319
+ parent_run_id: UUID | None = None,
320
+ metadata: dict[str, Any] | None = None,
321
+ run_type: str | None = None,
322
+ name: str | None = None,
327
323
  **kwargs: Any,
328
324
  ) -> Run:
329
325
  """Create a chain Run."""
@@ -370,7 +366,7 @@ class _TracerCore(ABC):
370
366
  self,
371
367
  outputs: dict[str, Any],
372
368
  run_id: UUID,
373
- inputs: Optional[dict[str, Any]] = None,
369
+ inputs: dict[str, Any] | None = None,
374
370
  ) -> Run:
375
371
  """Update a chain run with outputs and end time."""
376
372
  chain_run = self._get_run(run_id)
@@ -389,7 +385,7 @@ class _TracerCore(ABC):
389
385
  def _errored_chain_run(
390
386
  self,
391
387
  error: BaseException,
392
- inputs: Optional[dict[str, Any]],
388
+ inputs: dict[str, Any] | None,
393
389
  run_id: UUID,
394
390
  ) -> Run:
395
391
  chain_run = self._get_run(run_id)
@@ -405,11 +401,11 @@ class _TracerCore(ABC):
405
401
  serialized: dict[str, Any],
406
402
  input_str: str,
407
403
  run_id: UUID,
408
- tags: Optional[list[str]] = None,
409
- parent_run_id: Optional[UUID] = None,
410
- metadata: Optional[dict[str, Any]] = None,
411
- name: Optional[str] = None,
412
- inputs: Optional[dict[str, Any]] = None,
404
+ tags: list[str] | None = None,
405
+ parent_run_id: UUID | None = None,
406
+ metadata: dict[str, Any] | None = None,
407
+ name: str | None = None,
408
+ inputs: dict[str, Any] | None = None,
413
409
  **kwargs: Any,
414
410
  ) -> Run:
415
411
  """Create a tool run."""
@@ -472,10 +468,10 @@ class _TracerCore(ABC):
472
468
  serialized: dict[str, Any],
473
469
  query: str,
474
470
  run_id: UUID,
475
- parent_run_id: Optional[UUID] = None,
476
- tags: Optional[list[str]] = None,
477
- metadata: Optional[dict[str, Any]] = None,
478
- name: Optional[str] = None,
471
+ parent_run_id: UUID | None = None,
472
+ tags: list[str] | None = None,
473
+ metadata: dict[str, Any] | None = None,
474
+ name: str | None = None,
479
475
  **kwargs: Any,
480
476
  ) -> Run:
481
477
  """Create a retrieval run."""
@@ -532,7 +528,7 @@ class _TracerCore(ABC):
532
528
  """Return self copied."""
533
529
  return self
534
530
 
535
- def _end_trace(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
531
+ def _end_trace(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
536
532
  """End a trace for a run.
537
533
 
538
534
  Args:
@@ -540,7 +536,7 @@ class _TracerCore(ABC):
540
536
  """
541
537
  return None
542
538
 
543
- def _on_run_create(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
539
+ def _on_run_create(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
544
540
  """Process a run upon creation.
545
541
 
546
542
  Args:
@@ -548,7 +544,7 @@ class _TracerCore(ABC):
548
544
  """
549
545
  return None
550
546
 
551
- def _on_run_update(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
547
+ def _on_run_update(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
552
548
  """Process a run upon update.
553
549
 
554
550
  Args:
@@ -556,7 +552,7 @@ class _TracerCore(ABC):
556
552
  """
557
553
  return None
558
554
 
559
- def _on_llm_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
555
+ def _on_llm_start(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
560
556
  """Process the LLM Run upon start.
561
557
 
562
558
  Args:
@@ -568,8 +564,8 @@ class _TracerCore(ABC):
568
564
  self,
569
565
  run: Run, # noqa: ARG002
570
566
  token: str, # noqa: ARG002
571
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]], # noqa: ARG002
572
- ) -> Union[Coroutine[Any, Any, None], None]:
567
+ chunk: GenerationChunk | ChatGenerationChunk | None, # noqa: ARG002
568
+ ) -> Coroutine[Any, Any, None] | None:
573
569
  """Process new LLM token.
574
570
 
575
571
  Args:
@@ -579,7 +575,7 @@ class _TracerCore(ABC):
579
575
  """
580
576
  return None
581
577
 
582
- def _on_llm_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
578
+ def _on_llm_end(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
583
579
  """Process the LLM Run.
584
580
 
585
581
  Args:
@@ -587,7 +583,7 @@ class _TracerCore(ABC):
587
583
  """
588
584
  return None
589
585
 
590
- def _on_llm_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
586
+ def _on_llm_error(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
591
587
  """Process the LLM Run upon error.
592
588
 
593
589
  Args:
@@ -595,7 +591,7 @@ class _TracerCore(ABC):
595
591
  """
596
592
  return None
597
593
 
598
- def _on_chain_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
594
+ def _on_chain_start(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
599
595
  """Process the Chain Run upon start.
600
596
 
601
597
  Args:
@@ -603,7 +599,7 @@ class _TracerCore(ABC):
603
599
  """
604
600
  return None
605
601
 
606
- def _on_chain_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
602
+ def _on_chain_end(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
607
603
  """Process the Chain Run.
608
604
 
609
605
  Args:
@@ -611,7 +607,7 @@ class _TracerCore(ABC):
611
607
  """
612
608
  return None
613
609
 
614
- def _on_chain_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
610
+ def _on_chain_error(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
615
611
  """Process the Chain Run upon error.
616
612
 
617
613
  Args:
@@ -619,7 +615,7 @@ class _TracerCore(ABC):
619
615
  """
620
616
  return None
621
617
 
622
- def _on_tool_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
618
+ def _on_tool_start(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
623
619
  """Process the Tool Run upon start.
624
620
 
625
621
  Args:
@@ -627,7 +623,7 @@ class _TracerCore(ABC):
627
623
  """
628
624
  return None
629
625
 
630
- def _on_tool_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
626
+ def _on_tool_end(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
631
627
  """Process the Tool Run.
632
628
 
633
629
  Args:
@@ -635,7 +631,7 @@ class _TracerCore(ABC):
635
631
  """
636
632
  return None
637
633
 
638
- def _on_tool_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
634
+ def _on_tool_error(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
639
635
  """Process the Tool Run upon error.
640
636
 
641
637
  Args:
@@ -643,7 +639,7 @@ class _TracerCore(ABC):
643
639
  """
644
640
  return None
645
641
 
646
- def _on_chat_model_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
642
+ def _on_chat_model_start(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
647
643
  """Process the Chat Model Run upon start.
648
644
 
649
645
  Args:
@@ -651,7 +647,7 @@ class _TracerCore(ABC):
651
647
  """
652
648
  return None
653
649
 
654
- def _on_retriever_start(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
650
+ def _on_retriever_start(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
655
651
  """Process the Retriever Run upon start.
656
652
 
657
653
  Args:
@@ -659,7 +655,7 @@ class _TracerCore(ABC):
659
655
  """
660
656
  return None
661
657
 
662
- def _on_retriever_end(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
658
+ def _on_retriever_end(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
663
659
  """Process the Retriever Run.
664
660
 
665
661
  Args:
@@ -667,7 +663,7 @@ class _TracerCore(ABC):
667
663
  """
668
664
  return None
669
665
 
670
- def _on_retriever_error(self, run: Run) -> Union[Coroutine[Any, Any, None], None]: # noqa: ARG002
666
+ def _on_retriever_error(self, run: Run) -> Coroutine[Any, Any, None] | None: # noqa: ARG002
671
667
  """Process the Retriever Run upon error.
672
668
 
673
669
  Args:
@@ -6,7 +6,7 @@ import logging
6
6
  import threading
7
7
  import weakref
8
8
  from concurrent.futures import Future, ThreadPoolExecutor, wait
9
- from typing import TYPE_CHECKING, Any, Optional, Union, cast
9
+ from typing import TYPE_CHECKING, Any, cast
10
10
  from uuid import UUID
11
11
 
12
12
  import langsmith
@@ -43,19 +43,19 @@ class EvaluatorCallbackHandler(BaseTracer):
43
43
  """
44
44
 
45
45
  name: str = "evaluator_callback_handler"
46
- example_id: Optional[UUID] = None
46
+ example_id: UUID | None = None
47
47
  """The example ID associated with the runs."""
48
48
  client: langsmith.Client
49
49
  """The LangSmith client instance used for evaluating the runs."""
50
50
  evaluators: Sequence[langsmith.RunEvaluator] = ()
51
51
  """The sequence of run evaluators to be executed."""
52
- executor: Optional[ThreadPoolExecutor] = None
52
+ executor: ThreadPoolExecutor | None = None
53
53
  """The thread pool executor used for running the evaluators."""
54
54
  futures: weakref.WeakSet[Future] = weakref.WeakSet()
55
55
  """The set of futures representing the running evaluators."""
56
56
  skip_unfinished: bool = True
57
57
  """Whether to skip runs that are not finished or raised an error."""
58
- project_name: Optional[str] = None
58
+ project_name: str | None = None
59
59
  """The LangSmith project name to be organize eval chain runs under."""
60
60
  logged_eval_results: dict[tuple[str, str], list[EvaluationResult]]
61
61
  lock: threading.Lock
@@ -63,11 +63,11 @@ class EvaluatorCallbackHandler(BaseTracer):
63
63
  def __init__(
64
64
  self,
65
65
  evaluators: Sequence[langsmith.RunEvaluator],
66
- client: Optional[langsmith.Client] = None,
67
- example_id: Optional[Union[UUID, str]] = None,
66
+ client: langsmith.Client | None = None,
67
+ example_id: UUID | str | None = None,
68
68
  skip_unfinished: bool = True, # noqa: FBT001,FBT002
69
- project_name: Optional[str] = "evaluators",
70
- max_concurrency: Optional[int] = None,
69
+ project_name: str | None = "evaluators",
70
+ max_concurrency: int | None = None,
71
71
  **kwargs: Any,
72
72
  ) -> None:
73
73
  """Create an EvaluatorCallbackHandler.
@@ -156,7 +156,7 @@ class EvaluatorCallbackHandler(BaseTracer):
156
156
 
157
157
  def _select_eval_results(
158
158
  self,
159
- results: Union[EvaluationResult, EvaluationResults],
159
+ results: EvaluationResult | EvaluationResults,
160
160
  ) -> list[EvaluationResult]:
161
161
  if isinstance(results, EvaluationResult):
162
162
  results_ = [results]
@@ -172,9 +172,9 @@ class EvaluatorCallbackHandler(BaseTracer):
172
172
 
173
173
  def _log_evaluation_feedback(
174
174
  self,
175
- evaluator_response: Union[EvaluationResult, EvaluationResults],
175
+ evaluator_response: EvaluationResult | EvaluationResults,
176
176
  run: Run,
177
- source_run_id: Optional[UUID] = None,
177
+ source_run_id: UUID | None = None,
178
178
  ) -> list[EvaluationResult]:
179
179
  results = self._select_eval_results(evaluator_response)
180
180
  for res in results:
@@ -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."""
@@ -639,11 +640,11 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
639
640
  input_str: str,
640
641
  *,
641
642
  run_id: UUID,
642
- tags: Optional[list[str]] = None,
643
- parent_run_id: Optional[UUID] = None,
644
- metadata: Optional[dict[str, Any]] = None,
645
- name: Optional[str] = None,
646
- 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,
647
648
  **kwargs: Any,
648
649
  ) -> None:
649
650
  """Start a trace for a tool run."""
@@ -680,8 +681,8 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
680
681
  error: BaseException,
681
682
  *,
682
683
  run_id: UUID,
683
- parent_run_id: Optional[UUID] = None,
684
- tags: Optional[list[str]] = None,
684
+ parent_run_id: UUID | None = None,
685
+ tags: list[str] | None = None,
685
686
  **kwargs: Any,
686
687
  ) -> None:
687
688
  """Run when tool errors."""
@@ -735,10 +736,10 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
735
736
  query: str,
736
737
  *,
737
738
  run_id: UUID,
738
- parent_run_id: Optional[UUID] = None,
739
- tags: Optional[list[str]] = None,
740
- metadata: Optional[dict[str, Any]] = None,
741
- 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,
742
743
  **kwargs: Any,
743
744
  ) -> None:
744
745
  """Run when Retriever starts running."""
@@ -807,14 +808,14 @@ class _AstreamEventsCallbackHandler(AsyncCallbackHandler, _StreamingCallbackHand
807
808
  async def _astream_events_implementation_v1(
808
809
  runnable: Runnable[Input, Output],
809
810
  value: Any,
810
- config: Optional[RunnableConfig] = None,
811
+ config: RunnableConfig | None = None,
811
812
  *,
812
- include_names: Optional[Sequence[str]] = None,
813
- include_types: Optional[Sequence[str]] = None,
814
- include_tags: Optional[Sequence[str]] = None,
815
- exclude_names: Optional[Sequence[str]] = None,
816
- exclude_types: Optional[Sequence[str]] = None,
817
- 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,
818
819
  **kwargs: Any,
819
820
  ) -> AsyncIterator[StandardStreamEvent]:
820
821
  stream = LogStreamCallbackHandler(
@@ -983,14 +984,14 @@ async def _astream_events_implementation_v1(
983
984
  async def _astream_events_implementation_v2(
984
985
  runnable: Runnable[Input, Output],
985
986
  value: Any,
986
- config: Optional[RunnableConfig] = None,
987
+ config: RunnableConfig | None = None,
987
988
  *,
988
- include_names: Optional[Sequence[str]] = None,
989
- include_types: Optional[Sequence[str]] = None,
990
- include_tags: Optional[Sequence[str]] = None,
991
- exclude_names: Optional[Sequence[str]] = None,
992
- exclude_types: Optional[Sequence[str]] = None,
993
- 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,
994
995
  **kwargs: Any,
995
996
  ) -> AsyncIterator[StandardStreamEvent]:
996
997
  """Implementation of the astream events API for V2 runnables."""