LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
    • Overview
    • Caches
    • Callbacks
    • Documents
    • Document loaders
    • Embeddings
    • Exceptions
    • Language models
    • Serialization
    • Output parsers
    • Prompts
    • Rate limiters
    • Retrievers
    • Runnables
    • Utilities
    • Vector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    OverviewCachesCallbacksDocumentsDocument loadersEmbeddingsExceptionsLanguage modelsSerializationOutput parsersPromptsRate limitersRetrieversRunnablesUtilitiesVector stores
    MCP Adapters
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-corerunnablesbaseRunnablewith_fallbacks
    Methodā—Since v0.1

    with_fallbacks

    Copy
    with_fallbacks(
      self,
      fallbacks: Sequence[Runnable[Input, Output]],
      *,
      exceptions_to_handle:
    View source on GitHub
    tuple
    [
    type
    [
    BaseException
    ]
    ,
    .
    .
    .
    ]
    =
    (
    Exception
    ,
    )
    ,
    exception_key
    :
    str
    |
    None
    =
    None
    )
    ->
    RunnableWithFallbacksT
    [
    Input
    ,
    Output
    ]

    Parameters

    NameTypeDescription
    fallbacks*Sequence[Runnable[Input, Output]]

    A sequence of runnables to try if the original Runnable fails.

    exceptions_to_handletuple[type[BaseException], ...]
    Default:(Exception,)
    exception_keystr | None
    Default:None
    fallbacks*Sequence[Runnable[Input, Output]]
    exceptions_to_handletuple[type[BaseException], ...]
    Default:(Exception,)
    exception_keystr | None
    Default:None

    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.

    Example:

    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

    A tuple of exception types to handle.

    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.

    A sequence of runnables to try if the original Runnable fails.

    A tuple of exception types to handle.

    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.