Home / Function/ test_retry_with_state_tracking() — langchain Function Reference

test_retry_with_state_tracking() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  f3897da6_d8aa_f65f_fba2_6fce0f163483["test_retry_with_state_tracking()"]
  2e06bf85_c70b_137b_2073_10124e82a7df["TestStateAndRuntime"]
  f3897da6_d8aa_f65f_fba2_6fce0f163483 -->|defined in| 2e06bf85_c70b_137b_2073_10124e82a7df
  style f3897da6_d8aa_f65f_fba2_6fce0f163483 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 634–675

    def test_retry_with_state_tracking(self) -> None:
        """Test middleware that tracks retry count in state."""

        class StateTrackingRetryMiddleware(AgentMiddleware):
            def wrap_model_call(
                self,
                request: ModelRequest,
                handler: Callable[[ModelRequest], ModelResponse],
            ) -> ModelCallResult:
                max_retries = 2
                for attempt in range(max_retries):
                    try:
                        return handler(request)
                    except Exception:
                        if attempt == max_retries - 1:
                            raise
                pytest.fail("Should have raised an exception")

        call_count = {"value": 0}

        class FailOnceThenSucceed(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"] == 1:
                    msg = "First fails"
                    raise ValueError(msg)
                return super()._generate(messages, **kwargs)

        model = FailOnceThenSucceed(messages=iter([AIMessage(content="Success")]))
        agent = create_agent(model=model, middleware=[StateTrackingRetryMiddleware()])

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

        assert call_count["value"] == 2  # Failed once, succeeded second time
        assert result["messages"][1].content == "Success"

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free