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_retry
    Method●Since v0.1

    with_retry

    Create a new Runnable that retries the original Runnable on exceptions.

    Copy
    with_retry(
      self,
      *,
      retry_if_exception_type: tuple[type[BaseException], ...] = (Exception,),
      wait_exponential_jitter: bool = True,
      exponential_jitter_params: ExponentialJitterParams | None = None,
      stop_after_attempt: int = 3
    ) -> Runnable[Input, Output]

    Example:

    from langchain_core.runnables import RunnableLambda
    
    count = 0
    
    def _lambda(x: int) -> None:
        global count
        count = count + 1
        if x == 1:
            raise ValueError("x is 1")
        else:
            pass
    
    runnable = RunnableLambda(_lambda)
    try:
        runnable.with_retry(
            stop_after_attempt=2,
            retry_if_exception_type=(ValueError,),
        ).invoke(1)
    except ValueError:
        pass
    
    assert count == 2

    Parameters

    NameTypeDescription
    retry_if_exception_typetuple[type[BaseException], ...]
    Default:(Exception,)

    A tuple of exception types to retry on.

    wait_exponential_jitterbool
    Default:True

    Whether to add jitter to the wait time between retries.

    stop_after_attemptint
    Default:3

    The maximum number of attempts to make before giving up.

    exponential_jitter_paramsExponentialJitterParams | None
    Default:None

    Parameters for tenacity.wait_exponential_jitter. Namely: initial, max, exp_base, and jitter (all float values).

    View source on GitHub