Runnable to passthrough inputs unchanged or with additional keys.
This Runnable behaves almost like the identity function, except that it
can be configured to add additional keys to the output, if the input is a
dict.
The examples below demonstrate this Runnable works using a few simple
chains. The chains rely on simple lambdas to make the examples easy to execute
and experiment with.
RunnablePassthrough(
self,
func: Callable[[Other], None] | Callable[[Other, RunnableConfig], None] | Callable[[Other], Awaitable[None]] | Callable[[Other, RunnableConfig], Awaitable[None]] | None = None,
afunc: Callable[[Other], Awaitable[None]] | Callable[[Other, RunnableConfig], Awaitable[None]] | None = None,
*,
input_type: type[Other] | None = None,
**kwargs: Any = {}
)In some cases, it may be useful to pass the input through while adding some
keys to the output. In this case, you can use the assign method:
from langchain_core.runnables import RunnablePassthrough
def fake_llm(prompt: str) -> str: # Fake LLM for the example
return "completion"
runnable = {
"llm1": fake_llm,
"llm2": fake_llm,
} | RunnablePassthrough.assign(
total_chars=lambda inputs: len(inputs["llm1"] + inputs["llm2"])
)
runnable.invoke("hello")
# {'llm1': 'completion', 'llm2': 'completion', 'total_chars': 20}| Name | Type | Description |
|---|---|---|
func | Callable[[Other], None] | Callable[[Other, RunnableConfig], None] | Callable[[Other], Awaitable[None]] | Callable[[Other, RunnableConfig], Awaitable[None]] | None | Default: NoneFunction to be called with the input. |
afunc | Callable[[Other], Awaitable[None]] | Callable[[Other, RunnableConfig], Awaitable[None]] | None | Default: NoneAsync function to be called with the input. |
input_type | type[Other] | None | Default: NoneType of the input. |
| Name | Type |
|---|---|
| func | Callable[[Other], None] | Callable[[Other, RunnableConfig], None] | Callable[[Other], Awaitable[None]] | Callable[[Other, RunnableConfig], Awaitable[None]] | None |
| afunc | Callable[[Other], Awaitable[None]] | Callable[[Other, RunnableConfig], Awaitable[None]] | None |
| input_type | type[Other] | None |
Get a JSON schema that represents the input to the Runnable.
Get a JSON schema that represents the output of the Runnable.
The type of config this Runnable accepts specified as a Pydantic model.
Get a JSON schema that represents the config of the Runnable.
Return a list of prompts used by this Runnable.
Pipe Runnable objects.
Pick keys from the output dict of this Runnable.
Run invoke in parallel on a list of inputs.
Run ainvoke in parallel on a list of inputs.
Stream all output from a Runnable, as reported to the callback system.
Generate a stream of events.
Bind arguments to a Runnable, returning a new Runnable.
Bind lifecycle listeners to a Runnable, returning a new Runnable.
Bind async lifecycle listeners to a Runnable.
Bind input and output types to a Runnable, returning a new Runnable.
Create a new Runnable that retries the original Runnable on exceptions.
Map a function to multiple iterables.
Add fallbacks to a Runnable, returning a new Runnable.
Create a BaseTool from a Runnable.