Home / Function/ test_decorator_complex_retry_logic() — langchain Function Reference

test_decorator_complex_retry_logic() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  a5eebc23_3db7_3503_0153_fcbedb8c1e47["test_decorator_complex_retry_logic()"]
  5a3c6db0_0302_046a_d217_22fb3d4e632e["TestWrapModelCallDecorator"]
  a5eebc23_3db7_3503_0153_fcbedb8c1e47 -->|defined in| 5a3c6db0_0302_046a_d217_22fb3d4e632e
  style a5eebc23_3db7_3503_0153_fcbedb8c1e47 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 1303–1346

    def test_decorator_complex_retry_logic(self) -> None:
        """Test decorator with complex retry logic and backoff."""
        attempts = []
        call_count = {"value": 0}

        class UnreliableModel(GenericFakeChatModel):
            @override
            def _generate(
                self,
                messages: list[BaseMessage],
                stop: list[str] | None = None,
                run_manager: CallbackManagerForLLMRun | None = None,
                **kwargs: Any,
            ) -> ChatResult:
                call_count["value"] += 1
                if call_count["value"] <= 2:
                    msg = f"Attempt {call_count['value']} failed"
                    raise ValueError(msg)
                return super()._generate(messages, **kwargs)

        @wrap_model_call
        def retry_with_tracking(
            request: ModelRequest,
            handler: Callable[[ModelRequest], ModelResponse],
        ) -> ModelCallResult:
            max_retries = 3
            for attempt in range(max_retries):
                attempts.append(attempt + 1)
                try:
                    return handler(request)
                except Exception:
                    # On error, continue to next attempt
                    if attempt < max_retries - 1:
                        continue  # Retry
                    raise  # All retries failed
            pytest.fail("Should have raised an exception")

        model = UnreliableModel(messages=iter([AIMessage(content="Finally worked")]))
        agent = create_agent(model=model, middleware=[retry_with_tracking])

        result = agent.invoke({"messages": [HumanMessage("Test")]})

        assert attempts == [1, 2, 3]
        assert result["messages"][1].content == "Finally worked"

Domain

Subdomains

Frequently Asked Questions

What does test_decorator_complex_retry_logic() do?
test_decorator_complex_retry_logic() 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_decorator_complex_retry_logic() defined?
test_decorator_complex_retry_logic() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_wrap_model_call.py at line 1303.

Analyze Your Own Codebase

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

Try Supermodel Free