Home / Function/ awrap_model_call() — langchain Function Reference

awrap_model_call() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  a82f3144_1fa4_c932_621e_e6113ce98c1c["awrap_model_call()"]
  949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e["AgentMiddleware"]
  a82f3144_1fa4_c932_621e_e6113ce98c1c -->|defined in| 949c7cf4_56fe_f3b4_cd89_9631a7e9cb1e
  style a82f3144_1fa4_c932_621e_e6113ce98c1c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/types.py lines 573–623

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

        The handler callback executes the model request and returns a `ModelResponse`.

        Middleware can call the handler multiple times for retry logic, skip calling
        it to short-circuit, or modify the request/response. Multiple middleware
        compose with first in list as outermost layer.

        Args:
            request: Model request to execute (includes state and runtime).
            handler: Async callback that executes the model request and returns
                `ModelResponse`.

                Call this to execute the model.

                Can be called multiple times for retry logic.

                Can skip calling it to short-circuit.

        Returns:
            The model call result.

        Examples:
            !!! example "Retry on error"

                ```python
                async def awrap_model_call(self, request, handler):
                    for attempt in range(3):
                        try:
                            return await handler(request)
                        except Exception:
                            if attempt == 2:
                                raise
                ```
        """
        msg = (
            "Asynchronous implementation of awrap_model_call is not available. "
            "You are likely encountering this error because you defined only the sync version "
            "(wrap_model_call) and invoked your agent in an asynchronous context "
            "(e.g., using `astream()` or `ainvoke()`). "
            "To resolve this, either: "
            "(1) subclass AgentMiddleware and implement the asynchronous awrap_model_call method, "
            "(2) use the @wrap_model_call decorator on a standalone async function, or "
            "(3) invoke your agent synchronously using `stream()` or `invoke()`."
        )
        raise NotImplementedError(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/types.py.
Where is awrap_model_call() defined?
awrap_model_call() is defined in libs/langchain_v1/langchain/agents/middleware/types.py at line 573.

Analyze Your Own Codebase

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

Try Supermodel Free