Home / Function/ _handle_failure() — langchain Function Reference

_handle_failure() — langchain Function Reference

Architecture documentation for the _handle_failure() function in model_retry.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5f0811d1_ab8b_7642_81d3_ed55469793d6["_handle_failure()"]
  5e783bd0_a142_0fd6_3238_fc2d9985440d["ModelRetryMiddleware"]
  5f0811d1_ab8b_7642_81d3_ed55469793d6 -->|defined in| 5e783bd0_a142_0fd6_3238_fc2d9985440d
  05371c56_6c46_808b_c839_e45e7d2e464e["wrap_model_call()"]
  05371c56_6c46_808b_c839_e45e7d2e464e -->|calls| 5f0811d1_ab8b_7642_81d3_ed55469793d6
  214769a6_e59c_3497_abe0_3dbaa3e81331["awrap_model_call()"]
  214769a6_e59c_3497_abe0_3dbaa3e81331 -->|calls| 5f0811d1_ab8b_7642_81d3_ed55469793d6
  edb7ba16_b462_070e_af1f_46c3f718cc0a["_format_failure_message()"]
  5f0811d1_ab8b_7642_81d3_ed55469793d6 -->|calls| edb7ba16_b462_070e_af1f_46c3f718cc0a
  style 5f0811d1_ab8b_7642_81d3_ed55469793d6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/model_retry.py lines 190–212

    def _handle_failure(self, exc: Exception, attempts_made: int) -> ModelResponse[ResponseT]:
        """Handle failure when all retries are exhausted.

        Args:
            exc: The exception that caused the failure.
            attempts_made: Number of attempts actually made.

        Returns:
            `ModelResponse` with error details.

        Raises:
            Exception: If `on_failure` is `'error'`, re-raises the exception.
        """
        if self.on_failure == "error":
            raise exc

        if callable(self.on_failure):
            content = self.on_failure(exc)
            ai_msg = AIMessage(content=content)
        else:
            ai_msg = self._format_failure_message(exc, attempts_made)

        return ModelResponse(result=[ai_msg])

Domain

Subdomains

Frequently Asked Questions

What does _handle_failure() do?
_handle_failure() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/model_retry.py.
Where is _handle_failure() defined?
_handle_failure() is defined in libs/langchain_v1/langchain/agents/middleware/model_retry.py at line 190.
What does _handle_failure() call?
_handle_failure() calls 1 function(s): _format_failure_message.
What calls _handle_failure()?
_handle_failure() is called by 2 function(s): awrap_model_call, wrap_model_call.

Analyze Your Own Codebase

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

Try Supermodel Free