Home / Function/ wrap_model_call() — langchain Function Reference

wrap_model_call() — langchain Function Reference

Architecture documentation for the wrap_model_call() function in model_fallback.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  33aebc2c_08b3_3401_6d19_c055a215c08c["wrap_model_call()"]
  eb36575f_ada3_a10f_f140_390f57f60565["ModelFallbackMiddleware"]
  33aebc2c_08b3_3401_6d19_c055a215c08c -->|defined in| eb36575f_ada3_a10f_f140_390f57f60565
  style 33aebc2c_08b3_3401_6d19_c055a215c08c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/model_fallback.py lines 72–104

    def wrap_model_call(
        self,
        request: ModelRequest[ContextT],
        handler: Callable[[ModelRequest[ContextT]], ModelResponse[ResponseT]],
    ) -> ModelResponse[ResponseT] | AIMessage:
        """Try fallback models in sequence on errors.

        Args:
            request: Initial model request.
            handler: Callback to execute the model.

        Returns:
            AIMessage from successful model call.

        Raises:
            Exception: If all models fail, re-raises last exception.
        """
        # Try primary model first
        last_exception: Exception
        try:
            return handler(request)
        except Exception as e:
            last_exception = e

        # Try fallback models
        for fallback_model in self.models:
            try:
                return handler(request.override(model=fallback_model))
            except Exception as e:
                last_exception = e
                continue

        raise last_exception

Domain

Subdomains

Frequently Asked Questions

What does wrap_model_call() do?
wrap_model_call() is a function in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/model_fallback.py.
Where is wrap_model_call() defined?
wrap_model_call() is defined in libs/langchain_v1/langchain/agents/middleware/model_fallback.py at line 72.

Analyze Your Own Codebase

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

Try Supermodel Free