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,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,
@@ -3,7 +3,6 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import logging
6
- import sys
7
6
  import traceback
8
7
  from abc import ABC, abstractmethod
9
8
  from datetime import datetime, timezone
@@ -11,8 +10,6 @@ from typing import (
11
10
  TYPE_CHECKING,
12
11
  Any,
13
12
  Literal,
14
- Optional,
15
- Union,
16
13
  cast,
17
14
  )
18
15
 
@@ -82,7 +79,7 @@ class _TracerCore(ABC):
82
79
  """Map of run ID to (trace_id, dotted_order). Cleared when tracer GCed."""
83
80
 
84
81
  @abstractmethod
85
- def _persist_run(self, run: Run) -> Union[Coroutine[Any, Any, None], None]:
82
+ def _persist_run(self, run: Run) -> Coroutine[Any, Any, None] | None:
86
83
  """Persist a run."""
87
84
 
88
85
  @staticmethod
@@ -98,17 +95,12 @@ class _TracerCore(ABC):
98
95
  """Get the stacktrace of the parent error."""
99
96
  msg = repr(error)
100
97
  try:
101
- if sys.version_info < (3, 10):
102
- tb = traceback.format_exception(
103
- error.__class__, error, error.__traceback__
104
- )
105
- else:
106
- tb = traceback.format_exception(error)
98
+ tb = traceback.format_exception(error)
107
99
  return (msg + "\n\n".join(tb)).strip()
108
100
  except: # noqa: E722
109
101
  return msg
110
102
 
111
- 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]
112
104
  current_dotted_order = run.start_time.strftime("%Y%m%dT%H%M%S%fZ") + str(run.id)
113
105
  if run.parent_run_id:
114
106
  if parent := self.order_map.get(run.parent_run_id):
@@ -132,9 +124,7 @@ class _TracerCore(ABC):
132
124
  self.order_map[run.id] = (run.trace_id, run.dotted_order)
133
125
  self.run_map[str(run.id)] = run
134
126
 
135
- def _get_run(
136
- self, run_id: UUID, run_type: Union[str, set[str], None] = None
137
- ) -> Run:
127
+ def _get_run(self, run_id: UUID, run_type: str | set[str] | None = None) -> Run:
138
128
  try:
139
129
  run = self.run_map[str(run_id)]
140
130
  except KeyError as exc:
@@ -142,7 +132,7 @@ class _TracerCore(ABC):
142
132
  raise TracerException(msg) from exc
143
133
 
144
134
  if isinstance(run_type, str):
145
- run_types: Union[set[str], None] = {run_type}
135
+ run_types: set[str] | None = {run_type}
146
136
  else:
147
137
  run_types = run_type
148
138
  if run_types is not None and run.run_type not in run_types:
@@ -158,10 +148,10 @@ class _TracerCore(ABC):
158
148
  serialized: dict[str, Any],
159
149
  messages: list[list[BaseMessage]],
160
150
  run_id: UUID,
161
- tags: Optional[list[str]] = None,
162
- parent_run_id: Optional[UUID] = None,
163
- metadata: Optional[dict[str, Any]] = None,
164
- 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,
165
155
  **kwargs: Any,
166
156
  ) -> Run:
167
157
  """Create a chat model run."""
@@ -202,10 +192,10 @@ class _TracerCore(ABC):
202
192
  serialized: dict[str, Any],
203
193
  prompts: list[str],
204
194
  run_id: UUID,
205
- tags: Optional[list[str]] = None,
206
- parent_run_id: Optional[UUID] = None,
207
- metadata: Optional[dict[str, Any]] = None,
208
- 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,
209
199
  **kwargs: Any,
210
200
  ) -> Run:
211
201
  """Create a llm run."""
@@ -230,8 +220,8 @@ class _TracerCore(ABC):
230
220
  self,
231
221
  token: str,
232
222
  run_id: UUID,
233
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]] = None,
234
- parent_run_id: Optional[UUID] = None, # noqa: ARG002
223
+ chunk: GenerationChunk | ChatGenerationChunk | None = None,
224
+ parent_run_id: UUID | None = None, # noqa: ARG002
235
225
  ) -> Run:
236
226
  """Append token event to LLM run and return the run."""
237
227
  llm_run = self._get_run(run_id, run_type={"llm", "chat_model"})
@@ -297,7 +287,7 @@ class _TracerCore(ABC):
297
287
  return llm_run
298
288
 
299
289
  def _errored_llm_run(
300
- self, error: BaseException, run_id: UUID, response: Optional[LLMResult] = None
290
+ self, error: BaseException, run_id: UUID, response: LLMResult | None = None
301
291
  ) -> Run:
302
292
  llm_run = self._get_run(run_id, run_type={"llm", "chat_model"})
303
293
  llm_run.error = self._get_stacktrace(error)
@@ -325,11 +315,11 @@ class _TracerCore(ABC):
325
315
  serialized: dict[str, Any],
326
316
  inputs: dict[str, Any],
327
317
  run_id: UUID,
328
- tags: Optional[list[str]] = None,
329
- parent_run_id: Optional[UUID] = None,
330
- metadata: Optional[dict[str, Any]] = None,
331
- run_type: Optional[str] = None,
332
- 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,
333
323
  **kwargs: Any,
334
324
  ) -> Run:
335
325
  """Create a chain Run."""
@@ -376,7 +366,7 @@ class _TracerCore(ABC):
376
366
  self,
377
367
  outputs: dict[str, Any],
378
368
  run_id: UUID,
379
- inputs: Optional[dict[str, Any]] = None,
369
+ inputs: dict[str, Any] | None = None,
380
370
  ) -> Run:
381
371
  """Update a chain run with outputs and end time."""
382
372
  chain_run = self._get_run(run_id)
@@ -395,7 +385,7 @@ class _TracerCore(ABC):
395
385
  def _errored_chain_run(
396
386
  self,
397
387
  error: BaseException,
398
- inputs: Optional[dict[str, Any]],
388
+ inputs: dict[str, Any] | None,
399
389
  run_id: UUID,
400
390
  ) -> Run:
401
391
  chain_run = self._get_run(run_id)
@@ -411,11 +401,11 @@ class _TracerCore(ABC):
411
401
  serialized: dict[str, Any],
412
402
  input_str: str,
413
403
  run_id: UUID,
414
- tags: Optional[list[str]] = None,
415
- parent_run_id: Optional[UUID] = None,
416
- metadata: Optional[dict[str, Any]] = None,
417
- name: Optional[str] = None,
418
- 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,
419
409
  **kwargs: Any,
420
410
  ) -> Run:
421
411
  """Create a tool run."""
@@ -478,10 +468,10 @@ class _TracerCore(ABC):
478
468
  serialized: dict[str, Any],
479
469
  query: str,
480
470
  run_id: UUID,
481
- parent_run_id: Optional[UUID] = None,
482
- tags: Optional[list[str]] = None,
483
- metadata: Optional[dict[str, Any]] = None,
484
- 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,
485
475
  **kwargs: Any,
486
476
  ) -> Run:
487
477
  """Create a retrieval run."""
@@ -538,7 +528,7 @@ class _TracerCore(ABC):
538
528
  """Return self copied."""
539
529
  return self
540
530
 
541
- 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
542
532
  """End a trace for a run.
543
533
 
544
534
  Args:
@@ -546,7 +536,7 @@ class _TracerCore(ABC):
546
536
  """
547
537
  return None
548
538
 
549
- 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
550
540
  """Process a run upon creation.
551
541
 
552
542
  Args:
@@ -554,7 +544,7 @@ class _TracerCore(ABC):
554
544
  """
555
545
  return None
556
546
 
557
- 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
558
548
  """Process a run upon update.
559
549
 
560
550
  Args:
@@ -562,7 +552,7 @@ class _TracerCore(ABC):
562
552
  """
563
553
  return None
564
554
 
565
- 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
566
556
  """Process the LLM Run upon start.
567
557
 
568
558
  Args:
@@ -574,8 +564,8 @@ class _TracerCore(ABC):
574
564
  self,
575
565
  run: Run, # noqa: ARG002
576
566
  token: str, # noqa: ARG002
577
- chunk: Optional[Union[GenerationChunk, ChatGenerationChunk]], # noqa: ARG002
578
- ) -> Union[Coroutine[Any, Any, None], None]:
567
+ chunk: GenerationChunk | ChatGenerationChunk | None, # noqa: ARG002
568
+ ) -> Coroutine[Any, Any, None] | None:
579
569
  """Process new LLM token.
580
570
 
581
571
  Args:
@@ -585,7 +575,7 @@ class _TracerCore(ABC):
585
575
  """
586
576
  return None
587
577
 
588
- 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
589
579
  """Process the LLM Run.
590
580
 
591
581
  Args:
@@ -593,7 +583,7 @@ class _TracerCore(ABC):
593
583
  """
594
584
  return None
595
585
 
596
- 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
597
587
  """Process the LLM Run upon error.
598
588
 
599
589
  Args:
@@ -601,7 +591,7 @@ class _TracerCore(ABC):
601
591
  """
602
592
  return None
603
593
 
604
- 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
605
595
  """Process the Chain Run upon start.
606
596
 
607
597
  Args:
@@ -609,7 +599,7 @@ class _TracerCore(ABC):
609
599
  """
610
600
  return None
611
601
 
612
- 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
613
603
  """Process the Chain Run.
614
604
 
615
605
  Args:
@@ -617,7 +607,7 @@ class _TracerCore(ABC):
617
607
  """
618
608
  return None
619
609
 
620
- 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
621
611
  """Process the Chain Run upon error.
622
612
 
623
613
  Args:
@@ -625,7 +615,7 @@ class _TracerCore(ABC):
625
615
  """
626
616
  return None
627
617
 
628
- 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
629
619
  """Process the Tool Run upon start.
630
620
 
631
621
  Args:
@@ -633,7 +623,7 @@ class _TracerCore(ABC):
633
623
  """
634
624
  return None
635
625
 
636
- 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
637
627
  """Process the Tool Run.
638
628
 
639
629
  Args:
@@ -641,7 +631,7 @@ class _TracerCore(ABC):
641
631
  """
642
632
  return None
643
633
 
644
- 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
645
635
  """Process the Tool Run upon error.
646
636
 
647
637
  Args:
@@ -649,7 +639,7 @@ class _TracerCore(ABC):
649
639
  """
650
640
  return None
651
641
 
652
- 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
653
643
  """Process the Chat Model Run upon start.
654
644
 
655
645
  Args:
@@ -657,7 +647,7 @@ class _TracerCore(ABC):
657
647
  """
658
648
  return None
659
649
 
660
- 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
661
651
  """Process the Retriever Run upon start.
662
652
 
663
653
  Args:
@@ -665,7 +655,7 @@ class _TracerCore(ABC):
665
655
  """
666
656
  return None
667
657
 
668
- 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
669
659
  """Process the Retriever Run.
670
660
 
671
661
  Args:
@@ -673,7 +663,7 @@ class _TracerCore(ABC):
673
663
  """
674
664
  return None
675
665
 
676
- 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
677
667
  """Process the Retriever Run upon error.
678
668
 
679
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: