Home / Function/ wrap_model_call() — langchain Function Reference

wrap_model_call() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  81fe6917_dcc1_9d58_de04_76b81cb1bf2a["wrap_model_call()"]
  9e579a9b_76f2_b0cc_bcc3_90d92b770762["StructuredOutputRetryMiddleware"]
  81fe6917_dcc1_9d58_de04_76b81cb1bf2a -->|defined in| 9e579a9b_76f2_b0cc_bcc3_90d92b770762
  style 81fe6917_dcc1_9d58_de04_76b81cb1bf2a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_structured_output_retry.py lines 32–64

    def wrap_model_call(
        self, request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
    ) -> ModelResponse:
        """Intercept and control model execution via handler callback.

        Args:
            request: The model request containing messages and configuration.
            handler: The function to call the model.

        Returns:
            The model response.

        Raises:
            StructuredOutputError: If max retries exceeded without success.
        """
        for attempt in range(self.max_retries + 1):
            try:
                return handler(request)
            except StructuredOutputError as exc:
                if attempt == self.max_retries:
                    raise

                # Include both the AI message and error in a single human message
                # to maintain valid chat history alternation
                ai_content = exc.ai_message.content
                error_message = (
                    f"Your previous response was:\n{ai_content}\n\n"
                    f"Error: {exc}. Please try again with a valid response."
                )
                request.messages.append(HumanMessage(content=error_message))

        # This should never be reached, but satisfies type checker
        return handler(request)

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/tests/unit_tests/agents/middleware/implementations/test_structured_output_retry.py.
Where is wrap_model_call() defined?
wrap_model_call() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_structured_output_retry.py at line 32.

Analyze Your Own Codebase

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

Try Supermodel Free