Home / File/ test_tool_retry.py — langchain Source File

test_tool_retry.py — langchain Source File

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

File python LangChainCore MessageInterface 14 imports 33 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b["test_tool_retry.py"]
  996b2db9_46dd_901f_f7eb_068bafab4b12["time"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 996b2db9_46dd_901f_f7eb_068bafab4b12
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> feec1ec4_6917_867b_d228_b134d0ff8099
  f69d6389_263d_68a4_7fbf_f14c0602a9ba["pytest"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> f69d6389_263d_68a4_7fbf_f14c0602a9ba
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  121262a1_0bd6_d637_bce3_307ab6b3ecd4["langchain_core.tools"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 121262a1_0bd6_d637_bce3_307ab6b3ecd4
  2ff5ef5d_5050_1ab2_e4f2_f72e391945a2["langgraph.checkpoint.memory"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 2ff5ef5d_5050_1ab2_e4f2_f72e391945a2
  24578fe3_c3c0_70ca_0624_6be4e15ad364["langgraph.prebuilt.tool_node"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 24578fe3_c3c0_70ca_0624_6be4e15ad364
  726d73c8_7999_511a_c1ec_4524fb1cc31a["langgraph.types"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 726d73c8_7999_511a_c1ec_4524fb1cc31a
  998e41c8_6a3e_eb9b_699f_db967a1242f7["langchain.agents.factory"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 998e41c8_6a3e_eb9b_699f_db967a1242f7
  1d2dc1c1_d133_ba97_472a_386346d9e12e["langchain.agents.middleware._retry"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 1d2dc1c1_d133_ba97_472a_386346d9e12e
  4bd2cb36_c0db_ee99_4324_fa1ebadbbb1c["langchain.agents.middleware.tool_retry"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 4bd2cb36_c0db_ee99_4324_fa1ebadbbb1c
  a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> a681398d_ed44_c914_1a44_5d174223b069
  069947d2_727b_035a_0691_c12203e2f5a6["tests.unit_tests.agents.model"]
  c71b26df_821f_59ac_c7ef_3b96fcbe0d5b --> 069947d2_727b_035a_0691_c12203e2f5a6
  style c71b26df_821f_59ac_c7ef_3b96fcbe0d5b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Tests for ToolRetryMiddleware functionality."""

import time
from collections.abc import Callable
from typing import Any

import pytest
from langchain_core.messages import HumanMessage, ToolCall, ToolMessage
from langchain_core.tools import tool
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.prebuilt.tool_node import ToolCallRequest
from langgraph.types import Command

from langchain.agents.factory import create_agent
from langchain.agents.middleware._retry import calculate_delay
from langchain.agents.middleware.tool_retry import ToolRetryMiddleware
from langchain.agents.middleware.types import wrap_tool_call
from tests.unit_tests.agents.model import FakeToolCallingModel


@tool
def working_tool(value: str) -> str:
    """Tool that always succeeds."""
    return f"Success: {value}"


@tool
def failing_tool(value: str) -> str:
    """Tool that always fails."""
    msg = f"Failed: {value}"
    raise ValueError(msg)


class TemporaryFailureTool:
    """Tool that fails a certain number of times before succeeding."""

    def __init__(self, fail_count: int):
        """Initialize with the number of times to fail.

        Args:
            fail_count: Number of times to fail before succeeding.
        """
        self.fail_count = fail_count
        self.attempt = 0

    def __call__(self, value: str) -> str:
        """Execute the tool.

        Args:
            value: Input string.

        Returns:
            Success message if attempt >= fail_count.

        Raises:
            ValueError: If attempt < fail_count.
        """
        self.attempt += 1
        if self.attempt <= self.fail_count:
            msg = f"Temporary failure {self.attempt}"
// ... (952 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • langchain.agents.factory
  • langchain.agents.middleware._retry
  • langchain.agents.middleware.tool_retry
  • langchain.agents.middleware.types
  • langchain_core.messages
  • langchain_core.tools
  • langgraph.checkpoint.memory
  • langgraph.prebuilt.tool_node
  • langgraph.types
  • pytest
  • tests.unit_tests.agents.model
  • time
  • typing

Frequently Asked Questions

What does test_tool_retry.py do?
test_tool_retry.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, MessageInterface subdomain.
What functions are defined in test_tool_retry.py?
test_tool_retry.py defines 33 function(s): failing_tool, test_tool_retry_async_backoff_timing, test_tool_retry_async_failing_tool, test_tool_retry_async_succeeds_after_retries, test_tool_retry_async_working_tool, test_tool_retry_backoff_timing, test_tool_retry_constant_backoff, test_tool_retry_custom_exception_filter, test_tool_retry_custom_failure_formatter, test_tool_retry_deprecated_raise_behavior, and 23 more.
What does test_tool_retry.py depend on?
test_tool_retry.py imports 14 module(s): collections.abc, langchain.agents.factory, langchain.agents.middleware._retry, langchain.agents.middleware.tool_retry, langchain.agents.middleware.types, langchain_core.messages, langchain_core.tools, langgraph.checkpoint.memory, and 6 more.
Where is test_tool_retry.py in the architecture?
test_tool_retry.py is located at libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_tool_retry.py (domain: LangChainCore, subdomain: MessageInterface, directory: libs/langchain_v1/tests/unit_tests/agents/middleware/implementations).

Analyze Your Own Codebase

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

Try Supermodel Free