Home / Function/ awrap_model_call() — langchain Function Reference

awrap_model_call() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  914b6a96_704c_ef23_6236_b6d24e64245f["awrap_model_call()"]
  888c953d_8f6f_ac7f_4274_ca04589e186a["LLMToolSelectorMiddleware"]
  914b6a96_704c_ef23_6236_b6d24e64245f -->|defined in| 888c953d_8f6f_ac7f_4274_ca04589e186a
  bdf758af_80be_0125_ef7b_855624a8a30a["_prepare_selection_request()"]
  914b6a96_704c_ef23_6236_b6d24e64245f -->|calls| bdf758af_80be_0125_ef7b_855624a8a30a
  963a6e36_74b3_e68c_c642_f81d47a7666b["_process_selection_response()"]
  914b6a96_704c_ef23_6236_b6d24e64245f -->|calls| 963a6e36_74b3_e68c_c642_f81d47a7666b
  80d478df_8f9e_226e_c90e_c056581780f1["_create_tool_selection_response()"]
  914b6a96_704c_ef23_6236_b6d24e64245f -->|calls| 80d478df_8f9e_226e_c90e_c056581780f1
  style 914b6a96_704c_ef23_6236_b6d24e64245f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/tool_selection.py lines 317–358

    async def awrap_model_call(
        self,
        request: ModelRequest[ContextT],
        handler: Callable[[ModelRequest[ContextT]], Awaitable[ModelResponse[ResponseT]]],
    ) -> ModelResponse[ResponseT] | AIMessage:
        """Filter tools based on LLM selection before invoking the model via handler.

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

        Returns:
            The model call result.

        Raises:
            AssertionError: If the selection model response is not a dict.
        """
        selection_request = self._prepare_selection_request(request)
        if selection_request is None:
            return await handler(request)

        # Create dynamic response model with Literal enum of available tool names
        type_adapter = _create_tool_selection_response(selection_request.available_tools)
        schema = type_adapter.json_schema()
        structured_model = selection_request.model.with_structured_output(schema)

        response = await structured_model.ainvoke(
            [
                {"role": "system", "content": selection_request.system_message},
                selection_request.last_user_message,
            ]
        )

        # Response should be a dict since we're passing a schema (not a Pydantic model class)
        if not isinstance(response, dict):
            msg = f"Expected dict response, got {type(response)}"
            raise AssertionError(msg)  # noqa: TRY004
        modified_request = self._process_selection_response(
            response, selection_request.available_tools, selection_request.valid_tool_names, request
        )
        return await handler(modified_request)

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/tool_selection.py.
Where is awrap_model_call() defined?
awrap_model_call() is defined in libs/langchain_v1/langchain/agents/middleware/tool_selection.py at line 317.
What does awrap_model_call() call?
awrap_model_call() calls 3 function(s): _create_tool_selection_response, _prepare_selection_request, _process_selection_response.

Analyze Your Own Codebase

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

Try Supermodel Free