langchain-core 0.4.0.dev0__py3-none-any.whl → 1.0.0a1__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/beta_decorator.py +2 -2
- langchain_core/_api/deprecation.py +1 -1
- langchain_core/beta/runnables/context.py +1 -1
- langchain_core/callbacks/base.py +14 -23
- langchain_core/callbacks/file.py +13 -2
- langchain_core/callbacks/manager.py +74 -157
- langchain_core/callbacks/streaming_stdout.py +3 -4
- langchain_core/callbacks/usage.py +2 -12
- langchain_core/chat_history.py +6 -6
- langchain_core/documents/base.py +1 -1
- langchain_core/documents/compressor.py +9 -6
- langchain_core/indexing/base.py +2 -2
- langchain_core/language_models/_utils.py +230 -101
- langchain_core/language_models/base.py +35 -23
- langchain_core/language_models/chat_models.py +245 -53
- langchain_core/language_models/fake_chat_models.py +28 -81
- langchain_core/load/dump.py +3 -4
- langchain_core/messages/__init__.py +38 -22
- langchain_core/messages/ai.py +188 -30
- langchain_core/messages/base.py +164 -25
- langchain_core/messages/block_translators/__init__.py +89 -0
- langchain_core/messages/block_translators/anthropic.py +451 -0
- langchain_core/messages/block_translators/bedrock.py +45 -0
- langchain_core/messages/block_translators/bedrock_converse.py +47 -0
- langchain_core/messages/block_translators/google_genai.py +45 -0
- langchain_core/messages/block_translators/google_vertexai.py +47 -0
- langchain_core/messages/block_translators/groq.py +45 -0
- langchain_core/messages/block_translators/langchain_v0.py +297 -0
- langchain_core/messages/block_translators/ollama.py +45 -0
- langchain_core/messages/block_translators/openai.py +586 -0
- langchain_core/messages/{content_blocks.py → content.py} +346 -213
- langchain_core/messages/human.py +29 -9
- langchain_core/messages/system.py +29 -9
- langchain_core/messages/tool.py +94 -13
- langchain_core/messages/utils.py +32 -234
- langchain_core/output_parsers/base.py +14 -50
- langchain_core/output_parsers/json.py +2 -5
- langchain_core/output_parsers/list.py +2 -7
- langchain_core/output_parsers/openai_functions.py +5 -28
- langchain_core/output_parsers/openai_tools.py +49 -90
- langchain_core/output_parsers/pydantic.py +2 -3
- langchain_core/output_parsers/transform.py +12 -53
- langchain_core/output_parsers/xml.py +9 -17
- langchain_core/prompt_values.py +8 -112
- langchain_core/prompts/chat.py +1 -3
- langchain_core/runnables/base.py +500 -451
- langchain_core/runnables/branch.py +1 -1
- langchain_core/runnables/fallbacks.py +4 -4
- langchain_core/runnables/history.py +1 -1
- langchain_core/runnables/passthrough.py +3 -3
- langchain_core/runnables/retry.py +1 -1
- langchain_core/runnables/router.py +1 -1
- langchain_core/structured_query.py +3 -7
- langchain_core/tools/base.py +14 -41
- langchain_core/tools/convert.py +2 -22
- langchain_core/tools/retriever.py +1 -8
- langchain_core/tools/structured.py +2 -10
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/base.py +7 -14
- langchain_core/tracers/core.py +4 -27
- langchain_core/tracers/event_stream.py +4 -15
- langchain_core/tracers/langchain.py +3 -14
- langchain_core/tracers/log_stream.py +2 -3
- langchain_core/utils/_merge.py +45 -7
- langchain_core/utils/function_calling.py +22 -9
- langchain_core/utils/utils.py +29 -0
- langchain_core/version.py +1 -1
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0a1.dist-info}/METADATA +7 -9
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0a1.dist-info}/RECORD +71 -64
- langchain_core/v1/__init__.py +0 -1
- langchain_core/v1/chat_models.py +0 -1047
- langchain_core/v1/messages.py +0 -755
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0a1.dist-info}/WHEEL +0 -0
- {langchain_core-0.4.0.dev0.dist-info → langchain_core-1.0.0a1.dist-info}/entry_points.txt +0 -0
langchain_core/runnables/base.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Base classes and utilities for
|
|
1
|
+
"""Base classes and utilities for ``Runnable``s."""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
@@ -111,17 +111,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
111
111
|
Key Methods
|
|
112
112
|
===========
|
|
113
113
|
|
|
114
|
-
-
|
|
115
|
-
-
|
|
116
|
-
-
|
|
117
|
-
-
|
|
114
|
+
- **``invoke``/``ainvoke``**: Transforms a single input into an output.
|
|
115
|
+
- **``batch``/``abatch``**: Efficiently transforms multiple inputs into outputs.
|
|
116
|
+
- **``stream``/``astream``**: Streams output from a single input as it's produced.
|
|
117
|
+
- **``astream_log``**: Streams output and selected intermediate results from an input.
|
|
118
118
|
|
|
119
119
|
Built-in optimizations:
|
|
120
120
|
|
|
121
121
|
- **Batch**: By default, batch runs invoke() in parallel using a thread pool executor.
|
|
122
122
|
Override to optimize batching.
|
|
123
123
|
|
|
124
|
-
- **Async**: Methods with
|
|
124
|
+
- **Async**: Methods with ``'a'`` suffix are asynchronous. By default, they execute
|
|
125
125
|
the sync counterpart using asyncio's thread pool.
|
|
126
126
|
Override for native async.
|
|
127
127
|
|
|
@@ -129,24 +129,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
129
129
|
execution, add tags and metadata for tracing and debugging etc.
|
|
130
130
|
|
|
131
131
|
Runnables expose schematic information about their input, output and config via
|
|
132
|
-
the input_schema property, the output_schema property and config_schema method.
|
|
132
|
+
the ``input_schema`` property, the ``output_schema`` property and ``config_schema`` method.
|
|
133
133
|
|
|
134
134
|
LCEL and Composition
|
|
135
135
|
====================
|
|
136
136
|
|
|
137
|
-
The LangChain Expression Language (LCEL) is a declarative way to compose Runnables
|
|
137
|
+
The LangChain Expression Language (LCEL) is a declarative way to compose ``Runnables``
|
|
138
138
|
into chains. Any chain constructed this way will automatically have sync, async,
|
|
139
139
|
batch, and streaming support.
|
|
140
140
|
|
|
141
|
-
The main composition primitives are RunnableSequence and RunnableParallel
|
|
141
|
+
The main composition primitives are ``RunnableSequence`` and ``RunnableParallel``.
|
|
142
142
|
|
|
143
|
-
|
|
143
|
+
**``RunnableSequence``** invokes a series of runnables sequentially, with
|
|
144
144
|
one Runnable's output serving as the next's input. Construct using
|
|
145
|
-
the
|
|
145
|
+
the ``|`` operator or by passing a list of runnables to ``RunnableSequence``.
|
|
146
146
|
|
|
147
|
-
|
|
147
|
+
**``RunnableParallel``** invokes runnables concurrently, providing the same input
|
|
148
148
|
to each. Construct it using a dict literal within a sequence or by passing a
|
|
149
|
-
dict to RunnableParallel
|
|
149
|
+
dict to ``RunnableParallel``.
|
|
150
150
|
|
|
151
151
|
|
|
152
152
|
For example,
|
|
@@ -171,11 +171,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
171
171
|
Standard Methods
|
|
172
172
|
================
|
|
173
173
|
|
|
174
|
-
All
|
|
174
|
+
All ``Runnable``s expose additional methods that can be used to modify their behavior
|
|
175
175
|
(e.g., add a retry policy, add lifecycle listeners, make them configurable, etc.).
|
|
176
176
|
|
|
177
|
-
These methods will work on any Runnable
|
|
178
|
-
by composing other
|
|
177
|
+
These methods will work on any ``Runnable``, including ``Runnable`` chains constructed
|
|
178
|
+
by composing other ``Runnable``s. See the individual methods for details.
|
|
179
179
|
|
|
180
180
|
For example,
|
|
181
181
|
|
|
@@ -232,17 +232,17 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
232
232
|
config={'callbacks': [ConsoleCallbackHandler()]}
|
|
233
233
|
)
|
|
234
234
|
|
|
235
|
-
For a UI (and much more) checkout LangSmith
|
|
235
|
+
For a UI (and much more) checkout `LangSmith <https://docs.smith.langchain.com/>`__.
|
|
236
236
|
|
|
237
237
|
""" # noqa: E501
|
|
238
238
|
|
|
239
239
|
name: Optional[str]
|
|
240
|
-
"""The name of the Runnable
|
|
240
|
+
"""The name of the ``Runnable``. Used for debugging and tracing."""
|
|
241
241
|
|
|
242
242
|
def get_name(
|
|
243
243
|
self, suffix: Optional[str] = None, *, name: Optional[str] = None
|
|
244
244
|
) -> str:
|
|
245
|
-
"""Get the name of the Runnable
|
|
245
|
+
"""Get the name of the ``Runnable``."""
|
|
246
246
|
if name:
|
|
247
247
|
name_ = name
|
|
248
248
|
elif hasattr(self, "name") and self.name:
|
|
@@ -273,7 +273,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
273
273
|
|
|
274
274
|
@property
|
|
275
275
|
def InputType(self) -> type[Input]: # noqa: N802
|
|
276
|
-
"""The type of input this Runnable accepts specified as a type annotation."""
|
|
276
|
+
"""The type of input this ``Runnable`` accepts specified as a type annotation.""" # noqa: E501
|
|
277
277
|
# First loop through all parent classes and if any of them is
|
|
278
278
|
# a pydantic model, we will pick up the generic parameterization
|
|
279
279
|
# from that model via the __pydantic_generic_metadata__ attribute.
|
|
@@ -299,7 +299,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
299
299
|
|
|
300
300
|
@property
|
|
301
301
|
def OutputType(self) -> type[Output]: # noqa: N802
|
|
302
|
-
"""The type of output this Runnable produces specified as a type annotation."""
|
|
302
|
+
"""The type of output this ``Runnable`` produces specified as a type annotation.""" # noqa: E501
|
|
303
303
|
# First loop through bases -- this will help generic
|
|
304
304
|
# any pydantic models.
|
|
305
305
|
for base in self.__class__.mro():
|
|
@@ -321,7 +321,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
321
321
|
|
|
322
322
|
@property
|
|
323
323
|
def input_schema(self) -> type[BaseModel]:
|
|
324
|
-
"""The type of input this Runnable accepts specified as a pydantic model."""
|
|
324
|
+
"""The type of input this ``Runnable`` accepts specified as a pydantic model."""
|
|
325
325
|
return self.get_input_schema()
|
|
326
326
|
|
|
327
327
|
def get_input_schema(
|
|
@@ -330,9 +330,9 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
330
330
|
) -> type[BaseModel]:
|
|
331
331
|
"""Get a pydantic model that can be used to validate input to the Runnable.
|
|
332
332
|
|
|
333
|
-
|
|
334
|
-
methods will have a dynamic input schema that
|
|
335
|
-
configuration the Runnable is invoked with.
|
|
333
|
+
``Runnable``s that leverage the ``configurable_fields`` and
|
|
334
|
+
``configurable_alternatives`` methods will have a dynamic input schema that
|
|
335
|
+
depends on which configuration the ``Runnable`` is invoked with.
|
|
336
336
|
|
|
337
337
|
This method allows to get an input schema for a specific configuration.
|
|
338
338
|
|
|
@@ -367,13 +367,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
367
367
|
def get_input_jsonschema(
|
|
368
368
|
self, config: Optional[RunnableConfig] = None
|
|
369
369
|
) -> dict[str, Any]:
|
|
370
|
-
"""Get a JSON schema that represents the input to the Runnable
|
|
370
|
+
"""Get a JSON schema that represents the input to the ``Runnable``.
|
|
371
371
|
|
|
372
372
|
Args:
|
|
373
373
|
config: A config to use when generating the schema.
|
|
374
374
|
|
|
375
375
|
Returns:
|
|
376
|
-
A JSON schema that represents the input to the Runnable
|
|
376
|
+
A JSON schema that represents the input to the ``Runnable``.
|
|
377
377
|
|
|
378
378
|
Example:
|
|
379
379
|
|
|
@@ -395,18 +395,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
395
395
|
|
|
396
396
|
@property
|
|
397
397
|
def output_schema(self) -> type[BaseModel]:
|
|
398
|
-
"""The type of output this Runnable produces specified as a pydantic model."""
|
|
398
|
+
"""The type of output this ``Runnable`` produces specified as a pydantic model.""" # noqa: E501
|
|
399
399
|
return self.get_output_schema()
|
|
400
400
|
|
|
401
401
|
def get_output_schema(
|
|
402
402
|
self,
|
|
403
403
|
config: Optional[RunnableConfig] = None, # noqa: ARG002
|
|
404
404
|
) -> type[BaseModel]:
|
|
405
|
-
"""Get a pydantic model that can be used to validate output to the Runnable
|
|
405
|
+
"""Get a pydantic model that can be used to validate output to the ``Runnable``.
|
|
406
406
|
|
|
407
|
-
|
|
408
|
-
methods will have a dynamic output schema that
|
|
409
|
-
configuration the Runnable is invoked with.
|
|
407
|
+
``Runnable``s that leverage the ``configurable_fields`` and
|
|
408
|
+
``configurable_alternatives`` methods will have a dynamic output schema that
|
|
409
|
+
depends on which configuration the ``Runnable`` is invoked with.
|
|
410
410
|
|
|
411
411
|
This method allows to get an output schema for a specific configuration.
|
|
412
412
|
|
|
@@ -441,13 +441,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
441
441
|
def get_output_jsonschema(
|
|
442
442
|
self, config: Optional[RunnableConfig] = None
|
|
443
443
|
) -> dict[str, Any]:
|
|
444
|
-
"""Get a JSON schema that represents the output of the Runnable
|
|
444
|
+
"""Get a JSON schema that represents the output of the ``Runnable``.
|
|
445
445
|
|
|
446
446
|
Args:
|
|
447
447
|
config: A config to use when generating the schema.
|
|
448
448
|
|
|
449
449
|
Returns:
|
|
450
|
-
A JSON schema that represents the output of the Runnable
|
|
450
|
+
A JSON schema that represents the output of the ``Runnable``.
|
|
451
451
|
|
|
452
452
|
Example:
|
|
453
453
|
|
|
@@ -469,22 +469,23 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
469
469
|
|
|
470
470
|
@property
|
|
471
471
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
472
|
-
"""List configurable fields for this Runnable
|
|
472
|
+
"""List configurable fields for this ``Runnable``."""
|
|
473
473
|
return []
|
|
474
474
|
|
|
475
475
|
def config_schema(
|
|
476
476
|
self, *, include: Optional[Sequence[str]] = None
|
|
477
477
|
) -> type[BaseModel]:
|
|
478
|
-
"""The type of config this Runnable accepts specified as a pydantic model.
|
|
478
|
+
"""The type of config this ``Runnable`` accepts specified as a pydantic model.
|
|
479
479
|
|
|
480
|
-
To mark a field as configurable, see the
|
|
481
|
-
and
|
|
480
|
+
To mark a field as configurable, see the ``configurable_fields``
|
|
481
|
+
and ``configurable_alternatives`` methods.
|
|
482
482
|
|
|
483
483
|
Args:
|
|
484
484
|
include: A list of fields to include in the config schema.
|
|
485
485
|
|
|
486
486
|
Returns:
|
|
487
487
|
A pydantic model that can be used to validate config.
|
|
488
|
+
|
|
488
489
|
"""
|
|
489
490
|
include = include or []
|
|
490
491
|
config_specs = self.config_specs
|
|
@@ -519,20 +520,21 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
519
520
|
def get_config_jsonschema(
|
|
520
521
|
self, *, include: Optional[Sequence[str]] = None
|
|
521
522
|
) -> dict[str, Any]:
|
|
522
|
-
"""Get a JSON schema that represents the config of the Runnable
|
|
523
|
+
"""Get a JSON schema that represents the config of the ``Runnable``.
|
|
523
524
|
|
|
524
525
|
Args:
|
|
525
526
|
include: A list of fields to include in the config schema.
|
|
526
527
|
|
|
527
528
|
Returns:
|
|
528
|
-
A JSON schema that represents the config of the Runnable
|
|
529
|
+
A JSON schema that represents the config of the ``Runnable``.
|
|
529
530
|
|
|
530
531
|
.. versionadded:: 0.3.0
|
|
532
|
+
|
|
531
533
|
"""
|
|
532
534
|
return self.config_schema(include=include).model_json_schema()
|
|
533
535
|
|
|
534
536
|
def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph:
|
|
535
|
-
"""Return a graph representation of this Runnable
|
|
537
|
+
"""Return a graph representation of this ``Runnable``."""
|
|
536
538
|
graph = Graph()
|
|
537
539
|
try:
|
|
538
540
|
input_node = graph.add_node(self.get_input_schema(config))
|
|
@@ -552,7 +554,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
552
554
|
def get_prompts(
|
|
553
555
|
self, config: Optional[RunnableConfig] = None
|
|
554
556
|
) -> list[BasePromptTemplate]:
|
|
555
|
-
"""Return a list of prompts used by this Runnable
|
|
557
|
+
"""Return a list of prompts used by this ``Runnable``."""
|
|
556
558
|
from langchain_core.prompts.base import BasePromptTemplate
|
|
557
559
|
|
|
558
560
|
return [
|
|
@@ -571,7 +573,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
571
573
|
Mapping[str, Union[Runnable[Any, Other], Callable[[Any], Other], Any]],
|
|
572
574
|
],
|
|
573
575
|
) -> RunnableSerializable[Input, Other]:
|
|
574
|
-
"""Compose this Runnable with another object to create a RunnableSequence
|
|
576
|
+
"""Compose this ``Runnable`` with another object to create a ``RunnableSequence``.""" # noqa: E501
|
|
575
577
|
return RunnableSequence(self, coerce_to_runnable(other))
|
|
576
578
|
|
|
577
579
|
def __ror__(
|
|
@@ -584,7 +586,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
584
586
|
Mapping[str, Union[Runnable[Other, Any], Callable[[Other], Any], Any]],
|
|
585
587
|
],
|
|
586
588
|
) -> RunnableSerializable[Other, Output]:
|
|
587
|
-
"""Compose this Runnable with another object to create a RunnableSequence
|
|
589
|
+
"""Compose this ``Runnable`` with another object to create a ``RunnableSequence``.""" # noqa: E501
|
|
588
590
|
return RunnableSequence(coerce_to_runnable(other), self)
|
|
589
591
|
|
|
590
592
|
def pipe(
|
|
@@ -592,9 +594,9 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
592
594
|
*others: Union[Runnable[Any, Other], Callable[[Any], Other]],
|
|
593
595
|
name: Optional[str] = None,
|
|
594
596
|
) -> RunnableSerializable[Input, Other]:
|
|
595
|
-
"""Compose this Runnable with Runnable
|
|
597
|
+
"""Compose this ``Runnable`` with ``Runnable``-like objects to make a ``RunnableSequence``.
|
|
596
598
|
|
|
597
|
-
Equivalent to
|
|
599
|
+
Equivalent to ``RunnableSequence(self, *others)`` or ``self | others[0] | ...``
|
|
598
600
|
|
|
599
601
|
Example:
|
|
600
602
|
.. code-block:: python
|
|
@@ -621,11 +623,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
621
623
|
await sequence.abatch([1, 2, 3])
|
|
622
624
|
# -> [4, 6, 8]
|
|
623
625
|
|
|
624
|
-
"""
|
|
626
|
+
""" # noqa: E501
|
|
625
627
|
return RunnableSequence(self, *others, name=name)
|
|
626
628
|
|
|
627
629
|
def pick(self, keys: Union[str, list[str]]) -> RunnableSerializable[Any, Any]:
|
|
628
|
-
"""Pick keys from the output dict of this Runnable
|
|
630
|
+
"""Pick keys from the output dict of this ``Runnable``.
|
|
629
631
|
|
|
630
632
|
Pick single key:
|
|
631
633
|
.. code-block:: python
|
|
@@ -688,9 +690,9 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
688
690
|
],
|
|
689
691
|
],
|
|
690
692
|
) -> RunnableSerializable[Any, Any]:
|
|
691
|
-
"""Assigns new fields to the dict output of this Runnable
|
|
693
|
+
"""Assigns new fields to the dict output of this ``Runnable``.
|
|
692
694
|
|
|
693
|
-
Returns a new Runnable
|
|
695
|
+
Returns a new ``Runnable``.
|
|
694
696
|
|
|
695
697
|
.. code-block:: python
|
|
696
698
|
|
|
@@ -735,15 +737,16 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
735
737
|
"""Transform a single input into an output.
|
|
736
738
|
|
|
737
739
|
Args:
|
|
738
|
-
input: The input to the Runnable
|
|
739
|
-
config: A config to use when invoking the Runnable
|
|
740
|
+
input: The input to the ``Runnable``.
|
|
741
|
+
config: A config to use when invoking the ``Runnable``.
|
|
740
742
|
The config supports standard keys like ``'tags'``, ``'metadata'`` for
|
|
741
743
|
tracing purposes, ``'max_concurrency'`` for controlling how much work to
|
|
742
|
-
do in parallel, and other keys. Please refer to the RunnableConfig
|
|
744
|
+
do in parallel, and other keys. Please refer to the ``RunnableConfig``
|
|
743
745
|
for more details. Defaults to None.
|
|
744
746
|
|
|
745
747
|
Returns:
|
|
746
|
-
The output of the Runnable
|
|
748
|
+
The output of the ``Runnable``.
|
|
749
|
+
|
|
747
750
|
"""
|
|
748
751
|
|
|
749
752
|
async def ainvoke(
|
|
@@ -752,12 +755,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
752
755
|
config: Optional[RunnableConfig] = None,
|
|
753
756
|
**kwargs: Any,
|
|
754
757
|
) -> Output:
|
|
755
|
-
"""Default implementation of ainvoke
|
|
758
|
+
"""Default implementation of ``ainvoke``, calls ``invoke`` from a thread.
|
|
756
759
|
|
|
757
760
|
The default implementation allows usage of async code even if
|
|
758
|
-
the Runnable did not implement a native async version of invoke
|
|
761
|
+
the ``Runnable`` did not implement a native async version of ``invoke``.
|
|
759
762
|
|
|
760
763
|
Subclasses should override this method if they can run asynchronously.
|
|
764
|
+
|
|
761
765
|
"""
|
|
762
766
|
return await run_in_executor(config, self.invoke, input, config, **kwargs)
|
|
763
767
|
|
|
@@ -774,7 +778,8 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
774
778
|
The default implementation of batch works well for IO bound runnables.
|
|
775
779
|
|
|
776
780
|
Subclasses should override this method if they can batch more efficiently;
|
|
777
|
-
e.g., if the underlying Runnable uses an API which supports a batch mode.
|
|
781
|
+
e.g., if the underlying ``Runnable`` uses an API which supports a batch mode.
|
|
782
|
+
|
|
778
783
|
"""
|
|
779
784
|
if not inputs:
|
|
780
785
|
return []
|
|
@@ -825,9 +830,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
825
830
|
return_exceptions: bool = False,
|
|
826
831
|
**kwargs: Optional[Any],
|
|
827
832
|
) -> Iterator[tuple[int, Union[Output, Exception]]]:
|
|
828
|
-
"""Run invoke in parallel on a list of inputs.
|
|
833
|
+
"""Run ``invoke`` in parallel on a list of inputs.
|
|
829
834
|
|
|
830
835
|
Yields results as they complete.
|
|
836
|
+
|
|
831
837
|
"""
|
|
832
838
|
if not inputs:
|
|
833
839
|
return
|
|
@@ -876,26 +882,27 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
876
882
|
return_exceptions: bool = False,
|
|
877
883
|
**kwargs: Optional[Any],
|
|
878
884
|
) -> list[Output]:
|
|
879
|
-
"""Default implementation runs ainvoke in parallel using asyncio.gather
|
|
885
|
+
"""Default implementation runs ``ainvoke`` in parallel using ``asyncio.gather``.
|
|
880
886
|
|
|
881
|
-
The default implementation of batch works well for IO bound runnables.
|
|
887
|
+
The default implementation of ``batch`` works well for IO bound runnables.
|
|
882
888
|
|
|
883
889
|
Subclasses should override this method if they can batch more efficiently;
|
|
884
|
-
e.g., if the underlying Runnable uses an API which supports a batch mode.
|
|
890
|
+
e.g., if the underlying ``Runnable`` uses an API which supports a batch mode.
|
|
885
891
|
|
|
886
892
|
Args:
|
|
887
|
-
inputs: A list of inputs to the Runnable
|
|
888
|
-
config: A config to use when invoking the Runnable
|
|
893
|
+
inputs: A list of inputs to the ``Runnable``.
|
|
894
|
+
config: A config to use when invoking the ``Runnable``.
|
|
889
895
|
The config supports standard keys like ``'tags'``, ``'metadata'`` for
|
|
890
896
|
tracing purposes, ``'max_concurrency'`` for controlling how much work to
|
|
891
|
-
do in parallel, and other keys. Please refer to the RunnableConfig
|
|
897
|
+
do in parallel, and other keys. Please refer to the ``RunnableConfig``
|
|
892
898
|
for more details. Defaults to None.
|
|
893
899
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
894
900
|
Defaults to False.
|
|
895
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
901
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
896
902
|
|
|
897
903
|
Returns:
|
|
898
|
-
A list of outputs from the Runnable
|
|
904
|
+
A list of outputs from the ``Runnable``.
|
|
905
|
+
|
|
899
906
|
"""
|
|
900
907
|
if not inputs:
|
|
901
908
|
return []
|
|
@@ -944,23 +951,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
944
951
|
return_exceptions: bool = False,
|
|
945
952
|
**kwargs: Optional[Any],
|
|
946
953
|
) -> AsyncIterator[tuple[int, Union[Output, Exception]]]:
|
|
947
|
-
"""Run ainvoke in parallel on a list of inputs.
|
|
954
|
+
"""Run ``ainvoke`` in parallel on a list of inputs.
|
|
948
955
|
|
|
949
956
|
Yields results as they complete.
|
|
950
957
|
|
|
951
958
|
Args:
|
|
952
|
-
inputs: A list of inputs to the Runnable
|
|
953
|
-
config: A config to use when invoking the Runnable
|
|
959
|
+
inputs: A list of inputs to the ``Runnable``.
|
|
960
|
+
config: A config to use when invoking the ``Runnable``.
|
|
954
961
|
The config supports standard keys like ``'tags'``, ``'metadata'`` for
|
|
955
962
|
tracing purposes, ``'max_concurrency'`` for controlling how much work to
|
|
956
|
-
do in parallel, and other keys. Please refer to the RunnableConfig
|
|
963
|
+
do in parallel, and other keys. Please refer to the ``RunnableConfig``
|
|
957
964
|
for more details. Defaults to None.
|
|
958
965
|
return_exceptions: Whether to return exceptions instead of raising them.
|
|
959
966
|
Defaults to False.
|
|
960
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
967
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
961
968
|
|
|
962
969
|
Yields:
|
|
963
|
-
A tuple of the index of the input and the output from the Runnable
|
|
970
|
+
A tuple of the index of the input and the output from the ``Runnable``.
|
|
971
|
+
|
|
964
972
|
"""
|
|
965
973
|
if not inputs:
|
|
966
974
|
return
|
|
@@ -1000,17 +1008,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1000
1008
|
config: Optional[RunnableConfig] = None,
|
|
1001
1009
|
**kwargs: Optional[Any],
|
|
1002
1010
|
) -> Iterator[Output]:
|
|
1003
|
-
"""Default implementation of stream
|
|
1011
|
+
"""Default implementation of ``stream``, which calls ``invoke``.
|
|
1004
1012
|
|
|
1005
1013
|
Subclasses should override this method if they support streaming output.
|
|
1006
1014
|
|
|
1007
1015
|
Args:
|
|
1008
|
-
input: The input to the Runnable
|
|
1009
|
-
config: The config to use for the Runnable
|
|
1010
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1016
|
+
input: The input to the ``Runnable``.
|
|
1017
|
+
config: The config to use for the ``Runnable``. Defaults to None.
|
|
1018
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1011
1019
|
|
|
1012
1020
|
Yields:
|
|
1013
|
-
The output of the Runnable
|
|
1021
|
+
The output of the ``Runnable``.
|
|
1022
|
+
|
|
1014
1023
|
"""
|
|
1015
1024
|
yield self.invoke(input, config, **kwargs)
|
|
1016
1025
|
|
|
@@ -1020,17 +1029,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1020
1029
|
config: Optional[RunnableConfig] = None,
|
|
1021
1030
|
**kwargs: Optional[Any],
|
|
1022
1031
|
) -> AsyncIterator[Output]:
|
|
1023
|
-
"""Default implementation of astream
|
|
1032
|
+
"""Default implementation of ``astream``, which calls ``ainvoke``.
|
|
1024
1033
|
|
|
1025
1034
|
Subclasses should override this method if they support streaming output.
|
|
1026
1035
|
|
|
1027
1036
|
Args:
|
|
1028
|
-
input: The input to the Runnable
|
|
1029
|
-
config: The config to use for the Runnable
|
|
1030
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1037
|
+
input: The input to the ``Runnable``.
|
|
1038
|
+
config: The config to use for the ``Runnable``. Defaults to None.
|
|
1039
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1031
1040
|
|
|
1032
1041
|
Yields:
|
|
1033
|
-
The output of the Runnable
|
|
1042
|
+
The output of the ``Runnable``.
|
|
1043
|
+
|
|
1034
1044
|
"""
|
|
1035
1045
|
yield await self.ainvoke(input, config, **kwargs)
|
|
1036
1046
|
|
|
@@ -1083,7 +1093,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1083
1093
|
exclude_tags: Optional[Sequence[str]] = None,
|
|
1084
1094
|
**kwargs: Any,
|
|
1085
1095
|
) -> Union[AsyncIterator[RunLogPatch], AsyncIterator[RunLog]]:
|
|
1086
|
-
"""Stream all output from a Runnable
|
|
1096
|
+
"""Stream all output from a ``Runnable``, as reported to the callback system.
|
|
1087
1097
|
|
|
1088
1098
|
This includes all inner runs of LLMs, Retrievers, Tools, etc.
|
|
1089
1099
|
|
|
@@ -1094,20 +1104,21 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1094
1104
|
The Jsonpatch ops can be applied in order to construct state.
|
|
1095
1105
|
|
|
1096
1106
|
Args:
|
|
1097
|
-
input: The input to the Runnable
|
|
1098
|
-
config: The config to use for the Runnable
|
|
1107
|
+
input: The input to the ``Runnable``.
|
|
1108
|
+
config: The config to use for the ``Runnable``.
|
|
1099
1109
|
diff: Whether to yield diffs between each step or the current state.
|
|
1100
|
-
with_streamed_output_list: Whether to yield the streamed_output list.
|
|
1110
|
+
with_streamed_output_list: Whether to yield the ``streamed_output`` list.
|
|
1101
1111
|
include_names: Only include logs with these names.
|
|
1102
1112
|
include_types: Only include logs with these types.
|
|
1103
1113
|
include_tags: Only include logs with these tags.
|
|
1104
1114
|
exclude_names: Exclude logs with these names.
|
|
1105
1115
|
exclude_types: Exclude logs with these types.
|
|
1106
1116
|
exclude_tags: Exclude logs with these tags.
|
|
1107
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1117
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1108
1118
|
|
|
1109
1119
|
Yields:
|
|
1110
|
-
A RunLogPatch or RunLog object.
|
|
1120
|
+
A ``RunLogPatch`` or ``RunLog`` object.
|
|
1121
|
+
|
|
1111
1122
|
"""
|
|
1112
1123
|
from langchain_core.tracers.log_stream import (
|
|
1113
1124
|
LogStreamCallbackHandler,
|
|
@@ -1155,73 +1166,73 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1155
1166
|
) -> AsyncIterator[StreamEvent]:
|
|
1156
1167
|
"""Generate a stream of events.
|
|
1157
1168
|
|
|
1158
|
-
Use to create an iterator over StreamEvents that provide real-time information
|
|
1159
|
-
about the progress of the Runnable
|
|
1169
|
+
Use to create an iterator over ``StreamEvents`` that provide real-time information
|
|
1170
|
+
about the progress of the ``Runnable``, including ``StreamEvents`` from intermediate
|
|
1160
1171
|
results.
|
|
1161
1172
|
|
|
1162
|
-
A StreamEvent is a dictionary with the following schema:
|
|
1173
|
+
A ``StreamEvent`` is a dictionary with the following schema:
|
|
1163
1174
|
|
|
1164
1175
|
- ``event``: **str** - Event names are of the format:
|
|
1165
|
-
on_[runnable_type]_(start|stream|end)
|
|
1166
|
-
- ``name``: **str** - The name of the Runnable that generated the event.
|
|
1176
|
+
``on_[runnable_type]_(start|stream|end)``.
|
|
1177
|
+
- ``name``: **str** - The name of the ``Runnable`` that generated the event.
|
|
1167
1178
|
- ``run_id``: **str** - randomly generated ID associated with the given
|
|
1168
|
-
execution of the Runnable that emitted the event. A child Runnable that gets
|
|
1169
|
-
invoked as part of the execution of a parent Runnable is assigned its own
|
|
1179
|
+
execution of the ``Runnable`` that emitted the event. A child ``Runnable`` that gets
|
|
1180
|
+
invoked as part of the execution of a parent ``Runnable`` is assigned its own
|
|
1170
1181
|
unique ID.
|
|
1171
1182
|
- ``parent_ids``: **list[str]** - The IDs of the parent runnables that generated
|
|
1172
|
-
the event. The root Runnable will have an empty list. The order of the parent
|
|
1183
|
+
the event. The root ``Runnable`` will have an empty list. The order of the parent
|
|
1173
1184
|
IDs is from the root to the immediate parent. Only available for v2 version of
|
|
1174
1185
|
the API. The v1 version of the API will return an empty list.
|
|
1175
|
-
- ``tags``: **Optional[list[str]]** - The tags of the Runnable that generated
|
|
1186
|
+
- ``tags``: **Optional[list[str]]** - The tags of the ``Runnable`` that generated
|
|
1176
1187
|
the event.
|
|
1177
|
-
- ``metadata``: **Optional[dict[str, Any]]** - The metadata of the Runnable that
|
|
1188
|
+
- ``metadata``: **Optional[dict[str, Any]]** - The metadata of the ``Runnable`` that
|
|
1178
1189
|
generated the event.
|
|
1179
1190
|
- ``data``: **dict[str, Any]**
|
|
1180
1191
|
|
|
1181
|
-
|
|
1182
1192
|
Below is a table that illustrates some events that might be emitted by various
|
|
1183
1193
|
chains. Metadata fields have been omitted from the table for brevity.
|
|
1184
1194
|
Chain definitions have been included after the table.
|
|
1185
1195
|
|
|
1186
|
-
..
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1196
|
+
.. note::
|
|
1197
|
+
This reference table is for the v2 version of the schema.
|
|
1198
|
+
|
|
1199
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1200
|
+
| event | name | chunk | input | output |
|
|
1201
|
+
+==========================+==================+=====================================+===================================================+=====================================================+
|
|
1202
|
+
| ``on_chat_model_start`` | [model name] | | ``{"messages": [[SystemMessage, HumanMessage]]}`` | |
|
|
1203
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1204
|
+
| ``on_chat_model_stream`` | [model name] | ``AIMessageChunk(content="hello")`` | | |
|
|
1205
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1206
|
+
| ``on_chat_model_end`` | [model name] | | ``{"messages": [[SystemMessage, HumanMessage]]}`` | ``AIMessageChunk(content="hello world")`` |
|
|
1207
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1208
|
+
| ``on_llm_start`` | [model name] | | ``{'input': 'hello'}`` | |
|
|
1209
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1210
|
+
| ``on_llm_stream`` | [model name] | ``'Hello' `` | | |
|
|
1211
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1212
|
+
| ``on_llm_end`` | [model name] | | ``'Hello human!'`` | |
|
|
1213
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1214
|
+
| ``on_chain_start`` | format_docs | | | |
|
|
1215
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1216
|
+
| ``on_chain_stream`` | format_docs | ``'hello world!, goodbye world!'`` | | |
|
|
1217
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1218
|
+
| ``on_chain_end`` | format_docs | | ``[Document(...)]`` | ``'hello world!, goodbye world!'`` |
|
|
1219
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1220
|
+
| ``on_tool_start`` | some_tool | | ``{"x": 1, "y": "2"}`` | |
|
|
1221
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1222
|
+
| ``on_tool_end`` | some_tool | | | ``{"x": 1, "y": "2"}`` |
|
|
1223
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1224
|
+
| ``on_retriever_start`` | [retriever name] | | ``{"query": "hello"}`` | |
|
|
1225
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1226
|
+
| ``on_retriever_end`` | [retriever name] | | ``{"query": "hello"}`` | ``[Document(...), ..]`` |
|
|
1227
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1228
|
+
| ``on_prompt_start`` | [template_name] | | ``{"question": "hello"}`` | |
|
|
1229
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1230
|
+
| ``on_prompt_end`` | [template_name] | | ``{"question": "hello"}`` | ``ChatPromptValue(messages: [SystemMessage, ...])`` |
|
|
1231
|
+
+--------------------------+------------------+-------------------------------------+---------------------------------------------------+-----------------------------------------------------+
|
|
1221
1232
|
|
|
1222
1233
|
In addition to the standard events, users can also dispatch custom events (see example below).
|
|
1223
1234
|
|
|
1224
|
-
Custom events will be only be surfaced with in the
|
|
1235
|
+
Custom events will be only be surfaced with in the v2 version of the API!
|
|
1225
1236
|
|
|
1226
1237
|
A custom event has following format:
|
|
1227
1238
|
|
|
@@ -1235,7 +1246,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1235
1246
|
|
|
1236
1247
|
Here are declarations associated with the standard events shown above:
|
|
1237
1248
|
|
|
1238
|
-
|
|
1249
|
+
``format_docs``:
|
|
1239
1250
|
|
|
1240
1251
|
.. code-block:: python
|
|
1241
1252
|
|
|
@@ -1245,7 +1256,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1245
1256
|
|
|
1246
1257
|
format_docs = RunnableLambda(format_docs)
|
|
1247
1258
|
|
|
1248
|
-
|
|
1259
|
+
``some_tool``:
|
|
1249
1260
|
|
|
1250
1261
|
.. code-block:: python
|
|
1251
1262
|
|
|
@@ -1254,7 +1265,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1254
1265
|
'''Some_tool.'''
|
|
1255
1266
|
return {"x": x, "y": y}
|
|
1256
1267
|
|
|
1257
|
-
|
|
1268
|
+
``prompt``:
|
|
1258
1269
|
|
|
1259
1270
|
.. code-block:: python
|
|
1260
1271
|
|
|
@@ -1339,29 +1350,29 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1339
1350
|
print(event)
|
|
1340
1351
|
|
|
1341
1352
|
Args:
|
|
1342
|
-
input: The input to the Runnable
|
|
1343
|
-
config: The config to use for the Runnable
|
|
1344
|
-
version: The version of the schema to use either
|
|
1345
|
-
Users should use
|
|
1346
|
-
|
|
1353
|
+
input: The input to the ``Runnable``.
|
|
1354
|
+
config: The config to use for the ``Runnable``.
|
|
1355
|
+
version: The version of the schema to use either ``'v2'`` or ``'v1'``.
|
|
1356
|
+
Users should use ``'v2'``.
|
|
1357
|
+
``'v1'`` is for backwards compatibility and will be deprecated
|
|
1347
1358
|
in 0.4.0.
|
|
1348
1359
|
No default will be assigned until the API is stabilized.
|
|
1349
|
-
custom events will only be surfaced in
|
|
1350
|
-
include_names: Only include events from
|
|
1351
|
-
include_types: Only include events from
|
|
1352
|
-
include_tags: Only include events from
|
|
1353
|
-
exclude_names: Exclude events from
|
|
1354
|
-
exclude_types: Exclude events from
|
|
1355
|
-
exclude_tags: Exclude events from
|
|
1356
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1357
|
-
These will be passed to astream_log as this implementation
|
|
1358
|
-
of astream_events is built on top of astream_log
|
|
1360
|
+
custom events will only be surfaced in ``'v2'``.
|
|
1361
|
+
include_names: Only include events from ``Runnables`` with matching names.
|
|
1362
|
+
include_types: Only include events from ``Runnables`` with matching types.
|
|
1363
|
+
include_tags: Only include events from ``Runnables`` with matching tags.
|
|
1364
|
+
exclude_names: Exclude events from ``Runnables`` with matching names.
|
|
1365
|
+
exclude_types: Exclude events from ``Runnables`` with matching types.
|
|
1366
|
+
exclude_tags: Exclude events from ``Runnables`` with matching tags.
|
|
1367
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1368
|
+
These will be passed to ``astream_log`` as this implementation
|
|
1369
|
+
of ``astream_events`` is built on top of ``astream_log``.
|
|
1359
1370
|
|
|
1360
1371
|
Yields:
|
|
1361
|
-
An async stream of StreamEvents
|
|
1372
|
+
An async stream of ``StreamEvents``.
|
|
1362
1373
|
|
|
1363
1374
|
Raises:
|
|
1364
|
-
NotImplementedError: If the version is not
|
|
1375
|
+
NotImplementedError: If the version is not ``'v1'`` or ``'v2'``.
|
|
1365
1376
|
|
|
1366
1377
|
""" # noqa: E501
|
|
1367
1378
|
from langchain_core.tracers.event_stream import (
|
|
@@ -1411,19 +1422,20 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1411
1422
|
config: Optional[RunnableConfig] = None,
|
|
1412
1423
|
**kwargs: Optional[Any],
|
|
1413
1424
|
) -> Iterator[Output]:
|
|
1414
|
-
"""Default implementation of transform, which buffers input and calls astream
|
|
1425
|
+
"""Default implementation of transform, which buffers input and calls ``astream``.
|
|
1415
1426
|
|
|
1416
1427
|
Subclasses should override this method if they can start producing output while
|
|
1417
1428
|
input is still being generated.
|
|
1418
1429
|
|
|
1419
1430
|
Args:
|
|
1420
|
-
input: An iterator of inputs to the Runnable
|
|
1421
|
-
config: The config to use for the Runnable
|
|
1422
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1431
|
+
input: An iterator of inputs to the ``Runnable``.
|
|
1432
|
+
config: The config to use for the ``Runnable``. Defaults to None.
|
|
1433
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1423
1434
|
|
|
1424
1435
|
Yields:
|
|
1425
|
-
The output of the Runnable
|
|
1426
|
-
|
|
1436
|
+
The output of the ``Runnable``.
|
|
1437
|
+
|
|
1438
|
+
""" # noqa: E501
|
|
1427
1439
|
final: Input
|
|
1428
1440
|
got_first_val = False
|
|
1429
1441
|
|
|
@@ -1453,19 +1465,20 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1453
1465
|
config: Optional[RunnableConfig] = None,
|
|
1454
1466
|
**kwargs: Optional[Any],
|
|
1455
1467
|
) -> AsyncIterator[Output]:
|
|
1456
|
-
"""Default implementation of atransform, which buffers input and calls astream
|
|
1468
|
+
"""Default implementation of atransform, which buffers input and calls ``astream``.
|
|
1457
1469
|
|
|
1458
1470
|
Subclasses should override this method if they can start producing output while
|
|
1459
1471
|
input is still being generated.
|
|
1460
1472
|
|
|
1461
1473
|
Args:
|
|
1462
|
-
input: An async iterator of inputs to the Runnable
|
|
1463
|
-
config: The config to use for the Runnable
|
|
1464
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1474
|
+
input: An async iterator of inputs to the ``Runnable``.
|
|
1475
|
+
config: The config to use for the ``Runnable``. Defaults to None.
|
|
1476
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1465
1477
|
|
|
1466
1478
|
Yields:
|
|
1467
|
-
The output of the Runnable
|
|
1468
|
-
|
|
1479
|
+
The output of the ``Runnable``.
|
|
1480
|
+
|
|
1481
|
+
""" # noqa: E501
|
|
1469
1482
|
final: Input
|
|
1470
1483
|
got_first_val = False
|
|
1471
1484
|
|
|
@@ -1491,16 +1504,16 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1491
1504
|
yield output
|
|
1492
1505
|
|
|
1493
1506
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
1494
|
-
"""Bind arguments to a Runnable
|
|
1507
|
+
"""Bind arguments to a ``Runnable``, returning a new ``Runnable``.
|
|
1495
1508
|
|
|
1496
|
-
Useful when a Runnable in a chain requires an argument that is not
|
|
1497
|
-
in the output of the previous Runnable or included in the user input.
|
|
1509
|
+
Useful when a ``Runnable`` in a chain requires an argument that is not
|
|
1510
|
+
in the output of the previous ``Runnable`` or included in the user input.
|
|
1498
1511
|
|
|
1499
1512
|
Args:
|
|
1500
|
-
kwargs: The arguments to bind to the Runnable
|
|
1513
|
+
kwargs: The arguments to bind to the ``Runnable``.
|
|
1501
1514
|
|
|
1502
1515
|
Returns:
|
|
1503
|
-
A new Runnable with the arguments bound.
|
|
1516
|
+
A new ``Runnable`` with the arguments bound.
|
|
1504
1517
|
|
|
1505
1518
|
Example:
|
|
1506
1519
|
|
|
@@ -1538,14 +1551,15 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1538
1551
|
# Sadly Unpack is not well-supported by mypy so this will have to be untyped
|
|
1539
1552
|
**kwargs: Any,
|
|
1540
1553
|
) -> Runnable[Input, Output]:
|
|
1541
|
-
"""Bind config to a Runnable
|
|
1554
|
+
"""Bind config to a ``Runnable``, returning a new ``Runnable``.
|
|
1542
1555
|
|
|
1543
1556
|
Args:
|
|
1544
|
-
config: The config to bind to the Runnable
|
|
1545
|
-
kwargs: Additional keyword arguments to pass to the Runnable
|
|
1557
|
+
config: The config to bind to the ``Runnable``.
|
|
1558
|
+
kwargs: Additional keyword arguments to pass to the ``Runnable``.
|
|
1546
1559
|
|
|
1547
1560
|
Returns:
|
|
1548
|
-
A new Runnable with the config bound.
|
|
1561
|
+
A new ``Runnable`` with the config bound.
|
|
1562
|
+
|
|
1549
1563
|
"""
|
|
1550
1564
|
return RunnableBinding(
|
|
1551
1565
|
bound=self,
|
|
@@ -1569,22 +1583,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1569
1583
|
Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
|
|
1570
1584
|
] = None,
|
|
1571
1585
|
) -> Runnable[Input, Output]:
|
|
1572
|
-
"""Bind lifecycle listeners to a Runnable
|
|
1586
|
+
"""Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
1573
1587
|
|
|
1574
|
-
The Run object contains information about the run, including its id
|
|
1575
|
-
type
|
|
1576
|
-
added to the run.
|
|
1588
|
+
The Run object contains information about the run, including its ``id``,
|
|
1589
|
+
``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and
|
|
1590
|
+
any tags or metadata added to the run.
|
|
1577
1591
|
|
|
1578
1592
|
Args:
|
|
1579
|
-
on_start: Called before the Runnable starts running, with the Run
|
|
1580
|
-
Defaults to None.
|
|
1581
|
-
on_end: Called after the Runnable finishes running, with the Run
|
|
1582
|
-
Defaults to None.
|
|
1583
|
-
on_error: Called if the Runnable throws an error, with the Run
|
|
1584
|
-
Defaults to None.
|
|
1593
|
+
on_start: Called before the ``Runnable`` starts running, with the ``Run``
|
|
1594
|
+
object. Defaults to None.
|
|
1595
|
+
on_end: Called after the ``Runnable`` finishes running, with the ``Run``
|
|
1596
|
+
object. Defaults to None.
|
|
1597
|
+
on_error: Called if the ``Runnable`` throws an error, with the ``Run``
|
|
1598
|
+
object. Defaults to None.
|
|
1585
1599
|
|
|
1586
1600
|
Returns:
|
|
1587
|
-
A new Runnable with the listeners bound.
|
|
1601
|
+
A new ``Runnable`` with the listeners bound.
|
|
1588
1602
|
|
|
1589
1603
|
Example:
|
|
1590
1604
|
|
|
@@ -1636,22 +1650,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1636
1650
|
on_end: Optional[AsyncListener] = None,
|
|
1637
1651
|
on_error: Optional[AsyncListener] = None,
|
|
1638
1652
|
) -> Runnable[Input, Output]:
|
|
1639
|
-
"""Bind async lifecycle listeners to a Runnable
|
|
1653
|
+
"""Bind async lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
1640
1654
|
|
|
1641
|
-
The Run object contains information about the run, including its id
|
|
1642
|
-
type
|
|
1643
|
-
added to the run.
|
|
1655
|
+
The Run object contains information about the run, including its ``id``,
|
|
1656
|
+
``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and
|
|
1657
|
+
any tags or metadata added to the run.
|
|
1644
1658
|
|
|
1645
1659
|
Args:
|
|
1646
|
-
on_start: Called asynchronously before the Runnable starts running,
|
|
1647
|
-
with the Run object. Defaults to None.
|
|
1648
|
-
on_end: Called asynchronously after the Runnable finishes running,
|
|
1649
|
-
with the Run object. Defaults to None.
|
|
1650
|
-
on_error: Called asynchronously if the Runnable throws an error,
|
|
1651
|
-
with the Run object. Defaults to None.
|
|
1660
|
+
on_start: Called asynchronously before the ``Runnable`` starts running,
|
|
1661
|
+
with the ``Run`` object. Defaults to None.
|
|
1662
|
+
on_end: Called asynchronously after the ``Runnable`` finishes running,
|
|
1663
|
+
with the ``Run`` object. Defaults to None.
|
|
1664
|
+
on_error: Called asynchronously if the ``Runnable`` throws an error,
|
|
1665
|
+
with the ``Run`` object. Defaults to None.
|
|
1652
1666
|
|
|
1653
1667
|
Returns:
|
|
1654
|
-
A new Runnable with the listeners bound.
|
|
1668
|
+
A new ``Runnable`` with the listeners bound.
|
|
1655
1669
|
|
|
1656
1670
|
Example:
|
|
1657
1671
|
|
|
@@ -1702,7 +1716,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1702
1716
|
on end callback ends at 2025-03-01T07:05:29.883893+00:00
|
|
1703
1717
|
on end callback ends at 2025-03-01T07:05:30.884831+00:00
|
|
1704
1718
|
|
|
1705
|
-
"""
|
|
1719
|
+
""" # noqa: E501
|
|
1706
1720
|
from langchain_core.tracers.root_listeners import AsyncRootListenersTracer
|
|
1707
1721
|
|
|
1708
1722
|
return RunnableBinding(
|
|
@@ -1727,11 +1741,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1727
1741
|
input_type: Optional[type[Input]] = None,
|
|
1728
1742
|
output_type: Optional[type[Output]] = None,
|
|
1729
1743
|
) -> Runnable[Input, Output]:
|
|
1730
|
-
"""Bind input and output types to a Runnable
|
|
1744
|
+
"""Bind input and output types to a ``Runnable``, returning a new ``Runnable``.
|
|
1731
1745
|
|
|
1732
1746
|
Args:
|
|
1733
|
-
input_type: The input type to bind to the Runnable
|
|
1734
|
-
output_type: The output type to bind to the Runnable
|
|
1747
|
+
input_type: The input type to bind to the ``Runnable``. Defaults to None.
|
|
1748
|
+
output_type: The output type to bind to the ``Runnable``. Defaults to None.
|
|
1735
1749
|
|
|
1736
1750
|
Returns:
|
|
1737
1751
|
A new Runnable with the types bound.
|
|
@@ -1810,12 +1824,12 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1810
1824
|
)
|
|
1811
1825
|
|
|
1812
1826
|
def map(self) -> Runnable[list[Input], list[Output]]:
|
|
1813
|
-
"""Return a new Runnable that maps a list of inputs to a list of outputs.
|
|
1827
|
+
"""Return a new ``Runnable`` that maps a list of inputs to a list of outputs.
|
|
1814
1828
|
|
|
1815
|
-
Calls invoke
|
|
1829
|
+
Calls ``invoke`` with each input.
|
|
1816
1830
|
|
|
1817
1831
|
Returns:
|
|
1818
|
-
A new Runnable that maps a list of inputs to a list of outputs.
|
|
1832
|
+
A new ``Runnable`` that maps a list of inputs to a list of outputs.
|
|
1819
1833
|
|
|
1820
1834
|
Example:
|
|
1821
1835
|
|
|
@@ -1839,22 +1853,22 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1839
1853
|
exceptions_to_handle: tuple[type[BaseException], ...] = (Exception,),
|
|
1840
1854
|
exception_key: Optional[str] = None,
|
|
1841
1855
|
) -> RunnableWithFallbacksT[Input, Output]:
|
|
1842
|
-
"""Add fallbacks to a Runnable
|
|
1856
|
+
"""Add fallbacks to a ``Runnable``, returning a new ``Runnable``.
|
|
1843
1857
|
|
|
1844
|
-
The new Runnable will try the original Runnable
|
|
1858
|
+
The new ``Runnable`` will try the original ``Runnable``, and then each fallback
|
|
1845
1859
|
in order, upon failures.
|
|
1846
1860
|
|
|
1847
1861
|
Args:
|
|
1848
|
-
fallbacks: A sequence of runnables to try if the original Runnable fails.
|
|
1862
|
+
fallbacks: A sequence of runnables to try if the original ``Runnable`` fails.
|
|
1849
1863
|
exceptions_to_handle: A tuple of exception types to handle.
|
|
1850
|
-
Defaults to (Exception,)
|
|
1864
|
+
Defaults to ``(Exception,)``.
|
|
1851
1865
|
exception_key: If string is specified then handled exceptions will be passed
|
|
1852
1866
|
to fallbacks as part of the input under the specified key. If None,
|
|
1853
|
-
exceptions will not be passed to fallbacks. If used, the base Runnable
|
|
1867
|
+
exceptions will not be passed to fallbacks. If used, the base ``Runnable``
|
|
1854
1868
|
and its fallbacks must accept a dictionary as input. Defaults to None.
|
|
1855
1869
|
|
|
1856
1870
|
Returns:
|
|
1857
|
-
A new Runnable that will try the original Runnable
|
|
1871
|
+
A new ``Runnable`` that will try the original ``Runnable``, and then each
|
|
1858
1872
|
fallback in order, upon failures.
|
|
1859
1873
|
|
|
1860
1874
|
Example:
|
|
@@ -1881,18 +1895,18 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1881
1895
|
print(''.join(runnable.stream({}))) #foo bar
|
|
1882
1896
|
|
|
1883
1897
|
Args:
|
|
1884
|
-
fallbacks: A sequence of runnables to try if the original Runnable fails.
|
|
1898
|
+
fallbacks: A sequence of runnables to try if the original ``Runnable`` fails.
|
|
1885
1899
|
exceptions_to_handle: A tuple of exception types to handle.
|
|
1886
1900
|
exception_key: If string is specified then handled exceptions will be passed
|
|
1887
1901
|
to fallbacks as part of the input under the specified key. If None,
|
|
1888
|
-
exceptions will not be passed to fallbacks. If used, the base Runnable
|
|
1902
|
+
exceptions will not be passed to fallbacks. If used, the base ``Runnable``
|
|
1889
1903
|
and its fallbacks must accept a dictionary as input.
|
|
1890
1904
|
|
|
1891
1905
|
Returns:
|
|
1892
|
-
A new Runnable that will try the original Runnable
|
|
1906
|
+
A new ``Runnable`` that will try the original ``Runnable``, and then each
|
|
1893
1907
|
fallback in order, upon failures.
|
|
1894
1908
|
|
|
1895
|
-
"""
|
|
1909
|
+
""" # noqa: E501
|
|
1896
1910
|
from langchain_core.runnables.fallbacks import RunnableWithFallbacks
|
|
1897
1911
|
|
|
1898
1912
|
return RunnableWithFallbacks(
|
|
@@ -1917,10 +1931,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1917
1931
|
serialized: Optional[dict[str, Any]] = None,
|
|
1918
1932
|
**kwargs: Optional[Any],
|
|
1919
1933
|
) -> Output:
|
|
1920
|
-
"""Helper method to transform an Input value to an Output value, with callbacks.
|
|
1934
|
+
"""Helper method to transform an ``Input`` value to an ``Output`` value, with callbacks.
|
|
1921
1935
|
|
|
1922
|
-
Use this method to implement invoke
|
|
1923
|
-
|
|
1936
|
+
Use this method to implement ``invoke`` in subclasses.
|
|
1937
|
+
|
|
1938
|
+
""" # noqa: E501
|
|
1924
1939
|
config = ensure_config(config)
|
|
1925
1940
|
callback_manager = get_callback_manager_for_config(config)
|
|
1926
1941
|
run_manager = callback_manager.on_chain_start(
|
|
@@ -1967,10 +1982,10 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1967
1982
|
serialized: Optional[dict[str, Any]] = None,
|
|
1968
1983
|
**kwargs: Optional[Any],
|
|
1969
1984
|
) -> Output:
|
|
1970
|
-
"""Helper method to transform an Input value to an Output value, with callbacks.
|
|
1985
|
+
"""Helper method to transform an ``Input`` value to an ``Output`` value, with callbacks.
|
|
1971
1986
|
|
|
1972
|
-
Use this method to implement ainvoke
|
|
1973
|
-
"""
|
|
1987
|
+
Use this method to implement ``ainvoke`` in subclasses.
|
|
1988
|
+
""" # noqa: E501
|
|
1974
1989
|
config = ensure_config(config)
|
|
1975
1990
|
callback_manager = get_async_callback_manager_for_config(config)
|
|
1976
1991
|
run_manager = await callback_manager.on_chain_start(
|
|
@@ -2016,8 +2031,9 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2016
2031
|
) -> list[Output]:
|
|
2017
2032
|
"""Transform a list of inputs to a list of outputs, with callbacks.
|
|
2018
2033
|
|
|
2019
|
-
Helper method to transform an Input value to an Output value,
|
|
2020
|
-
with callbacks. Use this method to implement invoke
|
|
2034
|
+
Helper method to transform an ``Input`` value to an ``Output`` value,
|
|
2035
|
+
with callbacks. Use this method to implement ``invoke`` in subclasses.
|
|
2036
|
+
|
|
2021
2037
|
"""
|
|
2022
2038
|
if not inputs:
|
|
2023
2039
|
return []
|
|
@@ -2089,9 +2105,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2089
2105
|
) -> list[Output]:
|
|
2090
2106
|
"""Transform a list of inputs to a list of outputs, with callbacks.
|
|
2091
2107
|
|
|
2092
|
-
Helper method to transform an Input value to an Output value,
|
|
2108
|
+
Helper method to transform an ``Input`` value to an ``Output`` value,
|
|
2093
2109
|
with callbacks.
|
|
2094
|
-
|
|
2110
|
+
|
|
2111
|
+
Use this method to implement ``invoke`` in subclasses.
|
|
2112
|
+
|
|
2095
2113
|
"""
|
|
2096
2114
|
if not inputs:
|
|
2097
2115
|
return []
|
|
@@ -2163,9 +2181,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2163
2181
|
) -> Iterator[Output]:
|
|
2164
2182
|
"""Transform a stream with config.
|
|
2165
2183
|
|
|
2166
|
-
Helper method to transform an Iterator of Input values into an
|
|
2167
|
-
Output values, with callbacks.
|
|
2168
|
-
|
|
2184
|
+
Helper method to transform an ``Iterator`` of ``Input`` values into an
|
|
2185
|
+
``Iterator`` of ``Output`` values, with callbacks.
|
|
2186
|
+
|
|
2187
|
+
Use this to implement ``stream`` or ``transform`` in ``Runnable`` subclasses.
|
|
2188
|
+
|
|
2169
2189
|
"""
|
|
2170
2190
|
# Mixin that is used by both astream log and astream events implementation
|
|
2171
2191
|
from langchain_core.tracers._streaming import _StreamingCallbackHandler
|
|
@@ -2267,9 +2287,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2267
2287
|
) -> AsyncIterator[Output]:
|
|
2268
2288
|
"""Transform a stream with config.
|
|
2269
2289
|
|
|
2270
|
-
Helper method to transform an Async Iterator of Input values into an
|
|
2271
|
-
Iterator of Output values, with callbacks.
|
|
2272
|
-
|
|
2290
|
+
Helper method to transform an Async ``Iterator`` of ``Input`` values into an
|
|
2291
|
+
Async ``Iterator`` of ``Output`` values, with callbacks.
|
|
2292
|
+
|
|
2293
|
+
Use this to implement ``astream`` or ``atransform`` in ``Runnable`` subclasses.
|
|
2294
|
+
|
|
2273
2295
|
"""
|
|
2274
2296
|
# Mixin that is used by both astream log and astream events implementation
|
|
2275
2297
|
from langchain_core.tracers._streaming import _StreamingCallbackHandler
|
|
@@ -2361,14 +2383,13 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2361
2383
|
name: Optional[str] = None,
|
|
2362
2384
|
description: Optional[str] = None,
|
|
2363
2385
|
arg_types: Optional[dict[str, type]] = None,
|
|
2364
|
-
message_version: Literal["v0", "v1"] = "v0",
|
|
2365
2386
|
) -> BaseTool:
|
|
2366
|
-
"""Create a BaseTool from a Runnable
|
|
2387
|
+
"""Create a ``BaseTool`` from a ``Runnable``.
|
|
2367
2388
|
|
|
2368
|
-
``as_tool`` will instantiate a BaseTool with a name, description, and
|
|
2369
|
-
``args_schema`` from a Runnable
|
|
2389
|
+
``as_tool`` will instantiate a ``BaseTool`` with a name, description, and
|
|
2390
|
+
``args_schema`` from a ``Runnable``. Where possible, schemas are inferred
|
|
2370
2391
|
from ``runnable.get_input_schema``. Alternatively (e.g., if the
|
|
2371
|
-
Runnable takes a dict as input and the specific dict keys are not typed),
|
|
2392
|
+
``Runnable`` takes a dict as input and the specific dict keys are not typed),
|
|
2372
2393
|
the schema can be specified directly with ``args_schema``. You can also
|
|
2373
2394
|
pass ``arg_types`` to just specify the required arguments and their types.
|
|
2374
2395
|
|
|
@@ -2377,14 +2398,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2377
2398
|
name: The name of the tool. Defaults to None.
|
|
2378
2399
|
description: The description of the tool. Defaults to None.
|
|
2379
2400
|
arg_types: A dictionary of argument names to types. Defaults to None.
|
|
2380
|
-
message_version: Version of ToolMessage to return given
|
|
2381
|
-
:class:`~langchain_core.messages.
|
|
2382
|
-
|
|
2383
|
-
If ``"v0"``, output will be a v0 :class:`~langchain_core.messages.tool.ToolMessage`.
|
|
2384
|
-
If ``"v1"``, output will be a v1 :class:`~langchain_core.messages.v1.ToolMessage`.
|
|
2401
|
+
message_version: Version of ``ToolMessage`` to return given
|
|
2402
|
+
:class:`~langchain_core.messages.content.ToolCall` input.
|
|
2385
2403
|
|
|
2386
2404
|
Returns:
|
|
2387
|
-
A BaseTool instance.
|
|
2405
|
+
A ``BaseTool`` instance.
|
|
2388
2406
|
|
|
2389
2407
|
Typed dict input:
|
|
2390
2408
|
|
|
@@ -2457,7 +2475,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2457
2475
|
|
|
2458
2476
|
.. versionadded:: 0.2.14
|
|
2459
2477
|
|
|
2460
|
-
"""
|
|
2478
|
+
"""
|
|
2461
2479
|
# Avoid circular import
|
|
2462
2480
|
from langchain_core.tools import convert_runnable_to_tool
|
|
2463
2481
|
|
|
@@ -2467,7 +2485,6 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2467
2485
|
name=name,
|
|
2468
2486
|
description=description,
|
|
2469
2487
|
arg_types=arg_types,
|
|
2470
|
-
message_version=message_version,
|
|
2471
2488
|
)
|
|
2472
2489
|
|
|
2473
2490
|
|
|
@@ -2484,10 +2501,11 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2484
2501
|
|
|
2485
2502
|
@override
|
|
2486
2503
|
def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]:
|
|
2487
|
-
"""Serialize the Runnable to JSON.
|
|
2504
|
+
"""Serialize the ``Runnable`` to JSON.
|
|
2488
2505
|
|
|
2489
2506
|
Returns:
|
|
2490
|
-
A JSON-serializable representation of the Runnable
|
|
2507
|
+
A JSON-serializable representation of the ``Runnable``.
|
|
2508
|
+
|
|
2491
2509
|
"""
|
|
2492
2510
|
dumped = super().to_json()
|
|
2493
2511
|
with contextlib.suppress(Exception):
|
|
@@ -2497,13 +2515,13 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2497
2515
|
def configurable_fields(
|
|
2498
2516
|
self, **kwargs: AnyConfigurableField
|
|
2499
2517
|
) -> RunnableSerializable[Input, Output]:
|
|
2500
|
-
"""Configure particular Runnable fields at runtime.
|
|
2518
|
+
"""Configure particular ``Runnable`` fields at runtime.
|
|
2501
2519
|
|
|
2502
2520
|
Args:
|
|
2503
|
-
**kwargs: A dictionary of ConfigurableField instances to configure.
|
|
2521
|
+
**kwargs: A dictionary of ``ConfigurableField`` instances to configure.
|
|
2504
2522
|
|
|
2505
2523
|
Returns:
|
|
2506
|
-
A new Runnable with the fields configured.
|
|
2524
|
+
A new ``Runnable`` with the fields configured.
|
|
2507
2525
|
|
|
2508
2526
|
.. code-block:: python
|
|
2509
2527
|
|
|
@@ -2552,20 +2570,20 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2552
2570
|
prefix_keys: bool = False,
|
|
2553
2571
|
**kwargs: Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]],
|
|
2554
2572
|
) -> RunnableSerializable[Input, Output]:
|
|
2555
|
-
"""Configure alternatives for Runnables that can be set at runtime.
|
|
2573
|
+
"""Configure alternatives for ``Runnables`` that can be set at runtime.
|
|
2556
2574
|
|
|
2557
2575
|
Args:
|
|
2558
|
-
which: The ConfigurableField instance that will be used to select the
|
|
2576
|
+
which: The ``ConfigurableField`` instance that will be used to select the
|
|
2559
2577
|
alternative.
|
|
2560
2578
|
default_key: The default key to use if no alternative is selected.
|
|
2561
2579
|
Defaults to ``'default'``.
|
|
2562
|
-
prefix_keys: Whether to prefix the keys with the ConfigurableField id.
|
|
2580
|
+
prefix_keys: Whether to prefix the keys with the ``ConfigurableField`` id.
|
|
2563
2581
|
Defaults to False.
|
|
2564
|
-
**kwargs: A dictionary of keys to Runnable instances or callables that
|
|
2565
|
-
return Runnable instances.
|
|
2582
|
+
**kwargs: A dictionary of keys to ``Runnable`` instances or callables that
|
|
2583
|
+
return ``Runnable`` instances.
|
|
2566
2584
|
|
|
2567
2585
|
Returns:
|
|
2568
|
-
A new Runnable with the alternatives configured.
|
|
2586
|
+
A new ``Runnable`` with the alternatives configured.
|
|
2569
2587
|
|
|
2570
2588
|
.. code-block:: python
|
|
2571
2589
|
|
|
@@ -2679,25 +2697,26 @@ def _seq_output_schema(
|
|
|
2679
2697
|
|
|
2680
2698
|
|
|
2681
2699
|
class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
2682
|
-
"""Sequence of Runnables
|
|
2700
|
+
"""Sequence of ``Runnables``, where the output of each is the input of the next.
|
|
2683
2701
|
|
|
2684
|
-
|
|
2702
|
+
**``RunnableSequence``** is the most important composition operator in LangChain
|
|
2685
2703
|
as it is used in virtually every chain.
|
|
2686
2704
|
|
|
2687
|
-
A RunnableSequence can be instantiated directly or more commonly by using the
|
|
2688
|
-
operator where either the left or right operands (or both) must be a
|
|
2705
|
+
A ``RunnableSequence`` can be instantiated directly or more commonly by using the
|
|
2706
|
+
``|`` operator where either the left or right operands (or both) must be a
|
|
2707
|
+
``Runnable``.
|
|
2689
2708
|
|
|
2690
|
-
Any RunnableSequence automatically supports sync, async, batch.
|
|
2709
|
+
Any ``RunnableSequence`` automatically supports sync, async, batch.
|
|
2691
2710
|
|
|
2692
|
-
The default implementations of
|
|
2693
|
-
asyncio gather and will be faster than naive invocation of invoke or ainvoke
|
|
2694
|
-
for IO bound
|
|
2711
|
+
The default implementations of ``batch`` and ``abatch`` utilize threadpools and
|
|
2712
|
+
asyncio gather and will be faster than naive invocation of ``invoke`` or ``ainvoke``
|
|
2713
|
+
for IO bound ``Runnable``s.
|
|
2695
2714
|
|
|
2696
2715
|
Batching is implemented by invoking the batch method on each component of the
|
|
2697
|
-
RunnableSequence in order.
|
|
2716
|
+
``RunnableSequence`` in order.
|
|
2698
2717
|
|
|
2699
|
-
A RunnableSequence preserves the streaming properties of its components, so if
|
|
2700
|
-
components of the sequence implement a
|
|
2718
|
+
A ``RunnableSequence`` preserves the streaming properties of its components, so if
|
|
2719
|
+
all components of the sequence implement a ``transform`` method -- which
|
|
2701
2720
|
is the method that implements the logic to map a streaming input to a streaming
|
|
2702
2721
|
output -- then the sequence will be able to stream input to output!
|
|
2703
2722
|
|
|
@@ -2705,15 +2724,16 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2705
2724
|
streaming will only begin after this component is run. If there are
|
|
2706
2725
|
multiple blocking components, streaming begins after the last one.
|
|
2707
2726
|
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2727
|
+
.. note::
|
|
2728
|
+
``RunnableLambdas`` do not support ``transform`` by default! So if you need to
|
|
2729
|
+
use a ``RunnableLambdas`` be careful about where you place them in a
|
|
2730
|
+
``RunnableSequence`` (if you need to use the ``stream``/``astream`` methods).
|
|
2711
2731
|
|
|
2712
2732
|
If you need arbitrary logic and need streaming, you can subclass
|
|
2713
|
-
Runnable, and implement
|
|
2733
|
+
Runnable, and implement ``transform`` for whatever logic you need.
|
|
2714
2734
|
|
|
2715
2735
|
Here is a simple example that uses simple functions to illustrate the use of
|
|
2716
|
-
RunnableSequence
|
|
2736
|
+
``RunnableSequence``:
|
|
2717
2737
|
|
|
2718
2738
|
.. code-block:: python
|
|
2719
2739
|
|
|
@@ -2762,11 +2782,11 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2762
2782
|
# purposes. It allows specifying the `Input` on the first type, the `Output` of
|
|
2763
2783
|
# the last type.
|
|
2764
2784
|
first: Runnable[Input, Any]
|
|
2765
|
-
"""The first Runnable in the sequence."""
|
|
2785
|
+
"""The first ``Runnable`` in the sequence."""
|
|
2766
2786
|
middle: list[Runnable[Any, Any]] = Field(default_factory=list)
|
|
2767
|
-
"""The middle
|
|
2787
|
+
"""The middle ``Runnable`` in the sequence."""
|
|
2768
2788
|
last: Runnable[Any, Output]
|
|
2769
|
-
"""The last Runnable in the sequence."""
|
|
2789
|
+
"""The last ``Runnable`` in the sequence."""
|
|
2770
2790
|
|
|
2771
2791
|
def __init__(
|
|
2772
2792
|
self,
|
|
@@ -2776,13 +2796,13 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2776
2796
|
middle: Optional[list[Runnable[Any, Any]]] = None,
|
|
2777
2797
|
last: Optional[Runnable[Any, Any]] = None,
|
|
2778
2798
|
) -> None:
|
|
2779
|
-
"""Create a new RunnableSequence
|
|
2799
|
+
"""Create a new ``RunnableSequence``.
|
|
2780
2800
|
|
|
2781
2801
|
Args:
|
|
2782
2802
|
steps: The steps to include in the sequence.
|
|
2783
|
-
name: The name of the Runnable
|
|
2784
|
-
first: The first Runnable in the sequence. Defaults to None.
|
|
2785
|
-
middle: The middle Runnables in the sequence. Defaults to None.
|
|
2803
|
+
name: The name of the ``Runnable``. Defaults to None.
|
|
2804
|
+
first: The first ``Runnable`` in the sequence. Defaults to None.
|
|
2805
|
+
middle: The middle ``Runnables`` in the sequence. Defaults to None.
|
|
2786
2806
|
last: The last Runnable in the sequence. Defaults to None.
|
|
2787
2807
|
|
|
2788
2808
|
Raises:
|
|
@@ -2799,7 +2819,7 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2799
2819
|
if len(steps_flat) < 2:
|
|
2800
2820
|
msg = f"RunnableSequence must have at least 2 steps, got {len(steps_flat)}"
|
|
2801
2821
|
raise ValueError(msg)
|
|
2802
|
-
super().__init__(
|
|
2822
|
+
super().__init__(
|
|
2803
2823
|
first=steps_flat[0],
|
|
2804
2824
|
middle=list(steps_flat[1:-1]),
|
|
2805
2825
|
last=steps_flat[-1],
|
|
@@ -2813,10 +2833,10 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2813
2833
|
|
|
2814
2834
|
@property
|
|
2815
2835
|
def steps(self) -> list[Runnable[Any, Any]]:
|
|
2816
|
-
"""All the
|
|
2836
|
+
"""All the ``Runnable``s that make up the sequence in order.
|
|
2817
2837
|
|
|
2818
2838
|
Returns:
|
|
2819
|
-
A list of
|
|
2839
|
+
A list of ``Runnable``s.
|
|
2820
2840
|
"""
|
|
2821
2841
|
return [self.first, *self.middle, self.last]
|
|
2822
2842
|
|
|
@@ -2827,7 +2847,9 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2827
2847
|
|
|
2828
2848
|
Returns:
|
|
2829
2849
|
True if the object is serializable, False otherwise.
|
|
2830
|
-
|
|
2850
|
+
|
|
2851
|
+
Defaults to True.
|
|
2852
|
+
|
|
2831
2853
|
"""
|
|
2832
2854
|
return True
|
|
2833
2855
|
|
|
@@ -2838,26 +2860,27 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2838
2860
|
@property
|
|
2839
2861
|
@override
|
|
2840
2862
|
def InputType(self) -> type[Input]:
|
|
2841
|
-
"""The type of the input to the Runnable
|
|
2863
|
+
"""The type of the input to the ``Runnable``."""
|
|
2842
2864
|
return self.first.InputType
|
|
2843
2865
|
|
|
2844
2866
|
@property
|
|
2845
2867
|
@override
|
|
2846
2868
|
def OutputType(self) -> type[Output]:
|
|
2847
|
-
"""The type of the output of the Runnable
|
|
2869
|
+
"""The type of the output of the ``Runnable``."""
|
|
2848
2870
|
return self.last.OutputType
|
|
2849
2871
|
|
|
2850
2872
|
@override
|
|
2851
2873
|
def get_input_schema(
|
|
2852
2874
|
self, config: Optional[RunnableConfig] = None
|
|
2853
2875
|
) -> type[BaseModel]:
|
|
2854
|
-
"""Get the input schema of the Runnable
|
|
2876
|
+
"""Get the input schema of the ``Runnable``.
|
|
2855
2877
|
|
|
2856
2878
|
Args:
|
|
2857
2879
|
config: The config to use. Defaults to None.
|
|
2858
2880
|
|
|
2859
2881
|
Returns:
|
|
2860
|
-
The input schema of the Runnable
|
|
2882
|
+
The input schema of the ``Runnable``.
|
|
2883
|
+
|
|
2861
2884
|
"""
|
|
2862
2885
|
return _seq_input_schema(self.steps, config)
|
|
2863
2886
|
|
|
@@ -2865,23 +2888,25 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2865
2888
|
def get_output_schema(
|
|
2866
2889
|
self, config: Optional[RunnableConfig] = None
|
|
2867
2890
|
) -> type[BaseModel]:
|
|
2868
|
-
"""Get the output schema of the Runnable
|
|
2891
|
+
"""Get the output schema of the ``Runnable``.
|
|
2869
2892
|
|
|
2870
2893
|
Args:
|
|
2871
2894
|
config: The config to use. Defaults to None.
|
|
2872
2895
|
|
|
2873
2896
|
Returns:
|
|
2874
|
-
The output schema of the Runnable
|
|
2897
|
+
The output schema of the ``Runnable``.
|
|
2898
|
+
|
|
2875
2899
|
"""
|
|
2876
2900
|
return _seq_output_schema(self.steps, config)
|
|
2877
2901
|
|
|
2878
2902
|
@property
|
|
2879
2903
|
@override
|
|
2880
2904
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
2881
|
-
"""Get the config specs of the Runnable
|
|
2905
|
+
"""Get the config specs of the ``Runnable``.
|
|
2882
2906
|
|
|
2883
2907
|
Returns:
|
|
2884
|
-
The config specs of the Runnable
|
|
2908
|
+
The config specs of the ``Runnable``.
|
|
2909
|
+
|
|
2885
2910
|
"""
|
|
2886
2911
|
from langchain_core.beta.runnables.context import (
|
|
2887
2912
|
CONTEXT_CONFIG_PREFIX,
|
|
@@ -2929,16 +2954,17 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2929
2954
|
|
|
2930
2955
|
@override
|
|
2931
2956
|
def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph:
|
|
2932
|
-
"""Get the graph representation of the Runnable
|
|
2957
|
+
"""Get the graph representation of the ``Runnable``.
|
|
2933
2958
|
|
|
2934
2959
|
Args:
|
|
2935
2960
|
config: The config to use. Defaults to None.
|
|
2936
2961
|
|
|
2937
2962
|
Returns:
|
|
2938
|
-
The graph representation of the Runnable
|
|
2963
|
+
The graph representation of the ``Runnable``.
|
|
2939
2964
|
|
|
2940
2965
|
Raises:
|
|
2941
|
-
ValueError: If a Runnable has no first or last node.
|
|
2966
|
+
ValueError: If a ``Runnable`` has no first or last node.
|
|
2967
|
+
|
|
2942
2968
|
"""
|
|
2943
2969
|
from langchain_core.runnables.graph import Graph
|
|
2944
2970
|
|
|
@@ -3476,19 +3502,19 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
3476
3502
|
|
|
3477
3503
|
|
|
3478
3504
|
class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
3479
|
-
"""Runnable that runs a mapping of
|
|
3505
|
+
"""Runnable that runs a mapping of ``Runnable``s in parallel.
|
|
3480
3506
|
|
|
3481
3507
|
Returns a mapping of their outputs.
|
|
3482
3508
|
|
|
3483
|
-
RunnableParallel is one of the two main composition primitives for the LCEL,
|
|
3484
|
-
alongside RunnableSequence
|
|
3485
|
-
input to each.
|
|
3509
|
+
``RunnableParallel`` is one of the two main composition primitives for the LCEL,
|
|
3510
|
+
alongside ``RunnableSequence``. It invokes ``Runnable``s concurrently, providing the
|
|
3511
|
+
same input to each.
|
|
3486
3512
|
|
|
3487
|
-
A RunnableParallel can be instantiated directly or by using a dict literal
|
|
3488
|
-
sequence.
|
|
3513
|
+
A ``RunnableParallel`` can be instantiated directly or by using a dict literal
|
|
3514
|
+
within a sequence.
|
|
3489
3515
|
|
|
3490
3516
|
Here is a simple example that uses functions to illustrate the use of
|
|
3491
|
-
RunnableParallel
|
|
3517
|
+
``RunnableParallel``:
|
|
3492
3518
|
|
|
3493
3519
|
.. code-block:: python
|
|
3494
3520
|
|
|
@@ -3527,8 +3553,8 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3527
3553
|
sequence.batch([1, 2, 3])
|
|
3528
3554
|
await sequence.abatch([1, 2, 3])
|
|
3529
3555
|
|
|
3530
|
-
RunnableParallel makes it easy to run
|
|
3531
|
-
we simultaneously stream output from two different Runnables
|
|
3556
|
+
``RunnableParallel`` makes it easy to run ``Runnable``s in parallel. In the below
|
|
3557
|
+
example, we simultaneously stream output from two different ``Runnables``:
|
|
3532
3558
|
|
|
3533
3559
|
.. code-block:: python
|
|
3534
3560
|
|
|
@@ -3577,15 +3603,16 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3577
3603
|
Mapping[str, Union[Runnable[Input, Any], Callable[[Input], Any]]],
|
|
3578
3604
|
],
|
|
3579
3605
|
) -> None:
|
|
3580
|
-
"""Create a RunnableParallel
|
|
3606
|
+
"""Create a ``RunnableParallel``.
|
|
3581
3607
|
|
|
3582
3608
|
Args:
|
|
3583
3609
|
steps__: The steps to include. Defaults to None.
|
|
3584
3610
|
**kwargs: Additional steps to include.
|
|
3611
|
+
|
|
3585
3612
|
"""
|
|
3586
3613
|
merged = {**steps__} if steps__ is not None else {}
|
|
3587
3614
|
merged.update(kwargs)
|
|
3588
|
-
super().__init__(
|
|
3615
|
+
super().__init__(
|
|
3589
3616
|
steps__={key: coerce_to_runnable(r) for key, r in merged.items()}
|
|
3590
3617
|
)
|
|
3591
3618
|
|
|
@@ -3607,14 +3634,15 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3607
3634
|
def get_name(
|
|
3608
3635
|
self, suffix: Optional[str] = None, *, name: Optional[str] = None
|
|
3609
3636
|
) -> str:
|
|
3610
|
-
"""Get the name of the Runnable
|
|
3637
|
+
"""Get the name of the ``Runnable``.
|
|
3611
3638
|
|
|
3612
3639
|
Args:
|
|
3613
3640
|
suffix: The suffix to use. Defaults to None.
|
|
3614
3641
|
name: The name to use. Defaults to None.
|
|
3615
3642
|
|
|
3616
3643
|
Returns:
|
|
3617
|
-
The name of the Runnable
|
|
3644
|
+
The name of the ``Runnable``.
|
|
3645
|
+
|
|
3618
3646
|
"""
|
|
3619
3647
|
name = name or self.name or f"RunnableParallel<{','.join(self.steps__.keys())}>"
|
|
3620
3648
|
return super().get_name(suffix, name=name)
|
|
@@ -3622,7 +3650,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3622
3650
|
@property
|
|
3623
3651
|
@override
|
|
3624
3652
|
def InputType(self) -> Any:
|
|
3625
|
-
"""The type of the input to the Runnable
|
|
3653
|
+
"""The type of the input to the ``Runnable``."""
|
|
3626
3654
|
for step in self.steps__.values():
|
|
3627
3655
|
if step.InputType:
|
|
3628
3656
|
return step.InputType
|
|
@@ -3633,13 +3661,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3633
3661
|
def get_input_schema(
|
|
3634
3662
|
self, config: Optional[RunnableConfig] = None
|
|
3635
3663
|
) -> type[BaseModel]:
|
|
3636
|
-
"""Get the input schema of the Runnable
|
|
3664
|
+
"""Get the input schema of the ``Runnable``.
|
|
3637
3665
|
|
|
3638
3666
|
Args:
|
|
3639
3667
|
config: The config to use. Defaults to None.
|
|
3640
3668
|
|
|
3641
3669
|
Returns:
|
|
3642
|
-
The input schema of the Runnable
|
|
3670
|
+
The input schema of the ``Runnable``.
|
|
3671
|
+
|
|
3643
3672
|
"""
|
|
3644
3673
|
if all(
|
|
3645
3674
|
s.get_input_schema(config).model_json_schema().get("type", "object")
|
|
@@ -3663,13 +3692,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3663
3692
|
def get_output_schema(
|
|
3664
3693
|
self, config: Optional[RunnableConfig] = None
|
|
3665
3694
|
) -> type[BaseModel]:
|
|
3666
|
-
"""Get the output schema of the Runnable
|
|
3695
|
+
"""Get the output schema of the ``Runnable``.
|
|
3667
3696
|
|
|
3668
3697
|
Args:
|
|
3669
3698
|
config: The config to use. Defaults to None.
|
|
3670
3699
|
|
|
3671
3700
|
Returns:
|
|
3672
|
-
The output schema of the Runnable
|
|
3701
|
+
The output schema of the ``Runnable``.
|
|
3702
|
+
|
|
3673
3703
|
"""
|
|
3674
3704
|
fields = {k: (v.OutputType, ...) for k, v in self.steps__.items()}
|
|
3675
3705
|
return create_model_v2(self.get_name("Output"), field_definitions=fields)
|
|
@@ -3677,10 +3707,11 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3677
3707
|
@property
|
|
3678
3708
|
@override
|
|
3679
3709
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
3680
|
-
"""Get the config specs of the Runnable
|
|
3710
|
+
"""Get the config specs of the ``Runnable``.
|
|
3681
3711
|
|
|
3682
3712
|
Returns:
|
|
3683
|
-
The config specs of the Runnable
|
|
3713
|
+
The config specs of the ``Runnable``.
|
|
3714
|
+
|
|
3684
3715
|
"""
|
|
3685
3716
|
return get_unique_config_specs(
|
|
3686
3717
|
spec for step in self.steps__.values() for spec in step.config_specs
|
|
@@ -3688,16 +3719,17 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3688
3719
|
|
|
3689
3720
|
@override
|
|
3690
3721
|
def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph:
|
|
3691
|
-
"""Get the graph representation of the Runnable
|
|
3722
|
+
"""Get the graph representation of the ``Runnable``.
|
|
3692
3723
|
|
|
3693
3724
|
Args:
|
|
3694
3725
|
config: The config to use. Defaults to None.
|
|
3695
3726
|
|
|
3696
3727
|
Returns:
|
|
3697
|
-
The graph representation of the Runnable
|
|
3728
|
+
The graph representation of the ``Runnable``.
|
|
3698
3729
|
|
|
3699
3730
|
Raises:
|
|
3700
|
-
ValueError: If a Runnable has no first or last node.
|
|
3731
|
+
ValueError: If a ``Runnable`` has no first or last node.
|
|
3732
|
+
|
|
3701
3733
|
"""
|
|
3702
3734
|
from langchain_core.runnables.graph import Graph
|
|
3703
3735
|
|
|
@@ -3994,22 +4026,24 @@ RunnableMap = RunnableParallel
|
|
|
3994
4026
|
|
|
3995
4027
|
|
|
3996
4028
|
class RunnableGenerator(Runnable[Input, Output]):
|
|
3997
|
-
"""Runnable that runs a generator function.
|
|
4029
|
+
"""``Runnable`` that runs a generator function.
|
|
3998
4030
|
|
|
3999
|
-
|
|
4031
|
+
``RunnableGenerator``s can be instantiated directly or by using a generator within
|
|
4000
4032
|
a sequence.
|
|
4001
4033
|
|
|
4002
|
-
|
|
4003
|
-
parsers, while preserving streaming capabilities. Given a generator function
|
|
4004
|
-
a signature Iterator[A] -> Iterator[B]
|
|
4005
|
-
it to emit output chunks as soon as they are streamed
|
|
4034
|
+
``RunnableGenerator``s can be used to implement custom behavior, such as custom
|
|
4035
|
+
output parsers, while preserving streaming capabilities. Given a generator function
|
|
4036
|
+
with a signature ``Iterator[A] -> Iterator[B]``, wrapping it in a
|
|
4037
|
+
``RunnableGenerator`` allows it to emit output chunks as soon as they are streamed
|
|
4038
|
+
in from the previous step.
|
|
4006
4039
|
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4040
|
+
.. note::
|
|
4041
|
+
If a generator function has a ``signature A -> Iterator[B]``, such that it
|
|
4042
|
+
requires its input from the previous step to be completed before emitting chunks
|
|
4043
|
+
(e.g., most LLMs need the entire prompt available to start generating), it can
|
|
4044
|
+
instead be wrapped in a ``RunnableLambda``.
|
|
4011
4045
|
|
|
4012
|
-
Here is an example to show the basic mechanics of a RunnableGenerator
|
|
4046
|
+
Here is an example to show the basic mechanics of a ``RunnableGenerator``:
|
|
4013
4047
|
|
|
4014
4048
|
.. code-block:: python
|
|
4015
4049
|
|
|
@@ -4038,7 +4072,7 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4038
4072
|
await runnable.ainvoke(None) # "Have a nice day"
|
|
4039
4073
|
[p async for p in runnable.astream(None)] # ["Have", " a", " nice", " day"]
|
|
4040
4074
|
|
|
4041
|
-
RunnableGenerator makes it easy to implement custom behavior within a streaming
|
|
4075
|
+
``RunnableGenerator`` makes it easy to implement custom behavior within a streaming
|
|
4042
4076
|
context. Below we show an example:
|
|
4043
4077
|
|
|
4044
4078
|
.. code-block:: python
|
|
@@ -4092,15 +4126,16 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4092
4126
|
*,
|
|
4093
4127
|
name: Optional[str] = None,
|
|
4094
4128
|
) -> None:
|
|
4095
|
-
"""Initialize a RunnableGenerator
|
|
4129
|
+
"""Initialize a ``RunnableGenerator``.
|
|
4096
4130
|
|
|
4097
4131
|
Args:
|
|
4098
4132
|
transform: The transform function.
|
|
4099
4133
|
atransform: The async transform function. Defaults to None.
|
|
4100
|
-
name: The name of the Runnable
|
|
4134
|
+
name: The name of the ``Runnable``. Defaults to None.
|
|
4101
4135
|
|
|
4102
4136
|
Raises:
|
|
4103
4137
|
TypeError: If the transform is not a generator function.
|
|
4138
|
+
|
|
4104
4139
|
"""
|
|
4105
4140
|
if atransform is not None:
|
|
4106
4141
|
self._atransform = atransform
|
|
@@ -4295,12 +4330,12 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4295
4330
|
|
|
4296
4331
|
|
|
4297
4332
|
class RunnableLambda(Runnable[Input, Output]):
|
|
4298
|
-
"""RunnableLambda converts a python callable into a Runnable
|
|
4333
|
+
"""``RunnableLambda`` converts a python callable into a ``Runnable``.
|
|
4299
4334
|
|
|
4300
|
-
Wrapping a callable in a RunnableLambda makes the callable usable
|
|
4335
|
+
Wrapping a callable in a ``RunnableLambda`` makes the callable usable
|
|
4301
4336
|
within either a sync or async context.
|
|
4302
4337
|
|
|
4303
|
-
RunnableLambda can be composed as any other Runnable and provides
|
|
4338
|
+
``RunnableLambda`` can be composed as any other ``Runnable`` and provides
|
|
4304
4339
|
seamless integration with LangChain tracing.
|
|
4305
4340
|
|
|
4306
4341
|
``RunnableLambda`` is best suited for code that does not need to support
|
|
@@ -4377,7 +4412,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4377
4412
|
] = None,
|
|
4378
4413
|
name: Optional[str] = None,
|
|
4379
4414
|
) -> None:
|
|
4380
|
-
"""Create a RunnableLambda from a callable, and async callable or both.
|
|
4415
|
+
"""Create a ``RunnableLambda`` from a callable, and async callable or both.
|
|
4381
4416
|
|
|
4382
4417
|
Accepts both sync and async variants to allow providing efficient
|
|
4383
4418
|
implementations for sync and async execution.
|
|
@@ -4386,11 +4421,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4386
4421
|
func: Either sync or async callable
|
|
4387
4422
|
afunc: An async callable that takes an input and returns an output.
|
|
4388
4423
|
Defaults to None.
|
|
4389
|
-
name: The name of the Runnable
|
|
4424
|
+
name: The name of the ``Runnable``. Defaults to None.
|
|
4390
4425
|
|
|
4391
4426
|
Raises:
|
|
4392
|
-
TypeError: If the func is not a callable type.
|
|
4393
|
-
TypeError: If both func and afunc are provided.
|
|
4427
|
+
TypeError: If the ``func`` is not a callable type.
|
|
4428
|
+
TypeError: If both ``func`` and ``afunc`` are provided.
|
|
4429
|
+
|
|
4394
4430
|
"""
|
|
4395
4431
|
if afunc is not None:
|
|
4396
4432
|
self.afunc = afunc
|
|
@@ -4429,7 +4465,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4429
4465
|
@property
|
|
4430
4466
|
@override
|
|
4431
4467
|
def InputType(self) -> Any:
|
|
4432
|
-
"""The type of the input to this Runnable
|
|
4468
|
+
"""The type of the input to this ``Runnable``."""
|
|
4433
4469
|
func = getattr(self, "func", None) or self.afunc
|
|
4434
4470
|
try:
|
|
4435
4471
|
params = inspect.signature(func).parameters
|
|
@@ -4444,13 +4480,14 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4444
4480
|
def get_input_schema(
|
|
4445
4481
|
self, config: Optional[RunnableConfig] = None
|
|
4446
4482
|
) -> type[BaseModel]:
|
|
4447
|
-
"""The pydantic schema for the input to this Runnable
|
|
4483
|
+
"""The pydantic schema for the input to this ``Runnable``.
|
|
4448
4484
|
|
|
4449
4485
|
Args:
|
|
4450
4486
|
config: The config to use. Defaults to None.
|
|
4451
4487
|
|
|
4452
4488
|
Returns:
|
|
4453
|
-
The input schema for this Runnable
|
|
4489
|
+
The input schema for this ``Runnable``.
|
|
4490
|
+
|
|
4454
4491
|
"""
|
|
4455
4492
|
func = getattr(self, "func", None) or self.afunc
|
|
4456
4493
|
|
|
@@ -4488,10 +4525,11 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4488
4525
|
@property
|
|
4489
4526
|
@override
|
|
4490
4527
|
def OutputType(self) -> Any:
|
|
4491
|
-
"""The type of the output of this Runnable as a type annotation.
|
|
4528
|
+
"""The type of the output of this ``Runnable`` as a type annotation.
|
|
4492
4529
|
|
|
4493
4530
|
Returns:
|
|
4494
|
-
The type of the output of this Runnable
|
|
4531
|
+
The type of the output of this ``Runnable``.
|
|
4532
|
+
|
|
4495
4533
|
"""
|
|
4496
4534
|
func = getattr(self, "func", None) or self.afunc
|
|
4497
4535
|
try:
|
|
@@ -4537,11 +4575,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4537
4575
|
|
|
4538
4576
|
@functools.cached_property
|
|
4539
4577
|
def deps(self) -> list[Runnable]:
|
|
4540
|
-
"""The dependencies of this Runnable
|
|
4578
|
+
"""The dependencies of this ``Runnable``.
|
|
4541
4579
|
|
|
4542
4580
|
Returns:
|
|
4543
|
-
The dependencies of this Runnable
|
|
4544
|
-
variables that are
|
|
4581
|
+
The dependencies of this ``Runnable``. If the function has nonlocal
|
|
4582
|
+
variables that are ``Runnable``s, they are considered dependencies.
|
|
4583
|
+
|
|
4545
4584
|
"""
|
|
4546
4585
|
if hasattr(self, "func"):
|
|
4547
4586
|
objects = get_function_nonlocals(self.func)
|
|
@@ -4605,7 +4644,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4605
4644
|
__hash__ = None # type: ignore[assignment]
|
|
4606
4645
|
|
|
4607
4646
|
def __repr__(self) -> str:
|
|
4608
|
-
"""A string representation of this Runnable
|
|
4647
|
+
"""A string representation of this ``Runnable``."""
|
|
4609
4648
|
if self._repr is None:
|
|
4610
4649
|
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
|
4611
4650
|
self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
|
@@ -4771,18 +4810,19 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4771
4810
|
config: Optional[RunnableConfig] = None,
|
|
4772
4811
|
**kwargs: Optional[Any],
|
|
4773
4812
|
) -> Output:
|
|
4774
|
-
"""Invoke this Runnable synchronously.
|
|
4813
|
+
"""Invoke this ``Runnable`` synchronously.
|
|
4775
4814
|
|
|
4776
4815
|
Args:
|
|
4777
|
-
input: The input to this Runnable
|
|
4816
|
+
input: The input to this ``Runnable``.
|
|
4778
4817
|
config: The config to use. Defaults to None.
|
|
4779
4818
|
kwargs: Additional keyword arguments.
|
|
4780
4819
|
|
|
4781
4820
|
Returns:
|
|
4782
|
-
The output of this Runnable
|
|
4821
|
+
The output of this ``Runnable``.
|
|
4783
4822
|
|
|
4784
4823
|
Raises:
|
|
4785
|
-
TypeError: If the Runnable is a coroutine function.
|
|
4824
|
+
TypeError: If the ``Runnable`` is a coroutine function.
|
|
4825
|
+
|
|
4786
4826
|
"""
|
|
4787
4827
|
if hasattr(self, "func"):
|
|
4788
4828
|
return self._call_with_config(
|
|
@@ -4801,15 +4841,16 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4801
4841
|
config: Optional[RunnableConfig] = None,
|
|
4802
4842
|
**kwargs: Optional[Any],
|
|
4803
4843
|
) -> Output:
|
|
4804
|
-
"""Invoke this Runnable asynchronously.
|
|
4844
|
+
"""Invoke this ``Runnable`` asynchronously.
|
|
4805
4845
|
|
|
4806
4846
|
Args:
|
|
4807
|
-
input: The input to this Runnable
|
|
4847
|
+
input: The input to this ``Runnable``.
|
|
4808
4848
|
config: The config to use. Defaults to None.
|
|
4809
4849
|
kwargs: Additional keyword arguments.
|
|
4810
4850
|
|
|
4811
4851
|
Returns:
|
|
4812
|
-
The output of this Runnable
|
|
4852
|
+
The output of this ``Runnable``.
|
|
4853
|
+
|
|
4813
4854
|
"""
|
|
4814
4855
|
return await self._acall_with_config(
|
|
4815
4856
|
self._ainvoke,
|
|
@@ -5039,12 +5080,13 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
5039
5080
|
|
|
5040
5081
|
|
|
5041
5082
|
class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
5042
|
-
"""Runnable that calls another Runnable for each element of the input sequence.
|
|
5083
|
+
"""``Runnable`` that calls another ``Runnable`` for each element of the input sequence.
|
|
5043
5084
|
|
|
5044
|
-
Use only if creating a new RunnableEach subclass with different __init__ args.
|
|
5085
|
+
Use only if creating a new ``RunnableEach`` subclass with different ``__init__`` args.
|
|
5045
5086
|
|
|
5046
|
-
See documentation for RunnableEach for more details.
|
|
5047
|
-
|
|
5087
|
+
See documentation for ``RunnableEach`` for more details.
|
|
5088
|
+
|
|
5089
|
+
""" # noqa: E501
|
|
5048
5090
|
|
|
5049
5091
|
bound: Runnable[Input, Output]
|
|
5050
5092
|
|
|
@@ -5169,13 +5211,13 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5169
5211
|
|
|
5170
5212
|
|
|
5171
5213
|
class RunnableEach(RunnableEachBase[Input, Output]):
|
|
5172
|
-
"""Runnable that calls another Runnable for each element of the input sequence.
|
|
5214
|
+
"""``Runnable`` that calls another ``Runnable`` for each element of the input sequence.
|
|
5173
5215
|
|
|
5174
|
-
It allows you to call multiple inputs with the bounded Runnable
|
|
5216
|
+
It allows you to call multiple inputs with the bounded ``Runnable``.
|
|
5175
5217
|
|
|
5176
|
-
RunnableEach makes it easy to run multiple inputs for the Runnable
|
|
5218
|
+
``RunnableEach`` makes it easy to run multiple inputs for the ``Runnable``.
|
|
5177
5219
|
In the below example, we associate and run three inputs
|
|
5178
|
-
with a Runnable
|
|
5220
|
+
with a ``Runnable``:
|
|
5179
5221
|
|
|
5180
5222
|
.. code-block:: python
|
|
5181
5223
|
|
|
@@ -5194,7 +5236,7 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5194
5236
|
{'topic':'Biology'}])
|
|
5195
5237
|
print(output) # noqa: T201
|
|
5196
5238
|
|
|
5197
|
-
"""
|
|
5239
|
+
""" # noqa: E501
|
|
5198
5240
|
|
|
5199
5241
|
@override
|
|
5200
5242
|
def get_name(
|
|
@@ -5227,22 +5269,23 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5227
5269
|
Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
|
|
5228
5270
|
] = None,
|
|
5229
5271
|
) -> RunnableEach[Input, Output]:
|
|
5230
|
-
"""Bind lifecycle listeners to a Runnable
|
|
5272
|
+
"""Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
5231
5273
|
|
|
5232
|
-
The Run object contains information about the run, including its id
|
|
5233
|
-
type
|
|
5234
|
-
added to the run.
|
|
5274
|
+
The ``Run`` object contains information about the run, including its ``id``,
|
|
5275
|
+
``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and
|
|
5276
|
+
any tags or metadata added to the run.
|
|
5235
5277
|
|
|
5236
5278
|
Args:
|
|
5237
|
-
on_start: Called before the Runnable starts running, with the Run
|
|
5238
|
-
Defaults to None.
|
|
5239
|
-
on_end: Called after the Runnable finishes running, with the Run
|
|
5240
|
-
Defaults to None.
|
|
5241
|
-
on_error: Called if the Runnable throws an error, with the Run
|
|
5242
|
-
Defaults to None.
|
|
5279
|
+
on_start: Called before the ``Runnable`` starts running, with the ``Run``
|
|
5280
|
+
object. Defaults to None.
|
|
5281
|
+
on_end: Called after the ``Runnable`` finishes running, with the ``Run``
|
|
5282
|
+
object. Defaults to None.
|
|
5283
|
+
on_error: Called if the ``Runnable`` throws an error, with the ``Run``
|
|
5284
|
+
object. Defaults to None.
|
|
5243
5285
|
|
|
5244
5286
|
Returns:
|
|
5245
|
-
A new Runnable with the listeners bound.
|
|
5287
|
+
A new ``Runnable`` with the listeners bound.
|
|
5288
|
+
|
|
5246
5289
|
"""
|
|
5247
5290
|
return RunnableEach(
|
|
5248
5291
|
bound=self.bound.with_listeners(
|
|
@@ -5257,23 +5300,24 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5257
5300
|
on_end: Optional[AsyncListener] = None,
|
|
5258
5301
|
on_error: Optional[AsyncListener] = None,
|
|
5259
5302
|
) -> RunnableEach[Input, Output]:
|
|
5260
|
-
"""Bind async lifecycle listeners to a Runnable
|
|
5303
|
+
"""Bind async lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
5261
5304
|
|
|
5262
|
-
The Run object contains information about the run, including its id
|
|
5263
|
-
type
|
|
5264
|
-
added to the run.
|
|
5305
|
+
The ``Run`` object contains information about the run, including its ``id``,
|
|
5306
|
+
``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and
|
|
5307
|
+
any tags or metadata added to the run.
|
|
5265
5308
|
|
|
5266
5309
|
Args:
|
|
5267
|
-
on_start: Called asynchronously before the Runnable starts running,
|
|
5268
|
-
with the Run object. Defaults to None.
|
|
5269
|
-
on_end: Called asynchronously after the Runnable finishes running,
|
|
5270
|
-
with the Run object. Defaults to None.
|
|
5271
|
-
on_error: Called asynchronously if the Runnable throws an error,
|
|
5272
|
-
with the Run object. Defaults to None.
|
|
5310
|
+
on_start: Called asynchronously before the ``Runnable`` starts running,
|
|
5311
|
+
with the ``Run`` object. Defaults to None.
|
|
5312
|
+
on_end: Called asynchronously after the ``Runnable`` finishes running,
|
|
5313
|
+
with the ``Run`` object. Defaults to None.
|
|
5314
|
+
on_error: Called asynchronously if the ``Runnable`` throws an error,
|
|
5315
|
+
with the ``Run`` object. Defaults to None.
|
|
5273
5316
|
|
|
5274
5317
|
Returns:
|
|
5275
|
-
A new Runnable with the listeners bound.
|
|
5276
|
-
|
|
5318
|
+
A new ``Runnable`` with the listeners bound.
|
|
5319
|
+
|
|
5320
|
+
""" # noqa: E501
|
|
5277
5321
|
return RunnableEach(
|
|
5278
5322
|
bound=self.bound.with_alisteners(
|
|
5279
5323
|
on_start=on_start, on_end=on_end, on_error=on_error
|
|
@@ -5281,44 +5325,47 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5281
5325
|
)
|
|
5282
5326
|
|
|
5283
5327
|
|
|
5284
|
-
class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
5285
|
-
"""Runnable that delegates calls to another Runnable with a set of kwargs.
|
|
5328
|
+
class RunnableBindingBase(RunnableSerializable[Input, Output]): # type: ignore[no-redef]
|
|
5329
|
+
"""``Runnable`` that delegates calls to another ``Runnable`` with a set of kwargs.
|
|
5286
5330
|
|
|
5287
|
-
Use only if creating a new RunnableBinding subclass with different __init__
|
|
5331
|
+
Use only if creating a new ``RunnableBinding`` subclass with different ``__init__``
|
|
5332
|
+
args.
|
|
5333
|
+
|
|
5334
|
+
See documentation for ``RunnableBinding`` for more details.
|
|
5288
5335
|
|
|
5289
|
-
See documentation for RunnableBinding for more details.
|
|
5290
5336
|
"""
|
|
5291
5337
|
|
|
5292
5338
|
bound: Runnable[Input, Output]
|
|
5293
|
-
"""The underlying Runnable that this Runnable delegates to."""
|
|
5339
|
+
"""The underlying ``Runnable`` that this ``Runnable`` delegates to."""
|
|
5294
5340
|
|
|
5295
5341
|
kwargs: Mapping[str, Any] = Field(default_factory=dict)
|
|
5296
|
-
"""kwargs to pass to the underlying Runnable when running.
|
|
5342
|
+
"""kwargs to pass to the underlying ``Runnable`` when running.
|
|
5297
5343
|
|
|
5298
|
-
For example, when the Runnable binding is invoked the underlying
|
|
5299
|
-
Runnable will be invoked with the same input but with these additional
|
|
5344
|
+
For example, when the ``Runnable`` binding is invoked the underlying
|
|
5345
|
+
``Runnable`` will be invoked with the same input but with these additional
|
|
5300
5346
|
kwargs.
|
|
5347
|
+
|
|
5301
5348
|
"""
|
|
5302
5349
|
|
|
5303
|
-
config: RunnableConfig = Field(default_factory=RunnableConfig)
|
|
5304
|
-
"""The config to bind to the underlying Runnable
|
|
5350
|
+
config: RunnableConfig = Field(default_factory=RunnableConfig)
|
|
5351
|
+
"""The config to bind to the underlying ``Runnable``."""
|
|
5305
5352
|
|
|
5306
5353
|
config_factories: list[Callable[[RunnableConfig], RunnableConfig]] = Field(
|
|
5307
5354
|
default_factory=list
|
|
5308
5355
|
)
|
|
5309
|
-
"""The config factories to bind to the underlying Runnable
|
|
5356
|
+
"""The config factories to bind to the underlying ``Runnable``."""
|
|
5310
5357
|
|
|
5311
5358
|
# Union[Type[Input], BaseModel] + things like list[str]
|
|
5312
5359
|
custom_input_type: Optional[Any] = None
|
|
5313
|
-
"""Override the input type of the underlying Runnable with a custom type.
|
|
5360
|
+
"""Override the input type of the underlying ``Runnable`` with a custom type.
|
|
5314
5361
|
|
|
5315
|
-
The type can be a pydantic model, or a type annotation (e.g.,
|
|
5362
|
+
The type can be a pydantic model, or a type annotation (e.g., ``list[str]``).
|
|
5316
5363
|
"""
|
|
5317
5364
|
# Union[Type[Output], BaseModel] + things like list[str]
|
|
5318
5365
|
custom_output_type: Optional[Any] = None
|
|
5319
|
-
"""Override the output type of the underlying Runnable with a custom type.
|
|
5366
|
+
"""Override the output type of the underlying ``Runnable`` with a custom type.
|
|
5320
5367
|
|
|
5321
|
-
The type can be a pydantic model, or a type annotation (e.g.,
|
|
5368
|
+
The type can be a pydantic model, or a type annotation (e.g., ``list[str]``).
|
|
5322
5369
|
"""
|
|
5323
5370
|
|
|
5324
5371
|
model_config = ConfigDict(
|
|
@@ -5338,26 +5385,26 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
|
5338
5385
|
custom_output_type: Optional[Union[type[Output], BaseModel]] = None,
|
|
5339
5386
|
**other_kwargs: Any,
|
|
5340
5387
|
) -> None:
|
|
5341
|
-
"""Create a RunnableBinding from a Runnable and kwargs.
|
|
5388
|
+
"""Create a ``RunnableBinding`` from a ``Runnable`` and kwargs.
|
|
5342
5389
|
|
|
5343
5390
|
Args:
|
|
5344
|
-
bound: The underlying Runnable that this Runnable delegates calls to.
|
|
5345
|
-
kwargs: optional kwargs to pass to the underlying Runnable
|
|
5346
|
-
the underlying Runnable (e.g., via
|
|
5347
|
-
|
|
5391
|
+
bound: The underlying ``Runnable`` that this ``Runnable`` delegates calls to.
|
|
5392
|
+
kwargs: optional kwargs to pass to the underlying ``Runnable``, when running
|
|
5393
|
+
the underlying ``Runnable`` (e.g., via ``invoke``, ``batch``,
|
|
5394
|
+
``transform``, or ``stream`` or async variants)
|
|
5348
5395
|
Defaults to None.
|
|
5349
|
-
config: optional config to bind to the underlying Runnable
|
|
5396
|
+
config: optional config to bind to the underlying ``Runnable``.
|
|
5350
5397
|
Defaults to None.
|
|
5351
5398
|
config_factories: optional list of config factories to apply to the
|
|
5352
|
-
config before binding to the underlying Runnable
|
|
5399
|
+
config before binding to the underlying ``Runnable``.
|
|
5353
5400
|
Defaults to None.
|
|
5354
5401
|
custom_input_type: Specify to override the input type of the underlying
|
|
5355
|
-
|
|
5402
|
+
``Runnable`` with a custom type. Defaults to None.
|
|
5356
5403
|
custom_output_type: Specify to override the output type of the underlying
|
|
5357
|
-
|
|
5404
|
+
``Runnable`` with a custom type. Defaults to None.
|
|
5358
5405
|
**other_kwargs: Unpacked into the base class.
|
|
5359
|
-
"""
|
|
5360
|
-
super().__init__(
|
|
5406
|
+
""" # noqa: E501
|
|
5407
|
+
super().__init__(
|
|
5361
5408
|
bound=bound,
|
|
5362
5409
|
kwargs=kwargs or {},
|
|
5363
5410
|
config=config or {},
|
|
@@ -5430,7 +5477,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
|
5430
5477
|
def get_lc_namespace(cls) -> list[str]:
|
|
5431
5478
|
"""Get the namespace of the langchain object.
|
|
5432
5479
|
|
|
5433
|
-
Defaults to ["langchain", "schema", "runnable"]
|
|
5480
|
+
Defaults to ``["langchain", "schema", "runnable"]``.
|
|
5434
5481
|
"""
|
|
5435
5482
|
return ["langchain", "schema", "runnable"]
|
|
5436
5483
|
|
|
@@ -5682,29 +5729,29 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
|
5682
5729
|
yield item
|
|
5683
5730
|
|
|
5684
5731
|
|
|
5685
|
-
class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
5686
|
-
"""Wrap a Runnable with additional functionality.
|
|
5732
|
+
class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
|
|
5733
|
+
"""Wrap a ``Runnable`` with additional functionality.
|
|
5687
5734
|
|
|
5688
|
-
A RunnableBinding can be thought of as a "runnable decorator" that
|
|
5689
|
-
preserves the essential features of Runnable
|
|
5735
|
+
A ``RunnableBinding`` can be thought of as a "runnable decorator" that
|
|
5736
|
+
preserves the essential features of ``Runnable``; i.e., batching, streaming,
|
|
5690
5737
|
and async support, while adding additional functionality.
|
|
5691
5738
|
|
|
5692
|
-
Any class that inherits from Runnable can be bound to a
|
|
5693
|
-
Runnables expose a standard set of methods for creating
|
|
5694
|
-
or sub-classes of
|
|
5695
|
-
|
|
5739
|
+
Any class that inherits from ``Runnable`` can be bound to a ``RunnableBinding``.
|
|
5740
|
+
Runnables expose a standard set of methods for creating ``RunnableBindings``
|
|
5741
|
+
or sub-classes of ``RunnableBindings`` (e.g., ``RunnableRetry``,
|
|
5742
|
+
``RunnableWithFallbacks``) that add additional functionality.
|
|
5696
5743
|
|
|
5697
5744
|
These methods include:
|
|
5698
5745
|
|
|
5699
|
-
- ``bind``: Bind kwargs to pass to the underlying Runnable when running it.
|
|
5700
|
-
- ``with_config``: Bind config to pass to the underlying Runnable when running it.
|
|
5701
|
-
- ``with_listeners``: Bind lifecycle listeners to the underlying Runnable
|
|
5702
|
-
- ``with_types``: Override the input and output types of the underlying Runnable
|
|
5703
|
-
- ``with_retry``: Bind a retry policy to the underlying Runnable
|
|
5704
|
-
- ``with_fallbacks``: Bind a fallback policy to the underlying Runnable
|
|
5746
|
+
- ``bind``: Bind kwargs to pass to the underlying ``Runnable`` when running it.
|
|
5747
|
+
- ``with_config``: Bind config to pass to the underlying ``Runnable`` when running it.
|
|
5748
|
+
- ``with_listeners``: Bind lifecycle listeners to the underlying ``Runnable``.
|
|
5749
|
+
- ``with_types``: Override the input and output types of the underlying ``Runnable``.
|
|
5750
|
+
- ``with_retry``: Bind a retry policy to the underlying ``Runnable``.
|
|
5751
|
+
- ``with_fallbacks``: Bind a fallback policy to the underlying ``Runnable``.
|
|
5705
5752
|
|
|
5706
5753
|
Example:
|
|
5707
|
-
`bind`: Bind kwargs to pass to the underlying Runnable when running it.
|
|
5754
|
+
`bind`: Bind kwargs to pass to the underlying ``Runnable`` when running it.
|
|
5708
5755
|
|
|
5709
5756
|
.. code-block:: python
|
|
5710
5757
|
|
|
@@ -5718,7 +5765,8 @@ class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
|
5718
5765
|
runnable_binding = model.bind(stop=['-'])
|
|
5719
5766
|
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
|
|
5720
5767
|
|
|
5721
|
-
Can also be done by instantiating a RunnableBinding directly (not
|
|
5768
|
+
Can also be done by instantiating a ``RunnableBinding`` directly (not
|
|
5769
|
+
recommended):
|
|
5722
5770
|
|
|
5723
5771
|
.. code-block:: python
|
|
5724
5772
|
|
|
@@ -5729,18 +5777,19 @@ class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
|
5729
5777
|
)
|
|
5730
5778
|
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
|
|
5731
5779
|
|
|
5732
|
-
"""
|
|
5780
|
+
""" # noqa: E501
|
|
5733
5781
|
|
|
5734
5782
|
@override
|
|
5735
5783
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
5736
|
-
"""Bind additional kwargs to a Runnable
|
|
5784
|
+
"""Bind additional kwargs to a ``Runnable``, returning a new ``Runnable``.
|
|
5737
5785
|
|
|
5738
5786
|
Args:
|
|
5739
|
-
**kwargs: The kwargs to bind to the Runnable
|
|
5787
|
+
**kwargs: The kwargs to bind to the ``Runnable``.
|
|
5740
5788
|
|
|
5741
5789
|
Returns:
|
|
5742
|
-
A new Runnable with the same type and config as the original,
|
|
5790
|
+
A new ``Runnable`` with the same type and config as the original,
|
|
5743
5791
|
but with the additional kwargs bound.
|
|
5792
|
+
|
|
5744
5793
|
"""
|
|
5745
5794
|
return self.__class__(
|
|
5746
5795
|
bound=self.bound,
|
|
@@ -5781,22 +5830,22 @@ class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
|
5781
5830
|
Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
|
|
5782
5831
|
] = None,
|
|
5783
5832
|
) -> Runnable[Input, Output]:
|
|
5784
|
-
"""Bind lifecycle listeners to a Runnable
|
|
5833
|
+
"""Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
5785
5834
|
|
|
5786
|
-
The Run object contains information about the run, including its id
|
|
5787
|
-
type
|
|
5788
|
-
added to the run.
|
|
5835
|
+
The ``Run`` object contains information about the run, including its ``id``,
|
|
5836
|
+
``type``, ``input``, ``output``, ``error``, ``start_time``, ``end_time``, and
|
|
5837
|
+
any tags or metadata added to the run.
|
|
5789
5838
|
|
|
5790
5839
|
Args:
|
|
5791
|
-
on_start: Called before the Runnable starts running, with the Run
|
|
5792
|
-
Defaults to None.
|
|
5793
|
-
on_end: Called after the Runnable finishes running, with the Run
|
|
5794
|
-
Defaults to None.
|
|
5795
|
-
on_error: Called if the Runnable throws an error, with the Run
|
|
5796
|
-
Defaults to None.
|
|
5840
|
+
on_start: Called before the ``Runnable`` starts running, with the ``Run``
|
|
5841
|
+
object. Defaults to None.
|
|
5842
|
+
on_end: Called after the ``Runnable`` finishes running, with the ``Run``
|
|
5843
|
+
object. Defaults to None.
|
|
5844
|
+
on_error: Called if the ``Runnable`` throws an error, with the ``Run``
|
|
5845
|
+
object. Defaults to None.
|
|
5797
5846
|
|
|
5798
5847
|
Returns:
|
|
5799
|
-
A new Runnable with the listeners bound.
|
|
5848
|
+
A new ``Runnable`` with the listeners bound.
|
|
5800
5849
|
"""
|
|
5801
5850
|
from langchain_core.tracers.root_listeners import RootListenersTracer
|
|
5802
5851
|
|
|
@@ -5924,16 +5973,16 @@ RunnableLike = Union[
|
|
|
5924
5973
|
|
|
5925
5974
|
|
|
5926
5975
|
def coerce_to_runnable(thing: RunnableLike) -> Runnable[Input, Output]:
|
|
5927
|
-
"""Coerce a Runnable
|
|
5976
|
+
"""Coerce a ``Runnable``-like object into a ``Runnable``.
|
|
5928
5977
|
|
|
5929
5978
|
Args:
|
|
5930
|
-
thing: A Runnable
|
|
5979
|
+
thing: A ``Runnable``-like object.
|
|
5931
5980
|
|
|
5932
5981
|
Returns:
|
|
5933
|
-
A Runnable
|
|
5982
|
+
A ``Runnable``.
|
|
5934
5983
|
|
|
5935
5984
|
Raises:
|
|
5936
|
-
TypeError: If the object is not Runnable
|
|
5985
|
+
TypeError: If the object is not ``Runnable``-like.
|
|
5937
5986
|
"""
|
|
5938
5987
|
if isinstance(thing, Runnable):
|
|
5939
5988
|
return thing
|
|
@@ -5982,16 +6031,16 @@ def chain(
|
|
|
5982
6031
|
Callable[[Input], AsyncIterator[Output]],
|
|
5983
6032
|
],
|
|
5984
6033
|
) -> Runnable[Input, Output]:
|
|
5985
|
-
"""Decorate a function to make it a Runnable
|
|
6034
|
+
"""Decorate a function to make it a ``Runnable``.
|
|
5986
6035
|
|
|
5987
|
-
Sets the name of the Runnable to the name of the function.
|
|
6036
|
+
Sets the name of the ``Runnable`` to the name of the function.
|
|
5988
6037
|
Any runnables called by the function will be traced as dependencies.
|
|
5989
6038
|
|
|
5990
6039
|
Args:
|
|
5991
|
-
func: A
|
|
6040
|
+
func: A ``Callable``.
|
|
5992
6041
|
|
|
5993
6042
|
Returns:
|
|
5994
|
-
A Runnable
|
|
6043
|
+
A ``Runnable``.
|
|
5995
6044
|
|
|
5996
6045
|
Example:
|
|
5997
6046
|
|