langchain-core 1.0.0a6__py3-none-any.whl → 1.0.0a8__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 (131) hide show
  1. langchain_core/_api/__init__.py +3 -3
  2. langchain_core/_api/beta_decorator.py +6 -6
  3. langchain_core/_api/deprecation.py +21 -29
  4. langchain_core/_api/path.py +3 -6
  5. langchain_core/_import_utils.py +2 -3
  6. langchain_core/agents.py +10 -11
  7. langchain_core/caches.py +7 -7
  8. langchain_core/callbacks/base.py +91 -91
  9. langchain_core/callbacks/file.py +11 -11
  10. langchain_core/callbacks/manager.py +86 -89
  11. langchain_core/callbacks/stdout.py +8 -8
  12. langchain_core/callbacks/usage.py +4 -4
  13. langchain_core/chat_history.py +5 -5
  14. langchain_core/document_loaders/base.py +2 -2
  15. langchain_core/document_loaders/langsmith.py +15 -15
  16. langchain_core/documents/base.py +16 -16
  17. langchain_core/documents/compressor.py +4 -4
  18. langchain_core/example_selectors/length_based.py +1 -1
  19. langchain_core/example_selectors/semantic_similarity.py +17 -19
  20. langchain_core/exceptions.py +3 -3
  21. langchain_core/globals.py +3 -151
  22. langchain_core/indexing/api.py +44 -43
  23. langchain_core/indexing/base.py +30 -30
  24. langchain_core/indexing/in_memory.py +3 -3
  25. langchain_core/language_models/_utils.py +5 -7
  26. langchain_core/language_models/base.py +18 -132
  27. langchain_core/language_models/chat_models.py +118 -227
  28. langchain_core/language_models/fake.py +11 -11
  29. langchain_core/language_models/fake_chat_models.py +35 -29
  30. langchain_core/language_models/llms.py +91 -201
  31. langchain_core/load/dump.py +1 -1
  32. langchain_core/load/load.py +11 -12
  33. langchain_core/load/mapping.py +2 -4
  34. langchain_core/load/serializable.py +2 -4
  35. langchain_core/messages/ai.py +17 -20
  36. langchain_core/messages/base.py +23 -25
  37. langchain_core/messages/block_translators/__init__.py +2 -5
  38. langchain_core/messages/block_translators/anthropic.py +3 -3
  39. langchain_core/messages/block_translators/bedrock_converse.py +2 -2
  40. langchain_core/messages/block_translators/langchain_v0.py +2 -2
  41. langchain_core/messages/block_translators/openai.py +6 -6
  42. langchain_core/messages/content.py +120 -124
  43. langchain_core/messages/human.py +7 -7
  44. langchain_core/messages/system.py +7 -7
  45. langchain_core/messages/tool.py +24 -24
  46. langchain_core/messages/utils.py +67 -79
  47. langchain_core/output_parsers/base.py +12 -14
  48. langchain_core/output_parsers/json.py +4 -4
  49. langchain_core/output_parsers/list.py +3 -5
  50. langchain_core/output_parsers/openai_functions.py +3 -3
  51. langchain_core/output_parsers/openai_tools.py +3 -3
  52. langchain_core/output_parsers/pydantic.py +2 -2
  53. langchain_core/output_parsers/transform.py +13 -15
  54. langchain_core/output_parsers/xml.py +7 -9
  55. langchain_core/outputs/chat_generation.py +4 -4
  56. langchain_core/outputs/chat_result.py +1 -3
  57. langchain_core/outputs/generation.py +2 -2
  58. langchain_core/outputs/llm_result.py +5 -5
  59. langchain_core/prompts/__init__.py +1 -5
  60. langchain_core/prompts/base.py +10 -15
  61. langchain_core/prompts/chat.py +31 -82
  62. langchain_core/prompts/dict.py +2 -2
  63. langchain_core/prompts/few_shot.py +5 -5
  64. langchain_core/prompts/few_shot_with_templates.py +4 -4
  65. langchain_core/prompts/loading.py +3 -5
  66. langchain_core/prompts/prompt.py +4 -16
  67. langchain_core/prompts/string.py +2 -1
  68. langchain_core/prompts/structured.py +16 -23
  69. langchain_core/rate_limiters.py +3 -4
  70. langchain_core/retrievers.py +14 -14
  71. langchain_core/runnables/base.py +928 -1042
  72. langchain_core/runnables/branch.py +36 -40
  73. langchain_core/runnables/config.py +27 -35
  74. langchain_core/runnables/configurable.py +108 -124
  75. langchain_core/runnables/fallbacks.py +76 -72
  76. langchain_core/runnables/graph.py +39 -45
  77. langchain_core/runnables/graph_ascii.py +9 -11
  78. langchain_core/runnables/graph_mermaid.py +18 -19
  79. langchain_core/runnables/graph_png.py +8 -9
  80. langchain_core/runnables/history.py +114 -127
  81. langchain_core/runnables/passthrough.py +113 -139
  82. langchain_core/runnables/retry.py +43 -48
  83. langchain_core/runnables/router.py +23 -28
  84. langchain_core/runnables/schema.py +42 -44
  85. langchain_core/runnables/utils.py +28 -31
  86. langchain_core/stores.py +9 -13
  87. langchain_core/structured_query.py +8 -8
  88. langchain_core/tools/base.py +62 -115
  89. langchain_core/tools/convert.py +31 -35
  90. langchain_core/tools/render.py +1 -1
  91. langchain_core/tools/retriever.py +4 -4
  92. langchain_core/tools/simple.py +13 -17
  93. langchain_core/tools/structured.py +12 -15
  94. langchain_core/tracers/base.py +62 -64
  95. langchain_core/tracers/context.py +17 -35
  96. langchain_core/tracers/core.py +49 -53
  97. langchain_core/tracers/evaluation.py +11 -11
  98. langchain_core/tracers/event_stream.py +58 -60
  99. langchain_core/tracers/langchain.py +13 -13
  100. langchain_core/tracers/log_stream.py +22 -24
  101. langchain_core/tracers/root_listeners.py +14 -14
  102. langchain_core/tracers/run_collector.py +2 -4
  103. langchain_core/tracers/schemas.py +8 -8
  104. langchain_core/tracers/stdout.py +2 -1
  105. langchain_core/utils/__init__.py +0 -3
  106. langchain_core/utils/_merge.py +2 -2
  107. langchain_core/utils/aiter.py +24 -28
  108. langchain_core/utils/env.py +4 -4
  109. langchain_core/utils/function_calling.py +31 -41
  110. langchain_core/utils/html.py +3 -4
  111. langchain_core/utils/input.py +3 -3
  112. langchain_core/utils/iter.py +15 -19
  113. langchain_core/utils/json.py +3 -2
  114. langchain_core/utils/json_schema.py +6 -6
  115. langchain_core/utils/mustache.py +3 -5
  116. langchain_core/utils/pydantic.py +16 -18
  117. langchain_core/utils/usage.py +1 -1
  118. langchain_core/utils/utils.py +29 -29
  119. langchain_core/vectorstores/base.py +18 -21
  120. langchain_core/vectorstores/in_memory.py +14 -87
  121. langchain_core/vectorstores/utils.py +2 -2
  122. langchain_core/version.py +1 -1
  123. {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.0a8.dist-info}/METADATA +10 -21
  124. langchain_core-1.0.0a8.dist-info/RECORD +176 -0
  125. {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.0a8.dist-info}/WHEEL +1 -1
  126. langchain_core/messages/block_translators/ollama.py +0 -47
  127. langchain_core/prompts/pipeline.py +0 -138
  128. langchain_core/tracers/langchain_v1.py +0 -31
  129. langchain_core/utils/loading.py +0 -35
  130. langchain_core-1.0.0a6.dist-info/RECORD +0 -181
  131. langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
@@ -2,13 +2,10 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from collections.abc import Mapping
5
+ from collections.abc import Callable, Mapping
6
6
  from typing import (
7
7
  TYPE_CHECKING,
8
8
  Any,
9
- Callable,
10
- Optional,
11
- Union,
12
9
  cast,
13
10
  )
14
11
 
@@ -52,18 +49,16 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
52
49
  Returns the output of the selected Runnable.
53
50
 
54
51
  Example:
52
+ ```python
53
+ from langchain_core.runnables.router import RouterRunnable
54
+ from langchain_core.runnables import RunnableLambda
55
55
 
56
- .. code-block:: python
57
-
58
- from langchain_core.runnables.router import RouterRunnable
59
- from langchain_core.runnables import RunnableLambda
60
-
61
- add = RunnableLambda(func=lambda x: x + 1)
62
- square = RunnableLambda(func=lambda x: x**2)
63
-
64
- router = RouterRunnable(runnables={"add": add, "square": square})
65
- router.invoke({"key": "square", "input": 3})
56
+ add = RunnableLambda(func=lambda x: x + 1)
57
+ square = RunnableLambda(func=lambda x: x**2)
66
58
 
59
+ router = RouterRunnable(runnables={"add": add, "square": square})
60
+ router.invoke({"key": "square", "input": 3})
61
+ ```
67
62
  """
68
63
 
69
64
  runnables: Mapping[str, Runnable[Any, Output]]
@@ -77,7 +72,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
77
72
 
78
73
  def __init__(
79
74
  self,
80
- runnables: Mapping[str, Union[Runnable[Any, Output], Callable[[Any], Output]]],
75
+ runnables: Mapping[str, Runnable[Any, Output] | Callable[[Any], Output]],
81
76
  ) -> None:
82
77
  """Create a RouterRunnable.
83
78
 
@@ -110,7 +105,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
110
105
 
111
106
  @override
112
107
  def invoke(
113
- self, input: RouterInput, config: Optional[RunnableConfig] = None, **kwargs: Any
108
+ self, input: RouterInput, config: RunnableConfig | None = None, **kwargs: Any
114
109
  ) -> Output:
115
110
  key = input["key"]
116
111
  actual_input = input["input"]
@@ -125,8 +120,8 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
125
120
  async def ainvoke(
126
121
  self,
127
122
  input: RouterInput,
128
- config: Optional[RunnableConfig] = None,
129
- **kwargs: Optional[Any],
123
+ config: RunnableConfig | None = None,
124
+ **kwargs: Any | None,
130
125
  ) -> Output:
131
126
  key = input["key"]
132
127
  actual_input = input["input"]
@@ -141,10 +136,10 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
141
136
  def batch(
142
137
  self,
143
138
  inputs: list[RouterInput],
144
- config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
139
+ config: RunnableConfig | list[RunnableConfig] | None = None,
145
140
  *,
146
141
  return_exceptions: bool = False,
147
- **kwargs: Optional[Any],
142
+ **kwargs: Any | None,
148
143
  ) -> list[Output]:
149
144
  if not inputs:
150
145
  return []
@@ -157,7 +152,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
157
152
 
158
153
  def invoke(
159
154
  runnable: Runnable, input_: Input, config: RunnableConfig
160
- ) -> Union[Output, Exception]:
155
+ ) -> Output | Exception:
161
156
  if return_exceptions:
162
157
  try:
163
158
  return runnable.invoke(input_, config, **kwargs)
@@ -178,10 +173,10 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
178
173
  async def abatch(
179
174
  self,
180
175
  inputs: list[RouterInput],
181
- config: Optional[Union[RunnableConfig, list[RunnableConfig]]] = None,
176
+ config: RunnableConfig | list[RunnableConfig] | None = None,
182
177
  *,
183
178
  return_exceptions: bool = False,
184
- **kwargs: Optional[Any],
179
+ **kwargs: Any | None,
185
180
  ) -> list[Output]:
186
181
  if not inputs:
187
182
  return []
@@ -194,7 +189,7 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
194
189
 
195
190
  async def ainvoke(
196
191
  runnable: Runnable, input_: Input, config: RunnableConfig
197
- ) -> Union[Output, Exception]:
192
+ ) -> Output | Exception:
198
193
  if return_exceptions:
199
194
  try:
200
195
  return await runnable.ainvoke(input_, config, **kwargs)
@@ -214,8 +209,8 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
214
209
  def stream(
215
210
  self,
216
211
  input: RouterInput,
217
- config: Optional[RunnableConfig] = None,
218
- **kwargs: Optional[Any],
212
+ config: RunnableConfig | None = None,
213
+ **kwargs: Any | None,
219
214
  ) -> Iterator[Output]:
220
215
  key = input["key"]
221
216
  actual_input = input["input"]
@@ -230,8 +225,8 @@ class RouterRunnable(RunnableSerializable[RouterInput, Output]):
230
225
  async def astream(
231
226
  self,
232
227
  input: RouterInput,
233
- config: Optional[RunnableConfig] = None,
234
- **kwargs: Optional[Any],
228
+ config: RunnableConfig | None = None,
229
+ **kwargs: Any | None,
235
230
  ) -> AsyncIterator[Output]:
236
231
  key = input["key"]
237
232
  actual_input = input["input"]
@@ -2,7 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- from typing import TYPE_CHECKING, Any, Literal, Union
5
+ from typing import TYPE_CHECKING, Any, Literal
6
6
 
7
7
  from typing_extensions import NotRequired, TypedDict
8
8
 
@@ -28,7 +28,7 @@ class EventData(TypedDict, total=False):
28
28
 
29
29
  This field is only available if the Runnable raised an exception.
30
30
 
31
- .. versionadded:: 1.0.0
31
+ !!! version-added "Added in version 1.0.0"
32
32
  """
33
33
  output: Any
34
34
  """The output of the Runnable that generated the event.
@@ -53,46 +53,44 @@ class BaseStreamEvent(TypedDict):
53
53
  Schema of a streaming event which is produced from the astream_events method.
54
54
 
55
55
  Example:
56
-
57
- .. code-block:: python
58
-
59
- from langchain_core.runnables import RunnableLambda
60
-
61
-
62
- async def reverse(s: str) -> str:
63
- return s[::-1]
64
-
65
-
66
- chain = RunnableLambda(func=reverse)
67
-
68
- events = [event async for event in chain.astream_events("hello")]
69
-
70
- # will produce the following events
71
- # (where some fields have been omitted for brevity):
72
- [
73
- {
74
- "data": {"input": "hello"},
75
- "event": "on_chain_start",
76
- "metadata": {},
77
- "name": "reverse",
78
- "tags": [],
79
- },
80
- {
81
- "data": {"chunk": "olleh"},
82
- "event": "on_chain_stream",
83
- "metadata": {},
84
- "name": "reverse",
85
- "tags": [],
86
- },
87
- {
88
- "data": {"output": "olleh"},
89
- "event": "on_chain_end",
90
- "metadata": {},
91
- "name": "reverse",
92
- "tags": [],
93
- },
94
- ]
95
-
56
+ ```python
57
+ from langchain_core.runnables import RunnableLambda
58
+
59
+
60
+ async def reverse(s: str) -> str:
61
+ return s[::-1]
62
+
63
+
64
+ chain = RunnableLambda(func=reverse)
65
+
66
+ events = [event async for event in chain.astream_events("hello")]
67
+
68
+ # will produce the following events
69
+ # (where some fields have been omitted for brevity):
70
+ [
71
+ {
72
+ "data": {"input": "hello"},
73
+ "event": "on_chain_start",
74
+ "metadata": {},
75
+ "name": "reverse",
76
+ "tags": [],
77
+ },
78
+ {
79
+ "data": {"chunk": "olleh"},
80
+ "event": "on_chain_stream",
81
+ "metadata": {},
82
+ "name": "reverse",
83
+ "tags": [],
84
+ },
85
+ {
86
+ "data": {"output": "olleh"},
87
+ "event": "on_chain_end",
88
+ "metadata": {},
89
+ "name": "reverse",
90
+ "tags": [],
91
+ },
92
+ ]
93
+ ```
96
94
  """
97
95
 
98
96
  event: str
@@ -172,7 +170,7 @@ class StandardStreamEvent(BaseStreamEvent):
172
170
  class CustomStreamEvent(BaseStreamEvent):
173
171
  """Custom stream event created by the user.
174
172
 
175
- .. versionadded:: 0.2.15
173
+ !!! version-added "Added in version 0.2.15"
176
174
  """
177
175
 
178
176
  # Overwrite the event field to be more specific.
@@ -184,4 +182,4 @@ class CustomStreamEvent(BaseStreamEvent):
184
182
  """The data associated with the event. Free form and can be anything."""
185
183
 
186
184
 
187
- StreamEvent = Union[StandardStreamEvent, CustomStreamEvent]
185
+ StreamEvent = StandardStreamEvent | CustomStreamEvent
@@ -6,7 +6,7 @@ import ast
6
6
  import asyncio
7
7
  import inspect
8
8
  import textwrap
9
- from collections.abc import Mapping, Sequence
9
+ from collections.abc import Callable, Mapping, Sequence
10
10
  from contextvars import Context
11
11
  from functools import lru_cache
12
12
  from inspect import signature
@@ -14,13 +14,10 @@ from itertools import groupby
14
14
  from typing import (
15
15
  TYPE_CHECKING,
16
16
  Any,
17
- Callable,
18
17
  NamedTuple,
19
- Optional,
20
18
  Protocol,
21
19
  TypeGuard,
22
20
  TypeVar,
23
- Union,
24
21
  )
25
22
 
26
23
  from typing_extensions import override
@@ -58,7 +55,7 @@ async def gated_coro(semaphore: asyncio.Semaphore, coro: Coroutine) -> Any:
58
55
  return await coro
59
56
 
60
57
 
61
- async def gather_with_concurrency(n: Union[int, None], *coros: Coroutine) -> list:
58
+ async def gather_with_concurrency(n: int | None, *coros: Coroutine) -> list:
62
59
  """Gather coroutines with a limit on the number of concurrent coroutines.
63
60
 
64
61
  Args:
@@ -344,7 +341,7 @@ class GetLambdaSource(ast.NodeVisitor):
344
341
 
345
342
  def __init__(self) -> None:
346
343
  """Initialize the visitor."""
347
- self.source: Optional[str] = None
344
+ self.source: str | None = None
348
345
  self.count = 0
349
346
 
350
347
  @override
@@ -359,7 +356,7 @@ class GetLambdaSource(ast.NodeVisitor):
359
356
  self.source = ast.unparse(node)
360
357
 
361
358
 
362
- def get_function_first_arg_dict_keys(func: Callable) -> Optional[list[str]]:
359
+ def get_function_first_arg_dict_keys(func: Callable) -> list[str] | None:
363
360
  """Get the keys of the first argument of a function if it is a dict.
364
361
 
365
362
  Args:
@@ -379,7 +376,7 @@ def get_function_first_arg_dict_keys(func: Callable) -> Optional[list[str]]:
379
376
  return None
380
377
 
381
378
 
382
- def get_lambda_source(func: Callable) -> Optional[str]:
379
+ def get_lambda_source(func: Callable) -> str | None:
383
380
  """Get the source code of a lambda function.
384
381
 
385
382
  Args:
@@ -521,7 +518,7 @@ class SupportsAdd(Protocol[_T_contra, _T_co]):
521
518
  Addable = TypeVar("Addable", bound=SupportsAdd[Any, Any])
522
519
 
523
520
 
524
- def add(addables: Iterable[Addable]) -> Optional[Addable]:
521
+ def add(addables: Iterable[Addable]) -> Addable | None:
525
522
  """Add a sequence of addable objects together.
526
523
 
527
524
  Args:
@@ -530,13 +527,13 @@ def add(addables: Iterable[Addable]) -> Optional[Addable]:
530
527
  Returns:
531
528
  Optional[Addable]: The result of adding the addable objects.
532
529
  """
533
- final: Optional[Addable] = None
530
+ final: Addable | None = None
534
531
  for chunk in addables:
535
532
  final = chunk if final is None else final + chunk
536
533
  return final
537
534
 
538
535
 
539
- async def aadd(addables: AsyncIterable[Addable]) -> Optional[Addable]:
536
+ async def aadd(addables: AsyncIterable[Addable]) -> Addable | None:
540
537
  """Asynchronously add a sequence of addable objects together.
541
538
 
542
539
  Args:
@@ -545,7 +542,7 @@ async def aadd(addables: AsyncIterable[Addable]) -> Optional[Addable]:
545
542
  Returns:
546
543
  Optional[Addable]: The result of adding the addable objects.
547
544
  """
548
- final: Optional[Addable] = None
545
+ final: Addable | None = None
549
546
  async for chunk in addables:
550
547
  final = chunk if final is None else final + chunk
551
548
  return final
@@ -556,11 +553,11 @@ class ConfigurableField(NamedTuple):
556
553
 
557
554
  id: str
558
555
  """The unique identifier of the field."""
559
- name: Optional[str] = None
556
+ name: str | None = None
560
557
  """The name of the field. Defaults to None."""
561
- description: Optional[str] = None
558
+ description: str | None = None
562
559
  """The description of the field. Defaults to None."""
563
- annotation: Optional[Any] = None
560
+ annotation: Any | None = None
564
561
  """The annotation of the field. Defaults to None."""
565
562
  is_shared: bool = False
566
563
  """Whether the field is shared. Defaults to False."""
@@ -579,9 +576,9 @@ class ConfigurableFieldSingleOption(NamedTuple):
579
576
  """The options for the field."""
580
577
  default: str
581
578
  """The default value for the field."""
582
- name: Optional[str] = None
579
+ name: str | None = None
583
580
  """The name of the field. Defaults to None."""
584
- description: Optional[str] = None
581
+ description: str | None = None
585
582
  """The description of the field. Defaults to None."""
586
583
  is_shared: bool = False
587
584
  """Whether the field is shared. Defaults to False."""
@@ -600,9 +597,9 @@ class ConfigurableFieldMultiOption(NamedTuple):
600
597
  """The options for the field."""
601
598
  default: Sequence[str]
602
599
  """The default values for the field."""
603
- name: Optional[str] = None
600
+ name: str | None = None
604
601
  """The name of the field. Defaults to None."""
605
- description: Optional[str] = None
602
+ description: str | None = None
606
603
  """The description of the field. Defaults to None."""
607
604
  is_shared: bool = False
608
605
  """Whether the field is shared. Defaults to False."""
@@ -612,9 +609,9 @@ class ConfigurableFieldMultiOption(NamedTuple):
612
609
  return hash((self.id, tuple(self.options.keys()), tuple(self.default)))
613
610
 
614
611
 
615
- AnyConfigurableField = Union[
616
- ConfigurableField, ConfigurableFieldSingleOption, ConfigurableFieldMultiOption
617
- ]
612
+ AnyConfigurableField = (
613
+ ConfigurableField | ConfigurableFieldSingleOption | ConfigurableFieldMultiOption
614
+ )
618
615
 
619
616
 
620
617
  class ConfigurableFieldSpec(NamedTuple):
@@ -624,15 +621,15 @@ class ConfigurableFieldSpec(NamedTuple):
624
621
  """The unique identifier of the field."""
625
622
  annotation: Any
626
623
  """The annotation of the field."""
627
- name: Optional[str] = None
624
+ name: str | None = None
628
625
  """The name of the field. Defaults to None."""
629
- description: Optional[str] = None
626
+ description: str | None = None
630
627
  """The description of the field. Defaults to None."""
631
628
  default: Any = None
632
629
  """The default value for the field. Defaults to None."""
633
630
  is_shared: bool = False
634
631
  """Whether the field is shared. Defaults to False."""
635
- dependencies: Optional[list[str]] = None
632
+ dependencies: list[str] | None = None
636
633
  """The dependencies of the field. Defaults to None."""
637
634
 
638
635
 
@@ -672,12 +669,12 @@ class _RootEventFilter:
672
669
  def __init__(
673
670
  self,
674
671
  *,
675
- include_names: Optional[Sequence[str]] = None,
676
- include_types: Optional[Sequence[str]] = None,
677
- include_tags: Optional[Sequence[str]] = None,
678
- exclude_names: Optional[Sequence[str]] = None,
679
- exclude_types: Optional[Sequence[str]] = None,
680
- exclude_tags: Optional[Sequence[str]] = None,
672
+ include_names: Sequence[str] | None = None,
673
+ include_types: Sequence[str] | None = None,
674
+ include_tags: Sequence[str] | None = None,
675
+ exclude_names: Sequence[str] | None = None,
676
+ exclude_types: Sequence[str] | None = None,
677
+ exclude_tags: Sequence[str] | None = None,
681
678
  ) -> None:
682
679
  """Utility to filter the root event in the astream_events implementation.
683
680
 
langchain_core/stores.py CHANGED
@@ -11,9 +11,7 @@ from collections.abc import AsyncIterator, Iterator, Sequence
11
11
  from typing import (
12
12
  Any,
13
13
  Generic,
14
- Optional,
15
14
  TypeVar,
16
- Union,
17
15
  )
18
16
 
19
17
  from typing_extensions import override
@@ -82,7 +80,7 @@ class BaseStore(ABC, Generic[K, V]):
82
80
  """
83
81
 
84
82
  @abstractmethod
85
- def mget(self, keys: Sequence[K]) -> list[Optional[V]]:
83
+ def mget(self, keys: Sequence[K]) -> list[V | None]:
86
84
  """Get the values associated with the given keys.
87
85
 
88
86
  Args:
@@ -93,7 +91,7 @@ class BaseStore(ABC, Generic[K, V]):
93
91
  If a key is not found, the corresponding value will be None.
94
92
  """
95
93
 
96
- async def amget(self, keys: Sequence[K]) -> list[Optional[V]]:
94
+ async def amget(self, keys: Sequence[K]) -> list[V | None]:
97
95
  """Async get the values associated with the given keys.
98
96
 
99
97
  Args:
@@ -138,9 +136,7 @@ class BaseStore(ABC, Generic[K, V]):
138
136
  return await run_in_executor(None, self.mdelete, keys)
139
137
 
140
138
  @abstractmethod
141
- def yield_keys(
142
- self, *, prefix: Optional[str] = None
143
- ) -> Union[Iterator[K], Iterator[str]]:
139
+ def yield_keys(self, *, prefix: str | None = None) -> Iterator[K] | Iterator[str]:
144
140
  """Get an iterator over keys that match the given prefix.
145
141
 
146
142
  Args:
@@ -153,8 +149,8 @@ class BaseStore(ABC, Generic[K, V]):
153
149
  """
154
150
 
155
151
  async def ayield_keys(
156
- self, *, prefix: Optional[str] = None
157
- ) -> Union[AsyncIterator[K], AsyncIterator[str]]:
152
+ self, *, prefix: str | None = None
153
+ ) -> AsyncIterator[K] | AsyncIterator[str]:
158
154
  """Async get an iterator over keys that match the given prefix.
159
155
 
160
156
  Args:
@@ -184,7 +180,7 @@ class InMemoryBaseStore(BaseStore[str, V], Generic[V]):
184
180
  """Initialize an empty store."""
185
181
  self.store: dict[str, V] = {}
186
182
 
187
- def mget(self, keys: Sequence[str]) -> list[Optional[V]]:
183
+ def mget(self, keys: Sequence[str]) -> list[V | None]:
188
184
  """Get the values associated with the given keys.
189
185
 
190
186
  Args:
@@ -196,7 +192,7 @@ class InMemoryBaseStore(BaseStore[str, V], Generic[V]):
196
192
  """
197
193
  return [self.store.get(key) for key in keys]
198
194
 
199
- async def amget(self, keys: Sequence[str]) -> list[Optional[V]]:
195
+ async def amget(self, keys: Sequence[str]) -> list[V | None]:
200
196
  """Async get the values associated with the given keys.
201
197
 
202
198
  Args:
@@ -235,7 +231,7 @@ class InMemoryBaseStore(BaseStore[str, V], Generic[V]):
235
231
  """
236
232
  self.mdelete(keys)
237
233
 
238
- def yield_keys(self, prefix: Optional[str] = None) -> Iterator[str]:
234
+ def yield_keys(self, prefix: str | None = None) -> Iterator[str]:
239
235
  """Get an iterator over keys that match the given prefix.
240
236
 
241
237
  Args:
@@ -251,7 +247,7 @@ class InMemoryBaseStore(BaseStore[str, V], Generic[V]):
251
247
  if key.startswith(prefix):
252
248
  yield key
253
249
 
254
- async def ayield_keys(self, prefix: Optional[str] = None) -> AsyncIterator[str]:
250
+ async def ayield_keys(self, prefix: str | None = None) -> AsyncIterator[str]:
255
251
  """Async get an async iterator over keys that match the given prefix.
256
252
 
257
253
  Args:
@@ -4,7 +4,7 @@ from __future__ import annotations
4
4
 
5
5
  from abc import ABC, abstractmethod
6
6
  from enum import Enum
7
- from typing import TYPE_CHECKING, Any, Optional, Union
7
+ from typing import TYPE_CHECKING, Any
8
8
 
9
9
  from pydantic import BaseModel
10
10
 
@@ -15,12 +15,12 @@ if TYPE_CHECKING:
15
15
  class Visitor(ABC):
16
16
  """Defines interface for IR translation using a visitor pattern."""
17
17
 
18
- allowed_comparators: Optional[Sequence[Comparator]] = None
18
+ allowed_comparators: Sequence[Comparator] | None = None
19
19
  """Allowed comparators for the visitor."""
20
- allowed_operators: Optional[Sequence[Operator]] = None
20
+ allowed_operators: Sequence[Operator] | None = None
21
21
  """Allowed operators for the visitor."""
22
22
 
23
- def _validate_func(self, func: Union[Operator, Comparator]) -> None:
23
+ def _validate_func(self, func: Operator | Comparator) -> None:
24
24
  if (
25
25
  isinstance(func, Operator)
26
26
  and self.allowed_operators is not None
@@ -174,16 +174,16 @@ class StructuredQuery(Expr):
174
174
 
175
175
  query: str
176
176
  """Query string."""
177
- filter: Optional[FilterDirective]
177
+ filter: FilterDirective | None
178
178
  """Filtering expression."""
179
- limit: Optional[int]
179
+ limit: int | None
180
180
  """Limit on the number of results."""
181
181
 
182
182
  def __init__(
183
183
  self,
184
184
  query: str,
185
- filter: Optional[FilterDirective], # noqa: A002
186
- limit: Optional[int] = None,
185
+ filter: FilterDirective | None, # noqa: A002
186
+ limit: int | None = None,
187
187
  **kwargs: Any,
188
188
  ) -> None:
189
189
  """Create a StructuredQuery.