Home / File/ _retry.py — langchain Source File

_retry.py — langchain Source File

Architecture documentation for _retry.py, a python file in the langchain codebase. 3 imports, 0 dependents.

File python LangChainCore ApiManagement 3 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  9decc8a1_63f4_b789_d19f_e00296bc741d["_retry.py"]
  d33d86db_0d65_0c4a_5ba6_a863c29fa1c9["random"]
  9decc8a1_63f4_b789_d19f_e00296bc741d --> d33d86db_0d65_0c4a_5ba6_a863c29fa1c9
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  9decc8a1_63f4_b789_d19f_e00296bc741d --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  9decc8a1_63f4_b789_d19f_e00296bc741d --> feec1ec4_6917_867b_d228_b134d0ff8099
  style 9decc8a1_63f4_b789_d19f_e00296bc741d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Shared retry utilities for agent middleware.

This module contains common constants, utilities, and logic used by both
model and tool retry middleware implementations.
"""

from __future__ import annotations

import random
from collections.abc import Callable
from typing import Literal

# Type aliases
RetryOn = tuple[type[Exception], ...] | Callable[[Exception], bool]
"""Type for specifying which exceptions to retry on.

Can be either:
- A tuple of exception types to retry on (based on `isinstance` checks)
- A callable that takes an exception and returns `True` if it should be retried
"""

OnFailure = Literal["error", "continue"] | Callable[[Exception], str]
"""Type for specifying failure handling behavior.

Can be either:
- A literal action string (`'error'` or `'continue'`)
    - `'error'`: Re-raise the exception, stopping agent execution.
    - `'continue'`: Inject a message with the error details, allowing the agent to continue.
       For tool retries, a `ToolMessage` with the error details will be injected.
       For model retries, an `AIMessage` with the error details will be returned.
- A callable that takes an exception and returns a string for error message content
"""


def validate_retry_params(
    max_retries: int,
    initial_delay: float,
    max_delay: float,
    backoff_factor: float,
) -> None:
    """Validate retry parameters.

    Args:
        max_retries: Maximum number of retry attempts.
        initial_delay: Initial delay in seconds before first retry.
        max_delay: Maximum delay in seconds between retries.
        backoff_factor: Multiplier for exponential backoff.

    Raises:
        ValueError: If any parameter is invalid (negative values).
    """
    if max_retries < 0:
        msg = "max_retries must be >= 0"
        raise ValueError(msg)
    if initial_delay < 0:
        msg = "initial_delay must be >= 0"
        raise ValueError(msg)
    if max_delay < 0:
        msg = "max_delay must be >= 0"
        raise ValueError(msg)
// ... (64 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • random
  • typing

Frequently Asked Questions

What does _retry.py do?
_retry.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, ApiManagement subdomain.
What functions are defined in _retry.py?
_retry.py defines 3 function(s): calculate_delay, should_retry_exception, validate_retry_params.
What does _retry.py depend on?
_retry.py imports 3 module(s): collections.abc, random, typing.
Where is _retry.py in the architecture?
_retry.py is located at libs/langchain_v1/langchain/agents/middleware/_retry.py (domain: LangChainCore, subdomain: ApiManagement, directory: libs/langchain_v1/langchain/agents/middleware).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free