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

    Chat LangChain

    What can I help with?

    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 v1.8

    with_retry

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

    Parameters

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

    A tuple of exception types to retry on, or a callable that receives the raised exception and returns whether to retry it.

    wait_exponential_jitterbool
    Default:True
    stop_after_attemptint
    Default:3
    exponential_jitter_paramsExponentialJitterParams | None
    Default:None

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

    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

    Whether to add jitter to the wait time between retries.

    The maximum number of attempts to make before giving up.

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