class RunnableWithFallbacksRunnable<RunInput, RunOutput>Internal method that handles batching and configuration for a runnable
Default streaming implementation.
Assigns new fields to the dict output of this runnable. Returns a new runnable.
Convert a runnable to a tool. Return a new instance of RunnableToolLike
Default implementation of batch, which calls invoke N times.
Method to invoke the document transformation. This method calls the
Pick keys from the dict output of this runnable. Returns a new runnable.
Create a new runnable sequence that runs each individual runnable in series,
Stream output in chunks.
Generate a stream of events emitted by the internal steps of the runnable.
Stream all output from a runnable, as reported to the callback system.
Default implementation of transform, which buffers input and then calls stream.
Bind config to a Runnable, returning a new Runnable.
Create a new runnable from the current one that will try invoking
Bind lifecycle listeners to a Runnable, returning a new Runnable.
Add retry logic to an existing runnable.
The name of the serializable. Override to provide an alias or
A path to the module that contains the class, eg. ["langchain", "llms"]
import {
RunnableLambda,
RunnableWithFallbacks,
} from "@langchain/core/runnables";
const primaryOperation = (input: string): string => {
if (input !== "safe") {
throw new Error("Primary operation failed due to unsafe input");
}
return `Processed: ${input}`;
};
// Define a fallback operation that processes the input differently
const fallbackOperation = (input: string): string =>
`Fallback processed: ${input}`;
const primaryRunnable = RunnableLambda.from(primaryOperation);
const fallbackRunnable = RunnableLambda.from(fallbackOperation);
// Apply the fallback logic using the .withFallbacks() method
const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
// Alternatively, create a RunnableWithFallbacks instance manually
const manualFallbackChain = new RunnableWithFallbacks({
runnable: primaryRunnable,
fallbacks: [fallbackRunnable],
});
// Example invocation using .withFallbacks()
const res = await runnableWithFallback
.invoke("unsafe input")
.catch((error) => {
console.error("Failed after all attempts:", error.message);
});
// "Fallback processed: unsafe input"
// Example invocation using manual instantiation
const res = await manualFallbackChain
.invoke("safe")
.catch((error) => {
console.error("Failed after all attempts:", error.message);
});
// "Processed: safe"A path to the module that contains the class, eg. ["langchain", "llms"] Usually should be the same as the entrypoint the class is exported from.
A Runnable that can fallback to other Runnables if it fails. External APIs (e.g., APIs for a language model) may at times experience degraded performance or even downtime.
In these cases, it can be useful to have a fallback Runnable that can be used in place of the original Runnable (e.g., fallback to another LLM provider).
Fallbacks can be defined at the level of a single Runnable, or at the level of a chain of Runnables. Fallbacks are tried in order until one succeeds or all fail.
While you can instantiate a RunnableWithFallbacks directly, it is usually
more convenient to use the withFallbacks method on an existing Runnable.
When streaming, fallbacks will only be called on failures during the initial stream creation. Errors that occur after a stream starts will not fallback to the next Runnable.