with_fallbacks() — langchain Function Reference
Architecture documentation for the with_fallbacks() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 669b4a45_585f_aaba_6b12_00430dc4546a["with_fallbacks()"] 4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"] 669b4a45_585f_aaba_6b12_00430dc4546a -->|defined in| 4a62481c_02cb_a5de_1833_50669d5351a6 style 669b4a45_585f_aaba_6b12_00430dc4546a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/runnables/base.py lines 1947–2023
def with_fallbacks(
self,
fallbacks: Sequence[Runnable[Input, Output]],
*,
exceptions_to_handle: tuple[type[BaseException], ...] = (Exception,),
exception_key: str | None = None,
) -> RunnableWithFallbacksT[Input, Output]:
"""Add fallbacks to a `Runnable`, returning a new `Runnable`.
The new `Runnable` will try the original `Runnable`, and then each fallback
in order, upon failures.
Args:
fallbacks: A sequence of runnables to try if the original `Runnable`
fails.
exceptions_to_handle: A tuple of exception types to handle.
exception_key: If `string` is specified then handled exceptions will be
passed to fallbacks as part of the input under the specified key.
If `None`, exceptions will not be passed to fallbacks.
If used, the base `Runnable` and its fallbacks must accept a
dictionary as input.
Returns:
A new `Runnable` that will try the original `Runnable`, and then each
Fallback in order, upon failures.
Example:
```python
from typing import Iterator
from langchain_core.runnables import RunnableGenerator
def _generate_immediate_error(input: Iterator) -> Iterator[str]:
raise ValueError()
yield ""
def _generate(input: Iterator) -> Iterator[str]:
yield from "foo bar"
runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
[RunnableGenerator(_generate)]
)
print("".join(runnable.stream({}))) # foo bar
```
Args:
fallbacks: A sequence of runnables to try if the original `Runnable`
fails.
exceptions_to_handle: A tuple of exception types to handle.
exception_key: If `string` is specified then handled exceptions will be
passed to fallbacks as part of the input under the specified key.
If `None`, exceptions will not be passed to fallbacks.
If used, the base `Runnable` and its fallbacks must accept a
dictionary as input.
Returns:
A new `Runnable` that will try the original `Runnable`, and then each
Fallback in order, upon failures.
"""
# Import locally to prevent circular import
from langchain_core.runnables.fallbacks import ( # noqa: PLC0415
RunnableWithFallbacks,
)
return RunnableWithFallbacks(
runnable=self,
fallbacks=fallbacks,
exceptions_to_handle=exceptions_to_handle,
exception_key=exception_key,
)
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does with_fallbacks() do?
with_fallbacks() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/base.py.
Where is with_fallbacks() defined?
with_fallbacks() is defined in libs/core/langchain_core/runnables/base.py at line 1947.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free