langchain-core 0.3.73__py3-none-any.whl → 0.3.75__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/file.py +13 -2
- langchain_core/callbacks/manager.py +55 -16
- 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/base.py +33 -19
- langchain_core/language_models/chat_models.py +39 -20
- langchain_core/language_models/fake_chat_models.py +5 -4
- langchain_core/load/dump.py +3 -4
- langchain_core/messages/ai.py +4 -1
- langchain_core/messages/modifier.py +1 -1
- langchain_core/messages/tool.py +3 -3
- langchain_core/messages/utils.py +18 -17
- langchain_core/output_parsers/openai_tools.py +2 -0
- langchain_core/output_parsers/transform.py +2 -2
- langchain_core/output_parsers/xml.py +4 -3
- langchain_core/prompts/chat.py +1 -3
- langchain_core/runnables/base.py +507 -451
- langchain_core/runnables/branch.py +1 -1
- langchain_core/runnables/config.py +2 -2
- langchain_core/runnables/fallbacks.py +4 -4
- langchain_core/runnables/graph.py +3 -3
- 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 +8 -1
- langchain_core/tools/structured.py +1 -1
- langchain_core/tracers/_streaming.py +6 -7
- langchain_core/tracers/event_stream.py +1 -1
- langchain_core/tracers/log_stream.py +1 -1
- langchain_core/utils/function_calling.py +23 -10
- langchain_core/version.py +1 -1
- {langchain_core-0.3.73.dist-info → langchain_core-0.3.75.dist-info}/METADATA +6 -8
- {langchain_core-0.3.73.dist-info → langchain_core-0.3.75.dist-info}/RECORD +42 -42
- {langchain_core-0.3.73.dist-info → langchain_core-0.3.75.dist-info}/WHEEL +0 -0
- {langchain_core-0.3.73.dist-info → langchain_core-0.3.75.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
|
|
|
@@ -728,36 +730,38 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
728
730
|
@abstractmethod
|
|
729
731
|
def invoke(
|
|
730
732
|
self,
|
|
731
|
-
input: Input,
|
|
733
|
+
input: Input,
|
|
732
734
|
config: Optional[RunnableConfig] = None,
|
|
733
735
|
**kwargs: Any,
|
|
734
736
|
) -> 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(
|
|
750
753
|
self,
|
|
751
|
-
input: Input,
|
|
754
|
+
input: Input,
|
|
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
|
|
@@ -996,41 +1004,43 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
996
1004
|
|
|
997
1005
|
def stream(
|
|
998
1006
|
self,
|
|
999
|
-
input: Input,
|
|
1007
|
+
input: Input,
|
|
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
|
|
|
1017
1026
|
async def astream(
|
|
1018
1027
|
self,
|
|
1019
|
-
input: Input,
|
|
1028
|
+
input: Input,
|
|
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
|
|
|
@@ -1070,7 +1080,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1070
1080
|
|
|
1071
1081
|
async def astream_log(
|
|
1072
1082
|
self,
|
|
1073
|
-
input: Any,
|
|
1083
|
+
input: Any,
|
|
1074
1084
|
config: Optional[RunnableConfig] = None,
|
|
1075
1085
|
*,
|
|
1076
1086
|
diff: bool = True,
|
|
@@ -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,
|
|
@@ -1141,7 +1152,7 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1141
1152
|
|
|
1142
1153
|
async def astream_events(
|
|
1143
1154
|
self,
|
|
1144
|
-
input: Any,
|
|
1155
|
+
input: Any,
|
|
1145
1156
|
config: Optional[RunnableConfig] = None,
|
|
1146
1157
|
*,
|
|
1147
1158
|
version: Literal["v1", "v2"] = "v2",
|
|
@@ -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 (
|
|
@@ -1407,23 +1418,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1407
1418
|
|
|
1408
1419
|
def transform(
|
|
1409
1420
|
self,
|
|
1410
|
-
input: Iterator[Input],
|
|
1421
|
+
input: Iterator[Input],
|
|
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
|
|
|
@@ -1449,23 +1461,24 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
1449
1461
|
|
|
1450
1462
|
async def atransform(
|
|
1451
1463
|
self,
|
|
1452
|
-
input: AsyncIterator[Input],
|
|
1464
|
+
input: AsyncIterator[Input],
|
|
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
|
|
@@ -2362,12 +2384,12 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2362
2384
|
description: Optional[str] = None,
|
|
2363
2385
|
arg_types: Optional[dict[str, type]] = None,
|
|
2364
2386
|
) -> BaseTool:
|
|
2365
|
-
"""Create a BaseTool from a Runnable
|
|
2387
|
+
"""Create a ``BaseTool`` from a ``Runnable``.
|
|
2366
2388
|
|
|
2367
|
-
``as_tool`` will instantiate a BaseTool with a name, description, and
|
|
2368
|
-
``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
|
|
2369
2391
|
from ``runnable.get_input_schema``. Alternatively (e.g., if the
|
|
2370
|
-
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),
|
|
2371
2393
|
the schema can be specified directly with ``args_schema``. You can also
|
|
2372
2394
|
pass ``arg_types`` to just specify the required arguments and their types.
|
|
2373
2395
|
|
|
@@ -2376,9 +2398,11 @@ class Runnable(ABC, Generic[Input, Output]):
|
|
|
2376
2398
|
name: The name of the tool. Defaults to None.
|
|
2377
2399
|
description: The description of the tool. Defaults to None.
|
|
2378
2400
|
arg_types: A dictionary of argument names to types. Defaults to None.
|
|
2401
|
+
message_version: Version of ``ToolMessage`` to return given
|
|
2402
|
+
:class:`~langchain_core.messages.content_blocks.ToolCall` input.
|
|
2379
2403
|
|
|
2380
2404
|
Returns:
|
|
2381
|
-
A BaseTool instance.
|
|
2405
|
+
A ``BaseTool`` instance.
|
|
2382
2406
|
|
|
2383
2407
|
Typed dict input:
|
|
2384
2408
|
|
|
@@ -2477,10 +2501,11 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2477
2501
|
|
|
2478
2502
|
@override
|
|
2479
2503
|
def to_json(self) -> Union[SerializedConstructor, SerializedNotImplemented]:
|
|
2480
|
-
"""Serialize the Runnable to JSON.
|
|
2504
|
+
"""Serialize the ``Runnable`` to JSON.
|
|
2481
2505
|
|
|
2482
2506
|
Returns:
|
|
2483
|
-
A JSON-serializable representation of the Runnable
|
|
2507
|
+
A JSON-serializable representation of the ``Runnable``.
|
|
2508
|
+
|
|
2484
2509
|
"""
|
|
2485
2510
|
dumped = super().to_json()
|
|
2486
2511
|
with contextlib.suppress(Exception):
|
|
@@ -2490,13 +2515,13 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2490
2515
|
def configurable_fields(
|
|
2491
2516
|
self, **kwargs: AnyConfigurableField
|
|
2492
2517
|
) -> RunnableSerializable[Input, Output]:
|
|
2493
|
-
"""Configure particular Runnable fields at runtime.
|
|
2518
|
+
"""Configure particular ``Runnable`` fields at runtime.
|
|
2494
2519
|
|
|
2495
2520
|
Args:
|
|
2496
|
-
**kwargs: A dictionary of ConfigurableField instances to configure.
|
|
2521
|
+
**kwargs: A dictionary of ``ConfigurableField`` instances to configure.
|
|
2497
2522
|
|
|
2498
2523
|
Returns:
|
|
2499
|
-
A new Runnable with the fields configured.
|
|
2524
|
+
A new ``Runnable`` with the fields configured.
|
|
2500
2525
|
|
|
2501
2526
|
.. code-block:: python
|
|
2502
2527
|
|
|
@@ -2545,20 +2570,20 @@ class RunnableSerializable(Serializable, Runnable[Input, Output]):
|
|
|
2545
2570
|
prefix_keys: bool = False,
|
|
2546
2571
|
**kwargs: Union[Runnable[Input, Output], Callable[[], Runnable[Input, Output]]],
|
|
2547
2572
|
) -> RunnableSerializable[Input, Output]:
|
|
2548
|
-
"""Configure alternatives for Runnables that can be set at runtime.
|
|
2573
|
+
"""Configure alternatives for ``Runnables`` that can be set at runtime.
|
|
2549
2574
|
|
|
2550
2575
|
Args:
|
|
2551
|
-
which: The ConfigurableField instance that will be used to select the
|
|
2576
|
+
which: The ``ConfigurableField`` instance that will be used to select the
|
|
2552
2577
|
alternative.
|
|
2553
2578
|
default_key: The default key to use if no alternative is selected.
|
|
2554
2579
|
Defaults to ``'default'``.
|
|
2555
|
-
prefix_keys: Whether to prefix the keys with the ConfigurableField id.
|
|
2580
|
+
prefix_keys: Whether to prefix the keys with the ``ConfigurableField`` id.
|
|
2556
2581
|
Defaults to False.
|
|
2557
|
-
**kwargs: A dictionary of keys to Runnable instances or callables that
|
|
2558
|
-
return Runnable instances.
|
|
2582
|
+
**kwargs: A dictionary of keys to ``Runnable`` instances or callables that
|
|
2583
|
+
return ``Runnable`` instances.
|
|
2559
2584
|
|
|
2560
2585
|
Returns:
|
|
2561
|
-
A new Runnable with the alternatives configured.
|
|
2586
|
+
A new ``Runnable`` with the alternatives configured.
|
|
2562
2587
|
|
|
2563
2588
|
.. code-block:: python
|
|
2564
2589
|
|
|
@@ -2672,25 +2697,26 @@ def _seq_output_schema(
|
|
|
2672
2697
|
|
|
2673
2698
|
|
|
2674
2699
|
class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
2675
|
-
"""Sequence of Runnables
|
|
2700
|
+
"""Sequence of ``Runnables``, where the output of each is the input of the next.
|
|
2676
2701
|
|
|
2677
|
-
|
|
2702
|
+
**``RunnableSequence``** is the most important composition operator in LangChain
|
|
2678
2703
|
as it is used in virtually every chain.
|
|
2679
2704
|
|
|
2680
|
-
A RunnableSequence can be instantiated directly or more commonly by using the
|
|
2681
|
-
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``.
|
|
2682
2708
|
|
|
2683
|
-
Any RunnableSequence automatically supports sync, async, batch.
|
|
2709
|
+
Any ``RunnableSequence`` automatically supports sync, async, batch.
|
|
2684
2710
|
|
|
2685
|
-
The default implementations of
|
|
2686
|
-
asyncio gather and will be faster than naive invocation of invoke or ainvoke
|
|
2687
|
-
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.
|
|
2688
2714
|
|
|
2689
2715
|
Batching is implemented by invoking the batch method on each component of the
|
|
2690
|
-
RunnableSequence in order.
|
|
2716
|
+
``RunnableSequence`` in order.
|
|
2691
2717
|
|
|
2692
|
-
A RunnableSequence preserves the streaming properties of its components, so if
|
|
2693
|
-
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
|
|
2694
2720
|
is the method that implements the logic to map a streaming input to a streaming
|
|
2695
2721
|
output -- then the sequence will be able to stream input to output!
|
|
2696
2722
|
|
|
@@ -2698,15 +2724,16 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2698
2724
|
streaming will only begin after this component is run. If there are
|
|
2699
2725
|
multiple blocking components, streaming begins after the last one.
|
|
2700
2726
|
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
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).
|
|
2704
2731
|
|
|
2705
2732
|
If you need arbitrary logic and need streaming, you can subclass
|
|
2706
|
-
Runnable, and implement
|
|
2733
|
+
Runnable, and implement ``transform`` for whatever logic you need.
|
|
2707
2734
|
|
|
2708
2735
|
Here is a simple example that uses simple functions to illustrate the use of
|
|
2709
|
-
RunnableSequence
|
|
2736
|
+
``RunnableSequence``:
|
|
2710
2737
|
|
|
2711
2738
|
.. code-block:: python
|
|
2712
2739
|
|
|
@@ -2755,11 +2782,11 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2755
2782
|
# purposes. It allows specifying the `Input` on the first type, the `Output` of
|
|
2756
2783
|
# the last type.
|
|
2757
2784
|
first: Runnable[Input, Any]
|
|
2758
|
-
"""The first Runnable in the sequence."""
|
|
2785
|
+
"""The first ``Runnable`` in the sequence."""
|
|
2759
2786
|
middle: list[Runnable[Any, Any]] = Field(default_factory=list)
|
|
2760
|
-
"""The middle
|
|
2787
|
+
"""The middle ``Runnable`` in the sequence."""
|
|
2761
2788
|
last: Runnable[Any, Output]
|
|
2762
|
-
"""The last Runnable in the sequence."""
|
|
2789
|
+
"""The last ``Runnable`` in the sequence."""
|
|
2763
2790
|
|
|
2764
2791
|
def __init__(
|
|
2765
2792
|
self,
|
|
@@ -2769,13 +2796,13 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2769
2796
|
middle: Optional[list[Runnable[Any, Any]]] = None,
|
|
2770
2797
|
last: Optional[Runnable[Any, Any]] = None,
|
|
2771
2798
|
) -> None:
|
|
2772
|
-
"""Create a new RunnableSequence
|
|
2799
|
+
"""Create a new ``RunnableSequence``.
|
|
2773
2800
|
|
|
2774
2801
|
Args:
|
|
2775
2802
|
steps: The steps to include in the sequence.
|
|
2776
|
-
name: The name of the Runnable
|
|
2777
|
-
first: The first Runnable in the sequence. Defaults to None.
|
|
2778
|
-
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.
|
|
2779
2806
|
last: The last Runnable in the sequence. Defaults to None.
|
|
2780
2807
|
|
|
2781
2808
|
Raises:
|
|
@@ -2792,7 +2819,7 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2792
2819
|
if len(steps_flat) < 2:
|
|
2793
2820
|
msg = f"RunnableSequence must have at least 2 steps, got {len(steps_flat)}"
|
|
2794
2821
|
raise ValueError(msg)
|
|
2795
|
-
super().__init__(
|
|
2822
|
+
super().__init__(
|
|
2796
2823
|
first=steps_flat[0],
|
|
2797
2824
|
middle=list(steps_flat[1:-1]),
|
|
2798
2825
|
last=steps_flat[-1],
|
|
@@ -2806,10 +2833,10 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2806
2833
|
|
|
2807
2834
|
@property
|
|
2808
2835
|
def steps(self) -> list[Runnable[Any, Any]]:
|
|
2809
|
-
"""All the
|
|
2836
|
+
"""All the ``Runnable``s that make up the sequence in order.
|
|
2810
2837
|
|
|
2811
2838
|
Returns:
|
|
2812
|
-
A list of
|
|
2839
|
+
A list of ``Runnable``s.
|
|
2813
2840
|
"""
|
|
2814
2841
|
return [self.first, *self.middle, self.last]
|
|
2815
2842
|
|
|
@@ -2820,7 +2847,9 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2820
2847
|
|
|
2821
2848
|
Returns:
|
|
2822
2849
|
True if the object is serializable, False otherwise.
|
|
2823
|
-
|
|
2850
|
+
|
|
2851
|
+
Defaults to True.
|
|
2852
|
+
|
|
2824
2853
|
"""
|
|
2825
2854
|
return True
|
|
2826
2855
|
|
|
@@ -2831,26 +2860,27 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2831
2860
|
@property
|
|
2832
2861
|
@override
|
|
2833
2862
|
def InputType(self) -> type[Input]:
|
|
2834
|
-
"""The type of the input to the Runnable
|
|
2863
|
+
"""The type of the input to the ``Runnable``."""
|
|
2835
2864
|
return self.first.InputType
|
|
2836
2865
|
|
|
2837
2866
|
@property
|
|
2838
2867
|
@override
|
|
2839
2868
|
def OutputType(self) -> type[Output]:
|
|
2840
|
-
"""The type of the output of the Runnable
|
|
2869
|
+
"""The type of the output of the ``Runnable``."""
|
|
2841
2870
|
return self.last.OutputType
|
|
2842
2871
|
|
|
2843
2872
|
@override
|
|
2844
2873
|
def get_input_schema(
|
|
2845
2874
|
self, config: Optional[RunnableConfig] = None
|
|
2846
2875
|
) -> type[BaseModel]:
|
|
2847
|
-
"""Get the input schema of the Runnable
|
|
2876
|
+
"""Get the input schema of the ``Runnable``.
|
|
2848
2877
|
|
|
2849
2878
|
Args:
|
|
2850
2879
|
config: The config to use. Defaults to None.
|
|
2851
2880
|
|
|
2852
2881
|
Returns:
|
|
2853
|
-
The input schema of the Runnable
|
|
2882
|
+
The input schema of the ``Runnable``.
|
|
2883
|
+
|
|
2854
2884
|
"""
|
|
2855
2885
|
return _seq_input_schema(self.steps, config)
|
|
2856
2886
|
|
|
@@ -2858,23 +2888,25 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2858
2888
|
def get_output_schema(
|
|
2859
2889
|
self, config: Optional[RunnableConfig] = None
|
|
2860
2890
|
) -> type[BaseModel]:
|
|
2861
|
-
"""Get the output schema of the Runnable
|
|
2891
|
+
"""Get the output schema of the ``Runnable``.
|
|
2862
2892
|
|
|
2863
2893
|
Args:
|
|
2864
2894
|
config: The config to use. Defaults to None.
|
|
2865
2895
|
|
|
2866
2896
|
Returns:
|
|
2867
|
-
The output schema of the Runnable
|
|
2897
|
+
The output schema of the ``Runnable``.
|
|
2898
|
+
|
|
2868
2899
|
"""
|
|
2869
2900
|
return _seq_output_schema(self.steps, config)
|
|
2870
2901
|
|
|
2871
2902
|
@property
|
|
2872
2903
|
@override
|
|
2873
2904
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
2874
|
-
"""Get the config specs of the Runnable
|
|
2905
|
+
"""Get the config specs of the ``Runnable``.
|
|
2875
2906
|
|
|
2876
2907
|
Returns:
|
|
2877
|
-
The config specs of the Runnable
|
|
2908
|
+
The config specs of the ``Runnable``.
|
|
2909
|
+
|
|
2878
2910
|
"""
|
|
2879
2911
|
from langchain_core.beta.runnables.context import (
|
|
2880
2912
|
CONTEXT_CONFIG_PREFIX,
|
|
@@ -2922,16 +2954,17 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
2922
2954
|
|
|
2923
2955
|
@override
|
|
2924
2956
|
def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph:
|
|
2925
|
-
"""Get the graph representation of the Runnable
|
|
2957
|
+
"""Get the graph representation of the ``Runnable``.
|
|
2926
2958
|
|
|
2927
2959
|
Args:
|
|
2928
2960
|
config: The config to use. Defaults to None.
|
|
2929
2961
|
|
|
2930
2962
|
Returns:
|
|
2931
|
-
The graph representation of the Runnable
|
|
2963
|
+
The graph representation of the ``Runnable``.
|
|
2932
2964
|
|
|
2933
2965
|
Raises:
|
|
2934
|
-
ValueError: If a Runnable has no first or last node.
|
|
2966
|
+
ValueError: If a ``Runnable`` has no first or last node.
|
|
2967
|
+
|
|
2935
2968
|
"""
|
|
2936
2969
|
from langchain_core.runnables.graph import Graph
|
|
2937
2970
|
|
|
@@ -3469,19 +3502,19 @@ class RunnableSequence(RunnableSerializable[Input, Output]):
|
|
|
3469
3502
|
|
|
3470
3503
|
|
|
3471
3504
|
class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
3472
|
-
"""Runnable that runs a mapping of
|
|
3505
|
+
"""Runnable that runs a mapping of ``Runnable``s in parallel.
|
|
3473
3506
|
|
|
3474
3507
|
Returns a mapping of their outputs.
|
|
3475
3508
|
|
|
3476
|
-
RunnableParallel is one of the two main composition primitives for the LCEL,
|
|
3477
|
-
alongside RunnableSequence
|
|
3478
|
-
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.
|
|
3479
3512
|
|
|
3480
|
-
A RunnableParallel can be instantiated directly or by using a dict literal
|
|
3481
|
-
sequence.
|
|
3513
|
+
A ``RunnableParallel`` can be instantiated directly or by using a dict literal
|
|
3514
|
+
within a sequence.
|
|
3482
3515
|
|
|
3483
3516
|
Here is a simple example that uses functions to illustrate the use of
|
|
3484
|
-
RunnableParallel
|
|
3517
|
+
``RunnableParallel``:
|
|
3485
3518
|
|
|
3486
3519
|
.. code-block:: python
|
|
3487
3520
|
|
|
@@ -3520,8 +3553,8 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3520
3553
|
sequence.batch([1, 2, 3])
|
|
3521
3554
|
await sequence.abatch([1, 2, 3])
|
|
3522
3555
|
|
|
3523
|
-
RunnableParallel makes it easy to run
|
|
3524
|
-
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``:
|
|
3525
3558
|
|
|
3526
3559
|
.. code-block:: python
|
|
3527
3560
|
|
|
@@ -3570,15 +3603,16 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3570
3603
|
Mapping[str, Union[Runnable[Input, Any], Callable[[Input], Any]]],
|
|
3571
3604
|
],
|
|
3572
3605
|
) -> None:
|
|
3573
|
-
"""Create a RunnableParallel
|
|
3606
|
+
"""Create a ``RunnableParallel``.
|
|
3574
3607
|
|
|
3575
3608
|
Args:
|
|
3576
3609
|
steps__: The steps to include. Defaults to None.
|
|
3577
3610
|
**kwargs: Additional steps to include.
|
|
3611
|
+
|
|
3578
3612
|
"""
|
|
3579
3613
|
merged = {**steps__} if steps__ is not None else {}
|
|
3580
3614
|
merged.update(kwargs)
|
|
3581
|
-
super().__init__(
|
|
3615
|
+
super().__init__(
|
|
3582
3616
|
steps__={key: coerce_to_runnable(r) for key, r in merged.items()}
|
|
3583
3617
|
)
|
|
3584
3618
|
|
|
@@ -3600,14 +3634,15 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3600
3634
|
def get_name(
|
|
3601
3635
|
self, suffix: Optional[str] = None, *, name: Optional[str] = None
|
|
3602
3636
|
) -> str:
|
|
3603
|
-
"""Get the name of the Runnable
|
|
3637
|
+
"""Get the name of the ``Runnable``.
|
|
3604
3638
|
|
|
3605
3639
|
Args:
|
|
3606
3640
|
suffix: The suffix to use. Defaults to None.
|
|
3607
3641
|
name: The name to use. Defaults to None.
|
|
3608
3642
|
|
|
3609
3643
|
Returns:
|
|
3610
|
-
The name of the Runnable
|
|
3644
|
+
The name of the ``Runnable``.
|
|
3645
|
+
|
|
3611
3646
|
"""
|
|
3612
3647
|
name = name or self.name or f"RunnableParallel<{','.join(self.steps__.keys())}>"
|
|
3613
3648
|
return super().get_name(suffix, name=name)
|
|
@@ -3615,7 +3650,7 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3615
3650
|
@property
|
|
3616
3651
|
@override
|
|
3617
3652
|
def InputType(self) -> Any:
|
|
3618
|
-
"""The type of the input to the Runnable
|
|
3653
|
+
"""The type of the input to the ``Runnable``."""
|
|
3619
3654
|
for step in self.steps__.values():
|
|
3620
3655
|
if step.InputType:
|
|
3621
3656
|
return step.InputType
|
|
@@ -3626,13 +3661,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3626
3661
|
def get_input_schema(
|
|
3627
3662
|
self, config: Optional[RunnableConfig] = None
|
|
3628
3663
|
) -> type[BaseModel]:
|
|
3629
|
-
"""Get the input schema of the Runnable
|
|
3664
|
+
"""Get the input schema of the ``Runnable``.
|
|
3630
3665
|
|
|
3631
3666
|
Args:
|
|
3632
3667
|
config: The config to use. Defaults to None.
|
|
3633
3668
|
|
|
3634
3669
|
Returns:
|
|
3635
|
-
The input schema of the Runnable
|
|
3670
|
+
The input schema of the ``Runnable``.
|
|
3671
|
+
|
|
3636
3672
|
"""
|
|
3637
3673
|
if all(
|
|
3638
3674
|
s.get_input_schema(config).model_json_schema().get("type", "object")
|
|
@@ -3656,13 +3692,14 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3656
3692
|
def get_output_schema(
|
|
3657
3693
|
self, config: Optional[RunnableConfig] = None
|
|
3658
3694
|
) -> type[BaseModel]:
|
|
3659
|
-
"""Get the output schema of the Runnable
|
|
3695
|
+
"""Get the output schema of the ``Runnable``.
|
|
3660
3696
|
|
|
3661
3697
|
Args:
|
|
3662
3698
|
config: The config to use. Defaults to None.
|
|
3663
3699
|
|
|
3664
3700
|
Returns:
|
|
3665
|
-
The output schema of the Runnable
|
|
3701
|
+
The output schema of the ``Runnable``.
|
|
3702
|
+
|
|
3666
3703
|
"""
|
|
3667
3704
|
fields = {k: (v.OutputType, ...) for k, v in self.steps__.items()}
|
|
3668
3705
|
return create_model_v2(self.get_name("Output"), field_definitions=fields)
|
|
@@ -3670,10 +3707,11 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3670
3707
|
@property
|
|
3671
3708
|
@override
|
|
3672
3709
|
def config_specs(self) -> list[ConfigurableFieldSpec]:
|
|
3673
|
-
"""Get the config specs of the Runnable
|
|
3710
|
+
"""Get the config specs of the ``Runnable``.
|
|
3674
3711
|
|
|
3675
3712
|
Returns:
|
|
3676
|
-
The config specs of the Runnable
|
|
3713
|
+
The config specs of the ``Runnable``.
|
|
3714
|
+
|
|
3677
3715
|
"""
|
|
3678
3716
|
return get_unique_config_specs(
|
|
3679
3717
|
spec for step in self.steps__.values() for spec in step.config_specs
|
|
@@ -3681,16 +3719,17 @@ class RunnableParallel(RunnableSerializable[Input, dict[str, Any]]):
|
|
|
3681
3719
|
|
|
3682
3720
|
@override
|
|
3683
3721
|
def get_graph(self, config: Optional[RunnableConfig] = None) -> Graph:
|
|
3684
|
-
"""Get the graph representation of the Runnable
|
|
3722
|
+
"""Get the graph representation of the ``Runnable``.
|
|
3685
3723
|
|
|
3686
3724
|
Args:
|
|
3687
3725
|
config: The config to use. Defaults to None.
|
|
3688
3726
|
|
|
3689
3727
|
Returns:
|
|
3690
|
-
The graph representation of the Runnable
|
|
3728
|
+
The graph representation of the ``Runnable``.
|
|
3691
3729
|
|
|
3692
3730
|
Raises:
|
|
3693
|
-
ValueError: If a Runnable has no first or last node.
|
|
3731
|
+
ValueError: If a ``Runnable`` has no first or last node.
|
|
3732
|
+
|
|
3694
3733
|
"""
|
|
3695
3734
|
from langchain_core.runnables.graph import Graph
|
|
3696
3735
|
|
|
@@ -3987,22 +4026,24 @@ RunnableMap = RunnableParallel
|
|
|
3987
4026
|
|
|
3988
4027
|
|
|
3989
4028
|
class RunnableGenerator(Runnable[Input, Output]):
|
|
3990
|
-
"""Runnable that runs a generator function.
|
|
4029
|
+
"""``Runnable`` that runs a generator function.
|
|
3991
4030
|
|
|
3992
|
-
|
|
4031
|
+
``RunnableGenerator``s can be instantiated directly or by using a generator within
|
|
3993
4032
|
a sequence.
|
|
3994
4033
|
|
|
3995
|
-
|
|
3996
|
-
parsers, while preserving streaming capabilities. Given a generator function
|
|
3997
|
-
a signature Iterator[A] -> Iterator[B]
|
|
3998
|
-
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.
|
|
3999
4039
|
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
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``.
|
|
4004
4045
|
|
|
4005
|
-
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``:
|
|
4006
4047
|
|
|
4007
4048
|
.. code-block:: python
|
|
4008
4049
|
|
|
@@ -4031,7 +4072,7 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4031
4072
|
await runnable.ainvoke(None) # "Have a nice day"
|
|
4032
4073
|
[p async for p in runnable.astream(None)] # ["Have", " a", " nice", " day"]
|
|
4033
4074
|
|
|
4034
|
-
RunnableGenerator makes it easy to implement custom behavior within a streaming
|
|
4075
|
+
``RunnableGenerator`` makes it easy to implement custom behavior within a streaming
|
|
4035
4076
|
context. Below we show an example:
|
|
4036
4077
|
|
|
4037
4078
|
.. code-block:: python
|
|
@@ -4085,15 +4126,16 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4085
4126
|
*,
|
|
4086
4127
|
name: Optional[str] = None,
|
|
4087
4128
|
) -> None:
|
|
4088
|
-
"""Initialize a RunnableGenerator
|
|
4129
|
+
"""Initialize a ``RunnableGenerator``.
|
|
4089
4130
|
|
|
4090
4131
|
Args:
|
|
4091
4132
|
transform: The transform function.
|
|
4092
4133
|
atransform: The async transform function. Defaults to None.
|
|
4093
|
-
name: The name of the Runnable
|
|
4134
|
+
name: The name of the ``Runnable``. Defaults to None.
|
|
4094
4135
|
|
|
4095
4136
|
Raises:
|
|
4096
4137
|
TypeError: If the transform is not a generator function.
|
|
4138
|
+
|
|
4097
4139
|
"""
|
|
4098
4140
|
if atransform is not None:
|
|
4099
4141
|
self._atransform = atransform
|
|
@@ -4288,12 +4330,12 @@ class RunnableGenerator(Runnable[Input, Output]):
|
|
|
4288
4330
|
|
|
4289
4331
|
|
|
4290
4332
|
class RunnableLambda(Runnable[Input, Output]):
|
|
4291
|
-
"""RunnableLambda converts a python callable into a Runnable
|
|
4333
|
+
"""``RunnableLambda`` converts a python callable into a ``Runnable``.
|
|
4292
4334
|
|
|
4293
|
-
Wrapping a callable in a RunnableLambda makes the callable usable
|
|
4335
|
+
Wrapping a callable in a ``RunnableLambda`` makes the callable usable
|
|
4294
4336
|
within either a sync or async context.
|
|
4295
4337
|
|
|
4296
|
-
RunnableLambda can be composed as any other Runnable and provides
|
|
4338
|
+
``RunnableLambda`` can be composed as any other ``Runnable`` and provides
|
|
4297
4339
|
seamless integration with LangChain tracing.
|
|
4298
4340
|
|
|
4299
4341
|
``RunnableLambda`` is best suited for code that does not need to support
|
|
@@ -4370,7 +4412,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4370
4412
|
] = None,
|
|
4371
4413
|
name: Optional[str] = None,
|
|
4372
4414
|
) -> None:
|
|
4373
|
-
"""Create a RunnableLambda from a callable, and async callable or both.
|
|
4415
|
+
"""Create a ``RunnableLambda`` from a callable, and async callable or both.
|
|
4374
4416
|
|
|
4375
4417
|
Accepts both sync and async variants to allow providing efficient
|
|
4376
4418
|
implementations for sync and async execution.
|
|
@@ -4379,11 +4421,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4379
4421
|
func: Either sync or async callable
|
|
4380
4422
|
afunc: An async callable that takes an input and returns an output.
|
|
4381
4423
|
Defaults to None.
|
|
4382
|
-
name: The name of the Runnable
|
|
4424
|
+
name: The name of the ``Runnable``. Defaults to None.
|
|
4383
4425
|
|
|
4384
4426
|
Raises:
|
|
4385
|
-
TypeError: If the func is not a callable type.
|
|
4386
|
-
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
|
+
|
|
4387
4430
|
"""
|
|
4388
4431
|
if afunc is not None:
|
|
4389
4432
|
self.afunc = afunc
|
|
@@ -4422,7 +4465,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4422
4465
|
@property
|
|
4423
4466
|
@override
|
|
4424
4467
|
def InputType(self) -> Any:
|
|
4425
|
-
"""The type of the input to this Runnable
|
|
4468
|
+
"""The type of the input to this ``Runnable``."""
|
|
4426
4469
|
func = getattr(self, "func", None) or self.afunc
|
|
4427
4470
|
try:
|
|
4428
4471
|
params = inspect.signature(func).parameters
|
|
@@ -4437,13 +4480,14 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4437
4480
|
def get_input_schema(
|
|
4438
4481
|
self, config: Optional[RunnableConfig] = None
|
|
4439
4482
|
) -> type[BaseModel]:
|
|
4440
|
-
"""The pydantic schema for the input to this Runnable
|
|
4483
|
+
"""The pydantic schema for the input to this ``Runnable``.
|
|
4441
4484
|
|
|
4442
4485
|
Args:
|
|
4443
4486
|
config: The config to use. Defaults to None.
|
|
4444
4487
|
|
|
4445
4488
|
Returns:
|
|
4446
|
-
The input schema for this Runnable
|
|
4489
|
+
The input schema for this ``Runnable``.
|
|
4490
|
+
|
|
4447
4491
|
"""
|
|
4448
4492
|
func = getattr(self, "func", None) or self.afunc
|
|
4449
4493
|
|
|
@@ -4481,10 +4525,11 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4481
4525
|
@property
|
|
4482
4526
|
@override
|
|
4483
4527
|
def OutputType(self) -> Any:
|
|
4484
|
-
"""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.
|
|
4485
4529
|
|
|
4486
4530
|
Returns:
|
|
4487
|
-
The type of the output of this Runnable
|
|
4531
|
+
The type of the output of this ``Runnable``.
|
|
4532
|
+
|
|
4488
4533
|
"""
|
|
4489
4534
|
func = getattr(self, "func", None) or self.afunc
|
|
4490
4535
|
try:
|
|
@@ -4530,11 +4575,12 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4530
4575
|
|
|
4531
4576
|
@functools.cached_property
|
|
4532
4577
|
def deps(self) -> list[Runnable]:
|
|
4533
|
-
"""The dependencies of this Runnable
|
|
4578
|
+
"""The dependencies of this ``Runnable``.
|
|
4534
4579
|
|
|
4535
4580
|
Returns:
|
|
4536
|
-
The dependencies of this Runnable
|
|
4537
|
-
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
|
+
|
|
4538
4584
|
"""
|
|
4539
4585
|
if hasattr(self, "func"):
|
|
4540
4586
|
objects = get_function_nonlocals(self.func)
|
|
@@ -4598,7 +4644,7 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4598
4644
|
__hash__ = None # type: ignore[assignment]
|
|
4599
4645
|
|
|
4600
4646
|
def __repr__(self) -> str:
|
|
4601
|
-
"""A string representation of this Runnable
|
|
4647
|
+
"""A string representation of this ``Runnable``."""
|
|
4602
4648
|
if self._repr is None:
|
|
4603
4649
|
if hasattr(self, "func") and isinstance(self.func, itemgetter):
|
|
4604
4650
|
self._repr = f"RunnableLambda({str(self.func)[len('operator.') :]})"
|
|
@@ -4764,18 +4810,19 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4764
4810
|
config: Optional[RunnableConfig] = None,
|
|
4765
4811
|
**kwargs: Optional[Any],
|
|
4766
4812
|
) -> Output:
|
|
4767
|
-
"""Invoke this Runnable synchronously.
|
|
4813
|
+
"""Invoke this ``Runnable`` synchronously.
|
|
4768
4814
|
|
|
4769
4815
|
Args:
|
|
4770
|
-
input: The input to this Runnable
|
|
4816
|
+
input: The input to this ``Runnable``.
|
|
4771
4817
|
config: The config to use. Defaults to None.
|
|
4772
4818
|
kwargs: Additional keyword arguments.
|
|
4773
4819
|
|
|
4774
4820
|
Returns:
|
|
4775
|
-
The output of this Runnable
|
|
4821
|
+
The output of this ``Runnable``.
|
|
4776
4822
|
|
|
4777
4823
|
Raises:
|
|
4778
|
-
TypeError: If the Runnable is a coroutine function.
|
|
4824
|
+
TypeError: If the ``Runnable`` is a coroutine function.
|
|
4825
|
+
|
|
4779
4826
|
"""
|
|
4780
4827
|
if hasattr(self, "func"):
|
|
4781
4828
|
return self._call_with_config(
|
|
@@ -4794,15 +4841,16 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
4794
4841
|
config: Optional[RunnableConfig] = None,
|
|
4795
4842
|
**kwargs: Optional[Any],
|
|
4796
4843
|
) -> Output:
|
|
4797
|
-
"""Invoke this Runnable asynchronously.
|
|
4844
|
+
"""Invoke this ``Runnable`` asynchronously.
|
|
4798
4845
|
|
|
4799
4846
|
Args:
|
|
4800
|
-
input: The input to this Runnable
|
|
4847
|
+
input: The input to this ``Runnable``.
|
|
4801
4848
|
config: The config to use. Defaults to None.
|
|
4802
4849
|
kwargs: Additional keyword arguments.
|
|
4803
4850
|
|
|
4804
4851
|
Returns:
|
|
4805
|
-
The output of this Runnable
|
|
4852
|
+
The output of this ``Runnable``.
|
|
4853
|
+
|
|
4806
4854
|
"""
|
|
4807
4855
|
return await self._acall_with_config(
|
|
4808
4856
|
self._ainvoke,
|
|
@@ -5032,12 +5080,13 @@ class RunnableLambda(Runnable[Input, Output]):
|
|
|
5032
5080
|
|
|
5033
5081
|
|
|
5034
5082
|
class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
5035
|
-
"""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.
|
|
5036
5084
|
|
|
5037
|
-
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.
|
|
5038
5086
|
|
|
5039
|
-
See documentation for RunnableEach for more details.
|
|
5040
|
-
|
|
5087
|
+
See documentation for ``RunnableEach`` for more details.
|
|
5088
|
+
|
|
5089
|
+
""" # noqa: E501
|
|
5041
5090
|
|
|
5042
5091
|
bound: Runnable[Input, Output]
|
|
5043
5092
|
|
|
@@ -5162,13 +5211,13 @@ class RunnableEachBase(RunnableSerializable[list[Input], list[Output]]):
|
|
|
5162
5211
|
|
|
5163
5212
|
|
|
5164
5213
|
class RunnableEach(RunnableEachBase[Input, Output]):
|
|
5165
|
-
"""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.
|
|
5166
5215
|
|
|
5167
|
-
It allows you to call multiple inputs with the bounded Runnable
|
|
5216
|
+
It allows you to call multiple inputs with the bounded ``Runnable``.
|
|
5168
5217
|
|
|
5169
|
-
RunnableEach makes it easy to run multiple inputs for the Runnable
|
|
5218
|
+
``RunnableEach`` makes it easy to run multiple inputs for the ``Runnable``.
|
|
5170
5219
|
In the below example, we associate and run three inputs
|
|
5171
|
-
with a Runnable
|
|
5220
|
+
with a ``Runnable``:
|
|
5172
5221
|
|
|
5173
5222
|
.. code-block:: python
|
|
5174
5223
|
|
|
@@ -5187,7 +5236,7 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5187
5236
|
{'topic':'Biology'}])
|
|
5188
5237
|
print(output) # noqa: T201
|
|
5189
5238
|
|
|
5190
|
-
"""
|
|
5239
|
+
""" # noqa: E501
|
|
5191
5240
|
|
|
5192
5241
|
@override
|
|
5193
5242
|
def get_name(
|
|
@@ -5220,22 +5269,23 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5220
5269
|
Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
|
|
5221
5270
|
] = None,
|
|
5222
5271
|
) -> RunnableEach[Input, Output]:
|
|
5223
|
-
"""Bind lifecycle listeners to a Runnable
|
|
5272
|
+
"""Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
5224
5273
|
|
|
5225
|
-
The Run object contains information about the run, including its id
|
|
5226
|
-
type
|
|
5227
|
-
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.
|
|
5228
5277
|
|
|
5229
5278
|
Args:
|
|
5230
|
-
on_start: Called before the Runnable starts running, with the Run
|
|
5231
|
-
Defaults to None.
|
|
5232
|
-
on_end: Called after the Runnable finishes running, with the Run
|
|
5233
|
-
Defaults to None.
|
|
5234
|
-
on_error: Called if the Runnable throws an error, with the Run
|
|
5235
|
-
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.
|
|
5236
5285
|
|
|
5237
5286
|
Returns:
|
|
5238
|
-
A new Runnable with the listeners bound.
|
|
5287
|
+
A new ``Runnable`` with the listeners bound.
|
|
5288
|
+
|
|
5239
5289
|
"""
|
|
5240
5290
|
return RunnableEach(
|
|
5241
5291
|
bound=self.bound.with_listeners(
|
|
@@ -5250,23 +5300,24 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5250
5300
|
on_end: Optional[AsyncListener] = None,
|
|
5251
5301
|
on_error: Optional[AsyncListener] = None,
|
|
5252
5302
|
) -> RunnableEach[Input, Output]:
|
|
5253
|
-
"""Bind async lifecycle listeners to a Runnable
|
|
5303
|
+
"""Bind async lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
5254
5304
|
|
|
5255
|
-
The Run object contains information about the run, including its id
|
|
5256
|
-
type
|
|
5257
|
-
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.
|
|
5258
5308
|
|
|
5259
5309
|
Args:
|
|
5260
|
-
on_start: Called asynchronously before the Runnable starts running,
|
|
5261
|
-
with the Run object. Defaults to None.
|
|
5262
|
-
on_end: Called asynchronously after the Runnable finishes running,
|
|
5263
|
-
with the Run object. Defaults to None.
|
|
5264
|
-
on_error: Called asynchronously if the Runnable throws an error,
|
|
5265
|
-
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.
|
|
5266
5316
|
|
|
5267
5317
|
Returns:
|
|
5268
|
-
A new Runnable with the listeners bound.
|
|
5269
|
-
|
|
5318
|
+
A new ``Runnable`` with the listeners bound.
|
|
5319
|
+
|
|
5320
|
+
""" # noqa: E501
|
|
5270
5321
|
return RunnableEach(
|
|
5271
5322
|
bound=self.bound.with_alisteners(
|
|
5272
5323
|
on_start=on_start, on_end=on_end, on_error=on_error
|
|
@@ -5274,44 +5325,47 @@ class RunnableEach(RunnableEachBase[Input, Output]):
|
|
|
5274
5325
|
)
|
|
5275
5326
|
|
|
5276
5327
|
|
|
5277
|
-
class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
5278
|
-
"""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.
|
|
5330
|
+
|
|
5331
|
+
Use only if creating a new ``RunnableBinding`` subclass with different ``__init__``
|
|
5332
|
+
args.
|
|
5279
5333
|
|
|
5280
|
-
|
|
5334
|
+
See documentation for ``RunnableBinding`` for more details.
|
|
5281
5335
|
|
|
5282
|
-
See documentation for RunnableBinding for more details.
|
|
5283
5336
|
"""
|
|
5284
5337
|
|
|
5285
5338
|
bound: Runnable[Input, Output]
|
|
5286
|
-
"""The underlying Runnable that this Runnable delegates to."""
|
|
5339
|
+
"""The underlying ``Runnable`` that this ``Runnable`` delegates to."""
|
|
5287
5340
|
|
|
5288
5341
|
kwargs: Mapping[str, Any] = Field(default_factory=dict)
|
|
5289
|
-
"""kwargs to pass to the underlying Runnable when running.
|
|
5342
|
+
"""kwargs to pass to the underlying ``Runnable`` when running.
|
|
5290
5343
|
|
|
5291
|
-
For example, when the Runnable binding is invoked the underlying
|
|
5292
|
-
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
|
|
5293
5346
|
kwargs.
|
|
5347
|
+
|
|
5294
5348
|
"""
|
|
5295
5349
|
|
|
5296
|
-
config: RunnableConfig = Field(default_factory=RunnableConfig)
|
|
5297
|
-
"""The config to bind to the underlying Runnable
|
|
5350
|
+
config: RunnableConfig = Field(default_factory=RunnableConfig)
|
|
5351
|
+
"""The config to bind to the underlying ``Runnable``."""
|
|
5298
5352
|
|
|
5299
5353
|
config_factories: list[Callable[[RunnableConfig], RunnableConfig]] = Field(
|
|
5300
5354
|
default_factory=list
|
|
5301
5355
|
)
|
|
5302
|
-
"""The config factories to bind to the underlying Runnable
|
|
5356
|
+
"""The config factories to bind to the underlying ``Runnable``."""
|
|
5303
5357
|
|
|
5304
5358
|
# Union[Type[Input], BaseModel] + things like list[str]
|
|
5305
5359
|
custom_input_type: Optional[Any] = None
|
|
5306
|
-
"""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.
|
|
5307
5361
|
|
|
5308
|
-
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]``).
|
|
5309
5363
|
"""
|
|
5310
5364
|
# Union[Type[Output], BaseModel] + things like list[str]
|
|
5311
5365
|
custom_output_type: Optional[Any] = None
|
|
5312
|
-
"""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.
|
|
5313
5367
|
|
|
5314
|
-
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]``).
|
|
5315
5369
|
"""
|
|
5316
5370
|
|
|
5317
5371
|
model_config = ConfigDict(
|
|
@@ -5331,26 +5385,26 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
|
5331
5385
|
custom_output_type: Optional[Union[type[Output], BaseModel]] = None,
|
|
5332
5386
|
**other_kwargs: Any,
|
|
5333
5387
|
) -> None:
|
|
5334
|
-
"""Create a RunnableBinding from a Runnable and kwargs.
|
|
5388
|
+
"""Create a ``RunnableBinding`` from a ``Runnable`` and kwargs.
|
|
5335
5389
|
|
|
5336
5390
|
Args:
|
|
5337
|
-
bound: The underlying Runnable that this Runnable delegates calls to.
|
|
5338
|
-
kwargs: optional kwargs to pass to the underlying Runnable
|
|
5339
|
-
the underlying Runnable (e.g., via
|
|
5340
|
-
|
|
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)
|
|
5341
5395
|
Defaults to None.
|
|
5342
|
-
config: optional config to bind to the underlying Runnable
|
|
5396
|
+
config: optional config to bind to the underlying ``Runnable``.
|
|
5343
5397
|
Defaults to None.
|
|
5344
5398
|
config_factories: optional list of config factories to apply to the
|
|
5345
|
-
config before binding to the underlying Runnable
|
|
5399
|
+
config before binding to the underlying ``Runnable``.
|
|
5346
5400
|
Defaults to None.
|
|
5347
5401
|
custom_input_type: Specify to override the input type of the underlying
|
|
5348
|
-
|
|
5402
|
+
``Runnable`` with a custom type. Defaults to None.
|
|
5349
5403
|
custom_output_type: Specify to override the output type of the underlying
|
|
5350
|
-
|
|
5404
|
+
``Runnable`` with a custom type. Defaults to None.
|
|
5351
5405
|
**other_kwargs: Unpacked into the base class.
|
|
5352
|
-
"""
|
|
5353
|
-
super().__init__(
|
|
5406
|
+
""" # noqa: E501
|
|
5407
|
+
super().__init__(
|
|
5354
5408
|
bound=bound,
|
|
5355
5409
|
kwargs=kwargs or {},
|
|
5356
5410
|
config=config or {},
|
|
@@ -5423,7 +5477,7 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
|
5423
5477
|
def get_lc_namespace(cls) -> list[str]:
|
|
5424
5478
|
"""Get the namespace of the langchain object.
|
|
5425
5479
|
|
|
5426
|
-
Defaults to ["langchain", "schema", "runnable"]
|
|
5480
|
+
Defaults to ``["langchain", "schema", "runnable"]``.
|
|
5427
5481
|
"""
|
|
5428
5482
|
return ["langchain", "schema", "runnable"]
|
|
5429
5483
|
|
|
@@ -5675,29 +5729,29 @@ class RunnableBindingBase(RunnableSerializable[Input, Output]):
|
|
|
5675
5729
|
yield item
|
|
5676
5730
|
|
|
5677
5731
|
|
|
5678
|
-
class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
5679
|
-
"""Wrap a Runnable with additional functionality.
|
|
5732
|
+
class RunnableBinding(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
|
|
5733
|
+
"""Wrap a ``Runnable`` with additional functionality.
|
|
5680
5734
|
|
|
5681
|
-
A RunnableBinding can be thought of as a "runnable decorator" that
|
|
5682
|
-
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,
|
|
5683
5737
|
and async support, while adding additional functionality.
|
|
5684
5738
|
|
|
5685
|
-
Any class that inherits from Runnable can be bound to a
|
|
5686
|
-
Runnables expose a standard set of methods for creating
|
|
5687
|
-
or sub-classes of
|
|
5688
|
-
|
|
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.
|
|
5689
5743
|
|
|
5690
5744
|
These methods include:
|
|
5691
5745
|
|
|
5692
|
-
- ``bind``: Bind kwargs to pass to the underlying Runnable when running it.
|
|
5693
|
-
- ``with_config``: Bind config to pass to the underlying Runnable when running it.
|
|
5694
|
-
- ``with_listeners``: Bind lifecycle listeners to the underlying Runnable
|
|
5695
|
-
- ``with_types``: Override the input and output types of the underlying Runnable
|
|
5696
|
-
- ``with_retry``: Bind a retry policy to the underlying Runnable
|
|
5697
|
-
- ``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``.
|
|
5698
5752
|
|
|
5699
5753
|
Example:
|
|
5700
|
-
`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.
|
|
5701
5755
|
|
|
5702
5756
|
.. code-block:: python
|
|
5703
5757
|
|
|
@@ -5711,7 +5765,8 @@ class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
|
5711
5765
|
runnable_binding = model.bind(stop=['-'])
|
|
5712
5766
|
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
|
|
5713
5767
|
|
|
5714
|
-
Can also be done by instantiating a RunnableBinding directly (not
|
|
5768
|
+
Can also be done by instantiating a ``RunnableBinding`` directly (not
|
|
5769
|
+
recommended):
|
|
5715
5770
|
|
|
5716
5771
|
.. code-block:: python
|
|
5717
5772
|
|
|
@@ -5722,18 +5777,19 @@ class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
|
5722
5777
|
)
|
|
5723
5778
|
runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot`
|
|
5724
5779
|
|
|
5725
|
-
"""
|
|
5780
|
+
""" # noqa: E501
|
|
5726
5781
|
|
|
5727
5782
|
@override
|
|
5728
5783
|
def bind(self, **kwargs: Any) -> Runnable[Input, Output]:
|
|
5729
|
-
"""Bind additional kwargs to a Runnable
|
|
5784
|
+
"""Bind additional kwargs to a ``Runnable``, returning a new ``Runnable``.
|
|
5730
5785
|
|
|
5731
5786
|
Args:
|
|
5732
|
-
**kwargs: The kwargs to bind to the Runnable
|
|
5787
|
+
**kwargs: The kwargs to bind to the ``Runnable``.
|
|
5733
5788
|
|
|
5734
5789
|
Returns:
|
|
5735
|
-
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,
|
|
5736
5791
|
but with the additional kwargs bound.
|
|
5792
|
+
|
|
5737
5793
|
"""
|
|
5738
5794
|
return self.__class__(
|
|
5739
5795
|
bound=self.bound,
|
|
@@ -5774,22 +5830,22 @@ class RunnableBinding(RunnableBindingBase[Input, Output]):
|
|
|
5774
5830
|
Union[Callable[[Run], None], Callable[[Run, RunnableConfig], None]]
|
|
5775
5831
|
] = None,
|
|
5776
5832
|
) -> Runnable[Input, Output]:
|
|
5777
|
-
"""Bind lifecycle listeners to a Runnable
|
|
5833
|
+
"""Bind lifecycle listeners to a ``Runnable``, returning a new ``Runnable``.
|
|
5778
5834
|
|
|
5779
|
-
The Run object contains information about the run, including its id
|
|
5780
|
-
type
|
|
5781
|
-
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.
|
|
5782
5838
|
|
|
5783
5839
|
Args:
|
|
5784
|
-
on_start: Called before the Runnable starts running, with the Run
|
|
5785
|
-
Defaults to None.
|
|
5786
|
-
on_end: Called after the Runnable finishes running, with the Run
|
|
5787
|
-
Defaults to None.
|
|
5788
|
-
on_error: Called if the Runnable throws an error, with the Run
|
|
5789
|
-
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.
|
|
5790
5846
|
|
|
5791
5847
|
Returns:
|
|
5792
|
-
A new Runnable with the listeners bound.
|
|
5848
|
+
A new ``Runnable`` with the listeners bound.
|
|
5793
5849
|
"""
|
|
5794
5850
|
from langchain_core.tracers.root_listeners import RootListenersTracer
|
|
5795
5851
|
|
|
@@ -5917,16 +5973,16 @@ RunnableLike = Union[
|
|
|
5917
5973
|
|
|
5918
5974
|
|
|
5919
5975
|
def coerce_to_runnable(thing: RunnableLike) -> Runnable[Input, Output]:
|
|
5920
|
-
"""Coerce a Runnable
|
|
5976
|
+
"""Coerce a ``Runnable``-like object into a ``Runnable``.
|
|
5921
5977
|
|
|
5922
5978
|
Args:
|
|
5923
|
-
thing: A Runnable
|
|
5979
|
+
thing: A ``Runnable``-like object.
|
|
5924
5980
|
|
|
5925
5981
|
Returns:
|
|
5926
|
-
A Runnable
|
|
5982
|
+
A ``Runnable``.
|
|
5927
5983
|
|
|
5928
5984
|
Raises:
|
|
5929
|
-
TypeError: If the object is not Runnable
|
|
5985
|
+
TypeError: If the object is not ``Runnable``-like.
|
|
5930
5986
|
"""
|
|
5931
5987
|
if isinstance(thing, Runnable):
|
|
5932
5988
|
return thing
|
|
@@ -5975,16 +6031,16 @@ def chain(
|
|
|
5975
6031
|
Callable[[Input], AsyncIterator[Output]],
|
|
5976
6032
|
],
|
|
5977
6033
|
) -> Runnable[Input, Output]:
|
|
5978
|
-
"""Decorate a function to make it a Runnable
|
|
6034
|
+
"""Decorate a function to make it a ``Runnable``.
|
|
5979
6035
|
|
|
5980
|
-
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.
|
|
5981
6037
|
Any runnables called by the function will be traced as dependencies.
|
|
5982
6038
|
|
|
5983
6039
|
Args:
|
|
5984
|
-
func: A
|
|
6040
|
+
func: A ``Callable``.
|
|
5985
6041
|
|
|
5986
6042
|
Returns:
|
|
5987
|
-
A Runnable
|
|
6043
|
+
A ``Runnable``.
|
|
5988
6044
|
|
|
5989
6045
|
Example:
|
|
5990
6046
|
|