Home / Function/ bind_tools() — langchain Function Reference

bind_tools() — langchain Function Reference

Architecture documentation for the bind_tools() function in huggingface.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e5b0db37_7a35_b8c7_c069_a78188b87396["bind_tools()"]
  8cf0d6c0_abf8_3ee2_fd00_8bfc8c02058a["ChatHuggingFace"]
  e5b0db37_7a35_b8c7_c069_a78188b87396 -->|defined in| 8cf0d6c0_abf8_3ee2_fd00_8bfc8c02058a
  66ab56cc_d97c_b9a3_ace4_750c0873eaf8["with_structured_output()"]
  66ab56cc_d97c_b9a3_ace4_750c0873eaf8 -->|calls| e5b0db37_7a35_b8c7_c069_a78188b87396
  style e5b0db37_7a35_b8c7_c069_a78188b87396 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py lines 1032–1088

    def bind_tools(
        self,
        tools: Sequence[dict[str, Any] | type | Callable | BaseTool],
        *,
        tool_choice: dict | str | bool | None = None,
        **kwargs: Any,
    ) -> Runnable[LanguageModelInput, AIMessage]:
        """Bind tool-like objects to this chat model.

        Assumes model is compatible with OpenAI tool-calling API.

        Args:
            tools: A list of tool definitions to bind to this chat model.

                Supports any tool definition handled by [`convert_to_openai_tool`][langchain_core.utils.function_calling.convert_to_openai_tool].
            tool_choice: Which tool to require the model to call.
                Must be the name of the single provided function or
                `'auto'` to automatically determine which function to call
                (if any), or a dict of the form:
                {"type": "function", "function": {"name": <<tool_name>>}}.
            **kwargs: Any additional parameters to pass to the
                `langchain.runnable.Runnable` constructor.
        """  # noqa: E501
        formatted_tools = [convert_to_openai_tool(tool) for tool in tools]
        if tool_choice is not None and tool_choice:
            if len(formatted_tools) != 1:
                msg = (
                    "When specifying `tool_choice`, you must provide exactly one "
                    f"tool. Received {len(formatted_tools)} tools."
                )
                raise ValueError(msg)
            if isinstance(tool_choice, str):
                if tool_choice not in ("auto", "none", "required"):
                    tool_choice = {
                        "type": "function",
                        "function": {"name": tool_choice},
                    }
            elif isinstance(tool_choice, bool):
                tool_choice = formatted_tools[0]
            elif isinstance(tool_choice, dict):
                if (
                    formatted_tools[0]["function"]["name"]
                    != tool_choice["function"]["name"]
                ):
                    msg = (
                        f"Tool choice {tool_choice} was specified, but the only "
                        f"provided tool was {formatted_tools[0]['function']['name']}."
                    )
                    raise ValueError(msg)
            else:
                msg = (
                    f"Unrecognized tool_choice type. Expected str, bool or dict. "
                    f"Received: {tool_choice}"
                )
                raise ValueError(msg)
            kwargs["tool_choice"] = tool_choice
        return super().bind(tools=formatted_tools, **kwargs)

Domain

Subdomains

Frequently Asked Questions

What does bind_tools() do?
bind_tools() is a function in the langchain codebase, defined in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py.
Where is bind_tools() defined?
bind_tools() is defined in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py at line 1032.
What calls bind_tools()?
bind_tools() is called by 1 function(s): with_structured_output.

Analyze Your Own Codebase

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

Try Supermodel Free