Home / Function/ test_max_retries() — langchain Function Reference

test_max_retries() — langchain Function Reference

Architecture documentation for the test_max_retries() function in test_wrap_model_call.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  eedf4bc6_5e98_0645_1eaf_5284b71916c9["test_max_retries()"]
  1c3bae87_d793_bb0b_1a39_594b67feb9d4["TestRetryLogic"]
  eedf4bc6_5e98_0645_1eaf_5284b71916c9 -->|defined in| 1c3bae87_d793_bb0b_1a39_594b67feb9d4
  style eedf4bc6_5e98_0645_1eaf_5284b71916c9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py lines 153–199

    def test_max_retries(self) -> None:
        """Test middleware with maximum retry limit."""

        class AlwaysFailModel(GenericFakeChatModel):
            @override
            def _generate(
                self,
                messages: list[BaseMessage],
                stop: list[str] | None = None,
                run_manager: CallbackManagerForLLMRun | None = None,
                **kwargs: Any,
            ) -> ChatResult:
                msg = "Always fails"
                raise ValueError(msg)

        class MaxRetriesMiddleware(AgentMiddleware):
            def __init__(self, max_retries: int = 3):
                super().__init__()
                self.max_retries = max_retries
                self.attempts = []

            def wrap_model_call(
                self,
                request: ModelRequest,
                handler: Callable[[ModelRequest], ModelResponse],
            ) -> ModelCallResult:
                last_exception = None
                for attempt in range(self.max_retries):
                    self.attempts.append(attempt + 1)
                    try:
                        return handler(request)
                    except Exception as e:
                        last_exception = e
                        continue
                # Re-raise the last exception
                if last_exception:
                    raise last_exception
                pytest.fail("Should have raised an exception")

        retry_middleware = MaxRetriesMiddleware(max_retries=3)
        model = AlwaysFailModel(messages=iter([]))
        agent = create_agent(model=model, middleware=[retry_middleware])

        with pytest.raises(ValueError, match="Always fails"):
            agent.invoke({"messages": [HumanMessage("Test")]})

        assert retry_middleware.attempts == [1, 2, 3]

Domain

Subdomains

Frequently Asked Questions

What does test_max_retries() do?
test_max_retries() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py.
Where is test_max_retries() defined?
test_max_retries() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py at line 153.

Analyze Your Own Codebase

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

Try Supermodel Free