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.
- langchain_core/_api/__init__.py +3 -3
- langchain_core/_api/beta_decorator.py +6 -6
- langchain_core/_api/deprecation.py +21 -29
- langchain_core/_api/path.py +3 -6
- langchain_core/_import_utils.py +2 -3
- langchain_core/agents.py +10 -11
- langchain_core/caches.py +7 -7
- langchain_core/callbacks/base.py +91 -91
- langchain_core/callbacks/file.py +11 -11
- langchain_core/callbacks/manager.py +86 -89
- langchain_core/callbacks/stdout.py +8 -8
- langchain_core/callbacks/usage.py +4 -4
- langchain_core/chat_history.py +5 -5
- langchain_core/document_loaders/base.py +2 -2
- langchain_core/document_loaders/langsmith.py +15 -15
- langchain_core/documents/base.py +16 -16
- langchain_core/documents/compressor.py +4 -4
- langchain_core/example_selectors/length_based.py +1 -1
- langchain_core/example_selectors/semantic_similarity.py +17 -19
- langchain_core/exceptions.py +3 -3
- langchain_core/globals.py +3 -151
- langchain_core/indexing/api.py +44 -43
- langchain_core/indexing/base.py +30 -30
- langchain_core/indexing/in_memory.py +3 -3
- langchain_core/language_models/_utils.py +5 -7
- langchain_core/language_models/base.py +18 -132
- langchain_core/language_models/chat_models.py +118 -227
- langchain_core/language_models/fake.py +11 -11
- langchain_core/language_models/fake_chat_models.py +35 -29
- langchain_core/language_models/llms.py +91 -201
- langchain_core/load/dump.py +1 -1
- langchain_core/load/load.py +11 -12
- langchain_core/load/mapping.py +2 -4
- langchain_core/load/serializable.py +2 -4
- langchain_core/messages/ai.py +17 -20
- langchain_core/messages/base.py +23 -25
- langchain_core/messages/block_translators/__init__.py +2 -5
- langchain_core/messages/block_translators/anthropic.py +3 -3
- langchain_core/messages/block_translators/bedrock_converse.py +2 -2
- langchain_core/messages/block_translators/langchain_v0.py +2 -2
- langchain_core/messages/block_translators/openai.py +6 -6
- langchain_core/messages/content.py +120 -124
- langchain_core/messages/human.py +7 -7
- langchain_core/messages/system.py +7 -7
- langchain_core/messages/tool.py +24 -24
- langchain_core/messages/utils.py +67 -79
- langchain_core/output_parsers/base.py +12 -14
- langchain_core/output_parsers/json.py +4 -4
- langchain_core/output_parsers/list.py +3 -5
- langchain_core/output_parsers/openai_functions.py +3 -3
- langchain_core/output_parsers/openai_tools.py +3 -3
- langchain_core/output_parsers/pydantic.py +2 -2
- langchain_core/output_parsers/transform.py +13 -15
- langchain_core/output_parsers/xml.py +7 -9
- langchain_core/outputs/chat_generation.py +4 -4
- langchain_core/outputs/chat_result.py +1 -3
- langchain_core/outputs/generation.py +2 -2
- langchain_core/outputs/llm_result.py +5 -5
- langchain_core/prompts/__init__.py +1 -5
- langchain_core/prompts/base.py +10 -15
- langchain_core/prompts/chat.py +31 -82
- langchain_core/prompts/dict.py +2 -2
- langchain_core/prompts/few_shot.py +5 -5
- langchain_core/prompts/few_shot_with_templates.py +4 -4
- langchain_core/prompts/loading.py +3 -5
- langchain_core/prompts/prompt.py +4 -16
- langchain_core/prompts/string.py +2 -1
- langchain_core/prompts/structured.py +16 -23
- langchain_core/rate_limiters.py +3 -4
- langchain_core/retrievers.py +14 -14
- langchain_core/runnables/base.py +928 -1042
- langchain_core/runnables/branch.py +36 -40
- langchain_core/runnables/config.py +27 -35
- langchain_core/runnables/configurable.py +108 -124
- langchain_core/runnables/fallbacks.py +76 -72
- langchain_core/runnables/graph.py +39 -45
- langchain_core/runnables/graph_ascii.py +9 -11
- langchain_core/runnables/graph_mermaid.py +18 -19
- langchain_core/runnables/graph_png.py +8 -9
- langchain_core/runnables/history.py +114 -127
- langchain_core/runnables/passthrough.py +113 -139
- langchain_core/runnables/retry.py +43 -48
- langchain_core/runnables/router.py +23 -28
- langchain_core/runnables/schema.py +42 -44
- langchain_core/runnables/utils.py +28 -31
- langchain_core/stores.py +9 -13
- langchain_core/structured_query.py +8 -8
- langchain_core/tools/base.py +62 -115
- langchain_core/tools/convert.py +31 -35
- langchain_core/tools/render.py +1 -1
- langchain_core/tools/retriever.py +4 -4
- langchain_core/tools/simple.py +13 -17
- langchain_core/tools/structured.py +12 -15
- langchain_core/tracers/base.py +62 -64
- langchain_core/tracers/context.py +17 -35
- langchain_core/tracers/core.py +49 -53
- langchain_core/tracers/evaluation.py +11 -11
- langchain_core/tracers/event_stream.py +58 -60
- langchain_core/tracers/langchain.py +13 -13
- langchain_core/tracers/log_stream.py +22 -24
- langchain_core/tracers/root_listeners.py +14 -14
- langchain_core/tracers/run_collector.py +2 -4
- langchain_core/tracers/schemas.py +8 -8
- langchain_core/tracers/stdout.py +2 -1
- langchain_core/utils/__init__.py +0 -3
- langchain_core/utils/_merge.py +2 -2
- langchain_core/utils/aiter.py +24 -28
- langchain_core/utils/env.py +4 -4
- langchain_core/utils/function_calling.py +31 -41
- langchain_core/utils/html.py +3 -4
- langchain_core/utils/input.py +3 -3
- langchain_core/utils/iter.py +15 -19
- langchain_core/utils/json.py +3 -2
- langchain_core/utils/json_schema.py +6 -6
- langchain_core/utils/mustache.py +3 -5
- langchain_core/utils/pydantic.py +16 -18
- langchain_core/utils/usage.py +1 -1
- langchain_core/utils/utils.py +29 -29
- langchain_core/vectorstores/base.py +18 -21
- langchain_core/vectorstores/in_memory.py +14 -87
- langchain_core/vectorstores/utils.py +2 -2
- langchain_core/version.py +1 -1
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.0a8.dist-info}/METADATA +10 -21
- langchain_core-1.0.0a8.dist-info/RECORD +176 -0
- {langchain_core-1.0.0a6.dist-info → langchain_core-1.0.0a8.dist-info}/WHEEL +1 -1
- langchain_core/messages/block_translators/ollama.py +0 -47
- langchain_core/prompts/pipeline.py +0 -138
- langchain_core/tracers/langchain_v1.py +0 -31
- langchain_core/utils/loading.py +0 -35
- langchain_core-1.0.0a6.dist-info/RECORD +0 -181
- langchain_core-1.0.0a6.dist-info/entry_points.txt +0 -4
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"""Runnable that selects which branch to run based on a condition."""
|
|
2
2
|
|
|
3
|
-
from collections.abc import
|
|
3
|
+
from collections.abc import (
|
|
4
|
+
AsyncIterator,
|
|
5
|
+
Awaitable,
|
|
6
|
+
Callable,
|
|
7
|
+
Iterator,
|
|
8
|
+
Mapping,
|
|
9
|
+
Sequence,
|
|
10
|
+
)
|
|
4
11
|
from typing import (
|
|
5
12
|
Any,
|
|
6
|
-
Callable,
|
|
7
|
-
Optional,
|
|
8
|
-
Union,
|
|
9
13
|
cast,
|
|
10
14
|
)
|
|
11
15
|
|
|
@@ -45,21 +49,19 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
45
49
|
If no condition evaluates to True, the default branch is run on the input.
|
|
46
50
|
|
|
47
51
|
Examples:
|
|
52
|
+
```python
|
|
53
|
+
from langchain_core.runnables import RunnableBranch
|
|
54
|
+
|
|
55
|
+
branch = RunnableBranch(
|
|
56
|
+
(lambda x: isinstance(x, str), lambda x: x.upper()),
|
|
57
|
+
(lambda x: isinstance(x, int), lambda x: x + 1),
|
|
58
|
+
(lambda x: isinstance(x, float), lambda x: x * 2),
|
|
59
|
+
lambda x: "goodbye",
|
|
60
|
+
)
|
|
48
61
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
branch = RunnableBranch(
|
|
54
|
-
(lambda x: isinstance(x, str), lambda x: x.upper()),
|
|
55
|
-
(lambda x: isinstance(x, int), lambda x: x + 1),
|
|
56
|
-
(lambda x: isinstance(x, float), lambda x: x * 2),
|
|
57
|
-
lambda x: "goodbye",
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
branch.invoke("hello") # "HELLO"
|
|
61
|
-
branch.invoke(None) # "goodbye"
|
|
62
|
-
|
|
62
|
+
branch.invoke("hello") # "HELLO"
|
|
63
|
+
branch.invoke(None) # "goodbye"
|
|
64
|
+
```
|
|
63
65
|
"""
|
|
64
66
|
|
|
65
67
|
branches: Sequence[tuple[Runnable[Input, bool], Runnable[Input, Output]]]
|
|
@@ -69,17 +71,13 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
69
71
|
|
|
70
72
|
def __init__(
|
|
71
73
|
self,
|
|
72
|
-
*branches:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
RunnableLike,
|
|
80
|
-
],
|
|
81
|
-
RunnableLike, # To accommodate the default branch
|
|
82
|
-
],
|
|
74
|
+
*branches: tuple[
|
|
75
|
+
Runnable[Input, bool]
|
|
76
|
+
| Callable[[Input], bool]
|
|
77
|
+
| Callable[[Input], Awaitable[bool]],
|
|
78
|
+
RunnableLike,
|
|
79
|
+
]
|
|
80
|
+
| RunnableLike,
|
|
83
81
|
) -> None:
|
|
84
82
|
"""A Runnable that runs one of two branches based on a condition.
|
|
85
83
|
|
|
@@ -156,9 +154,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
156
154
|
return ["langchain", "schema", "runnable"]
|
|
157
155
|
|
|
158
156
|
@override
|
|
159
|
-
def get_input_schema(
|
|
160
|
-
self, config: Optional[RunnableConfig] = None
|
|
161
|
-
) -> type[BaseModel]:
|
|
157
|
+
def get_input_schema(self, config: RunnableConfig | None = None) -> type[BaseModel]:
|
|
162
158
|
runnables = (
|
|
163
159
|
[self.default]
|
|
164
160
|
+ [r for _, r in self.branches]
|
|
@@ -189,7 +185,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
189
185
|
|
|
190
186
|
@override
|
|
191
187
|
def invoke(
|
|
192
|
-
self, input: Input, config:
|
|
188
|
+
self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
|
|
193
189
|
) -> Output:
|
|
194
190
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
195
191
|
|
|
@@ -248,7 +244,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
248
244
|
|
|
249
245
|
@override
|
|
250
246
|
async def ainvoke(
|
|
251
|
-
self, input: Input, config:
|
|
247
|
+
self, input: Input, config: RunnableConfig | None = None, **kwargs: Any
|
|
252
248
|
) -> Output:
|
|
253
249
|
config = ensure_config(config)
|
|
254
250
|
callback_manager = get_async_callback_manager_for_config(config)
|
|
@@ -298,8 +294,8 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
298
294
|
def stream(
|
|
299
295
|
self,
|
|
300
296
|
input: Input,
|
|
301
|
-
config:
|
|
302
|
-
**kwargs:
|
|
297
|
+
config: RunnableConfig | None = None,
|
|
298
|
+
**kwargs: Any | None,
|
|
303
299
|
) -> Iterator[Output]:
|
|
304
300
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
305
301
|
|
|
@@ -319,7 +315,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
319
315
|
name=config.get("run_name") or self.get_name(),
|
|
320
316
|
run_id=config.pop("run_id", None),
|
|
321
317
|
)
|
|
322
|
-
final_output:
|
|
318
|
+
final_output: Output | None = None
|
|
323
319
|
final_output_supported = True
|
|
324
320
|
|
|
325
321
|
try:
|
|
@@ -382,8 +378,8 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
382
378
|
async def astream(
|
|
383
379
|
self,
|
|
384
380
|
input: Input,
|
|
385
|
-
config:
|
|
386
|
-
**kwargs:
|
|
381
|
+
config: RunnableConfig | None = None,
|
|
382
|
+
**kwargs: Any | None,
|
|
387
383
|
) -> AsyncIterator[Output]:
|
|
388
384
|
"""First evaluates the condition, then delegate to true or false branch.
|
|
389
385
|
|
|
@@ -403,7 +399,7 @@ class RunnableBranch(RunnableSerializable[Input, Output]):
|
|
|
403
399
|
name=config.get("run_name") or self.get_name(),
|
|
404
400
|
run_id=config.pop("run_id", None),
|
|
405
401
|
)
|
|
406
|
-
final_output:
|
|
402
|
+
final_output: Output | None = None
|
|
407
403
|
final_output_supported = True
|
|
408
404
|
|
|
409
405
|
try:
|
|
@@ -5,7 +5,7 @@ from __future__ import annotations
|
|
|
5
5
|
import asyncio
|
|
6
6
|
import uuid
|
|
7
7
|
import warnings
|
|
8
|
-
from collections.abc import Awaitable, Generator, Iterable, Iterator, Sequence
|
|
8
|
+
from collections.abc import Awaitable, Callable, Generator, Iterable, Iterator, Sequence
|
|
9
9
|
from concurrent.futures import Executor, Future, ThreadPoolExecutor
|
|
10
10
|
from contextlib import contextmanager
|
|
11
11
|
from contextvars import Context, ContextVar, Token, copy_context
|
|
@@ -13,11 +13,8 @@ from functools import partial
|
|
|
13
13
|
from typing import (
|
|
14
14
|
TYPE_CHECKING,
|
|
15
15
|
Any,
|
|
16
|
-
Callable,
|
|
17
|
-
Optional,
|
|
18
16
|
ParamSpec,
|
|
19
17
|
TypeVar,
|
|
20
|
-
Union,
|
|
21
18
|
cast,
|
|
22
19
|
)
|
|
23
20
|
|
|
@@ -42,7 +39,7 @@ if TYPE_CHECKING:
|
|
|
42
39
|
else:
|
|
43
40
|
# Pydantic validates through typed dicts, but
|
|
44
41
|
# the callbacks need forward refs updated
|
|
45
|
-
Callbacks =
|
|
42
|
+
Callbacks = list | Any | None
|
|
46
43
|
|
|
47
44
|
|
|
48
45
|
class EmptyDict(TypedDict, total=False):
|
|
@@ -75,7 +72,7 @@ class RunnableConfig(TypedDict, total=False):
|
|
|
75
72
|
Name for the tracer run for this call. Defaults to the name of the class.
|
|
76
73
|
"""
|
|
77
74
|
|
|
78
|
-
max_concurrency:
|
|
75
|
+
max_concurrency: int | None
|
|
79
76
|
"""
|
|
80
77
|
Maximum number of parallel calls to make. If not provided, defaults to
|
|
81
78
|
ThreadPoolExecutor's default.
|
|
@@ -94,7 +91,7 @@ class RunnableConfig(TypedDict, total=False):
|
|
|
94
91
|
configurable.
|
|
95
92
|
"""
|
|
96
93
|
|
|
97
|
-
run_id:
|
|
94
|
+
run_id: uuid.UUID | None
|
|
98
95
|
"""
|
|
99
96
|
Unique identifier for the tracer run for this call. If not provided, a new UUID
|
|
100
97
|
will be generated.
|
|
@@ -130,7 +127,7 @@ var_child_runnable_config: ContextVar[RunnableConfig | None] = ContextVar(
|
|
|
130
127
|
# This is imported and used in langgraph, so don't break.
|
|
131
128
|
def _set_config_context(
|
|
132
129
|
config: RunnableConfig,
|
|
133
|
-
) -> tuple[Token[
|
|
130
|
+
) -> tuple[Token[RunnableConfig | None], dict[str, Any] | None]:
|
|
134
131
|
"""Set the child Runnable config + tracing context.
|
|
135
132
|
|
|
136
133
|
Args:
|
|
@@ -192,7 +189,7 @@ def set_config_context(config: RunnableConfig) -> Generator[Context, None, None]
|
|
|
192
189
|
)
|
|
193
190
|
|
|
194
191
|
|
|
195
|
-
def ensure_config(config:
|
|
192
|
+
def ensure_config(config: RunnableConfig | None = None) -> RunnableConfig:
|
|
196
193
|
"""Ensure that a config is a dict with all keys present.
|
|
197
194
|
|
|
198
195
|
Args:
|
|
@@ -247,7 +244,7 @@ def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig:
|
|
|
247
244
|
|
|
248
245
|
|
|
249
246
|
def get_config_list(
|
|
250
|
-
config:
|
|
247
|
+
config: RunnableConfig | Sequence[RunnableConfig] | None, length: int
|
|
251
248
|
) -> list[RunnableConfig]:
|
|
252
249
|
"""Get a list of configs from a single config or a list of configs.
|
|
253
250
|
|
|
@@ -294,13 +291,13 @@ def get_config_list(
|
|
|
294
291
|
|
|
295
292
|
|
|
296
293
|
def patch_config(
|
|
297
|
-
config:
|
|
294
|
+
config: RunnableConfig | None,
|
|
298
295
|
*,
|
|
299
|
-
callbacks:
|
|
300
|
-
recursion_limit:
|
|
301
|
-
max_concurrency:
|
|
302
|
-
run_name:
|
|
303
|
-
configurable:
|
|
296
|
+
callbacks: BaseCallbackManager | None = None,
|
|
297
|
+
recursion_limit: int | None = None,
|
|
298
|
+
max_concurrency: int | None = None,
|
|
299
|
+
run_name: str | None = None,
|
|
300
|
+
configurable: dict[str, Any] | None = None,
|
|
304
301
|
) -> RunnableConfig:
|
|
305
302
|
"""Patch a config with new values.
|
|
306
303
|
|
|
@@ -339,7 +336,7 @@ def patch_config(
|
|
|
339
336
|
return config
|
|
340
337
|
|
|
341
338
|
|
|
342
|
-
def merge_configs(*configs:
|
|
339
|
+
def merge_configs(*configs: RunnableConfig | None) -> RunnableConfig:
|
|
343
340
|
"""Merge multiple configs into one.
|
|
344
341
|
|
|
345
342
|
Args:
|
|
@@ -406,15 +403,13 @@ def merge_configs(*configs: Optional[RunnableConfig]) -> RunnableConfig:
|
|
|
406
403
|
|
|
407
404
|
|
|
408
405
|
def call_func_with_variable_args(
|
|
409
|
-
func:
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
|
|
414
|
-
],
|
|
406
|
+
func: Callable[[Input], Output]
|
|
407
|
+
| Callable[[Input, RunnableConfig], Output]
|
|
408
|
+
| Callable[[Input, CallbackManagerForChainRun], Output]
|
|
409
|
+
| Callable[[Input, CallbackManagerForChainRun, RunnableConfig], Output],
|
|
415
410
|
input: Input,
|
|
416
411
|
config: RunnableConfig,
|
|
417
|
-
run_manager:
|
|
412
|
+
run_manager: CallbackManagerForChainRun | None = None,
|
|
418
413
|
**kwargs: Any,
|
|
419
414
|
) -> Output:
|
|
420
415
|
"""Call function that may optionally accept a run_manager and/or config.
|
|
@@ -440,18 +435,15 @@ def call_func_with_variable_args(
|
|
|
440
435
|
|
|
441
436
|
|
|
442
437
|
def acall_func_with_variable_args(
|
|
443
|
-
func:
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
[Input, AsyncCallbackManagerForChainRun, RunnableConfig],
|
|
449
|
-
Awaitable[Output],
|
|
450
|
-
],
|
|
438
|
+
func: Callable[[Input], Awaitable[Output]]
|
|
439
|
+
| Callable[[Input, RunnableConfig], Awaitable[Output]]
|
|
440
|
+
| Callable[[Input, AsyncCallbackManagerForChainRun], Awaitable[Output]]
|
|
441
|
+
| Callable[
|
|
442
|
+
[Input, AsyncCallbackManagerForChainRun, RunnableConfig], Awaitable[Output]
|
|
451
443
|
],
|
|
452
444
|
input: Input,
|
|
453
445
|
config: RunnableConfig,
|
|
454
|
-
run_manager:
|
|
446
|
+
run_manager: AsyncCallbackManagerForChainRun | None = None,
|
|
455
447
|
**kwargs: Any,
|
|
456
448
|
) -> Awaitable[Output]:
|
|
457
449
|
"""Async call function that may optionally accept a run_manager and/or config.
|
|
@@ -571,7 +563,7 @@ class ContextThreadPoolExecutor(ThreadPoolExecutor):
|
|
|
571
563
|
|
|
572
564
|
@contextmanager
|
|
573
565
|
def get_executor_for_config(
|
|
574
|
-
config:
|
|
566
|
+
config: RunnableConfig | None,
|
|
575
567
|
) -> Generator[Executor, None, None]:
|
|
576
568
|
"""Get an executor for a config.
|
|
577
569
|
|
|
@@ -589,7 +581,7 @@ def get_executor_for_config(
|
|
|
589
581
|
|
|
590
582
|
|
|
591
583
|
async def run_in_executor(
|
|
592
|
-
executor_or_config:
|
|
584
|
+
executor_or_config: Executor | RunnableConfig | None,
|
|
593
585
|
func: Callable[P, T],
|
|
594
586
|
*args: P.args,
|
|
595
587
|
**kwargs: P.kwargs,
|