Home / Function/ test_bind_runnables_as_tools() — langchain Function Reference

test_bind_runnables_as_tools() — langchain Function Reference

Architecture documentation for the test_bind_runnables_as_tools() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  401b4169_75eb_ad8d_39d6_1c2ec72849b1["test_bind_runnables_as_tools()"]
  971e928f_9c9b_ce7a_b93d_e762f2f5aa54["ChatModelIntegrationTests"]
  401b4169_75eb_ad8d_39d6_1c2ec72849b1 -->|defined in| 971e928f_9c9b_ce7a_b93d_e762f2f5aa54
  style 401b4169_75eb_ad8d_39d6_1c2ec72849b1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/standard-tests/langchain_tests/integration_tests/chat_models.py lines 1677–1740

    def test_bind_runnables_as_tools(self, model: BaseChatModel) -> None:
        """Test bind runnables as tools.

        Test that the model generates tool calls for tools that are derived from
        LangChain runnables. This test is skipped if the `has_tool_calling` property
        on the test class is set to `False`.

        This test is optional and should be skipped if the model does not support
        tool calling (see configuration below).

        ??? note "Configuration"

            To disable tool calling tests, set `has_tool_calling` to `False` in your
            test class:

            ```python
            class TestMyChatModelIntegration(ChatModelIntegrationTests):
                @property
                def has_tool_calling(self) -> bool:
                    return False
            ```

        ??? question "Troubleshooting"

            If this test fails, check that `bind_tools` is implemented to correctly
            translate LangChain tool objects into the appropriate schema for your
            chat model.

            This test may fail if the chat model does not support a `tool_choice`
            parameter. This parameter can be used to force a tool call. If
            `tool_choice` is not supported, set `has_tool_choice` to `False` in
            your test class:

            ```python
            @property
            def has_tool_choice(self) -> bool:
                return False
            ```

        """
        if not self.has_tool_calling:
            pytest.skip("Test requires tool calling.")

        prompt = ChatPromptTemplate.from_messages(
            [("human", "Hello. Please respond in the style of {answer_style}.")]
        )
        llm = GenericFakeChatModel(messages=iter(["hello matey"]))
        chain = prompt | llm | StrOutputParser()
        tool_ = chain.as_tool(
            name="greeting_generator",
            description="Generate a greeting in a particular style of speaking.",
        )
        if self.has_tool_choice:
            tool_choice: str | None = "any"
        else:
            tool_choice = None
        model_with_tools = model.bind_tools([tool_], tool_choice=tool_choice)
        query = "Using the tool, generate a Pirate greeting."
        result = model_with_tools.invoke(query)
        assert isinstance(result, AIMessage)
        assert result.tool_calls
        tool_call = result.tool_calls[0]
        assert tool_call["args"].get("answer_style")
        assert tool_call.get("type") == "tool_call"

Domain

Subdomains

Frequently Asked Questions

What does test_bind_runnables_as_tools() do?
test_bind_runnables_as_tools() is a function in the langchain codebase, defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py.
Where is test_bind_runnables_as_tools() defined?
test_bind_runnables_as_tools() is defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py at line 1677.

Analyze Your Own Codebase

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

Try Supermodel Free