Home / Function/ awrap_model_call() — langchain Function Reference

awrap_model_call() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/model_retry.py lines 264–312

    async def awrap_model_call(
        self,
        request: ModelRequest[ContextT],
        handler: Callable[[ModelRequest[ContextT]], Awaitable[ModelResponse[ResponseT]]],
    ) -> ModelResponse[ResponseT] | AIMessage:
        """Intercept and control async model execution with retry logic.

        Args:
            request: Model request with model, messages, state, and runtime.
            handler: Async callable to execute the model and returns `ModelResponse`.

        Returns:
            `ModelResponse` or `AIMessage` (the final result).

        Raises:
            RuntimeError: If the retry loop completes without returning. (This should not happen.)
        """
        # Initial attempt + retries
        for attempt in range(self.max_retries + 1):
            try:
                return await handler(request)
            except Exception as exc:
                attempts_made = attempt + 1  # attempt is 0-indexed

                # Check if we should retry this exception
                if not should_retry_exception(exc, self.retry_on):
                    # Exception is not retryable, handle failure immediately
                    return self._handle_failure(exc, attempts_made)

                # Check if we have more retries left
                if attempt < self.max_retries:
                    # Calculate and apply backoff delay
                    delay = calculate_delay(
                        attempt,
                        backoff_factor=self.backoff_factor,
                        initial_delay=self.initial_delay,
                        max_delay=self.max_delay,
                        jitter=self.jitter,
                    )
                    if delay > 0:
                        await asyncio.sleep(delay)
                    # Continue to next retry
                else:
                    # No more retries, handle failure
                    return self._handle_failure(exc, attempts_made)

        # Unreachable: loop always returns via handler success or _handle_failure
        msg = "Unexpected: retry loop completed without returning"
        raise RuntimeError(msg)

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free