Home / Function/ test_tool_calling() — langchain Function Reference

test_tool_calling() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  d8558ca0_4956_161f_b284_039f6f9cdfd3["test_tool_calling()"]
  971e928f_9c9b_ce7a_b93d_e762f2f5aa54["ChatModelIntegrationTests"]
  d8558ca0_4956_161f_b284_039f6f9cdfd3 -->|defined in| 971e928f_9c9b_ce7a_b93d_e762f2f5aa54
  93a476c6_2ee3_787e_23b4_3d1e8b2d3794["magic_function()"]
  d8558ca0_4956_161f_b284_039f6f9cdfd3 -->|calls| 93a476c6_2ee3_787e_23b4_3d1e8b2d3794
  873906e7_1aac_f857_fbc7_973f8d33d1de["_validate_tool_call_message()"]
  d8558ca0_4956_161f_b284_039f6f9cdfd3 -->|calls| 873906e7_1aac_f857_fbc7_973f8d33d1de
  style d8558ca0_4956_161f_b284_039f6f9cdfd3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/standard-tests/langchain_tests/integration_tests/chat_models.py lines 1553–1613

    def test_tool_calling(self, model: BaseChatModel) -> None:
        """Test that the model generates tool calls.

        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 and the model consistently fails this
            test, you can `xfail` the test:

            ```python
            @pytest.mark.xfail(reason=("Does not support tool_choice."))
            def test_tool_calling(self, model: BaseChatModel) -> None:
                super().test_tool_calling(model)
            ```

            Otherwise, in the case that only one tool is bound, ensure that
            `tool_choice` supports the string `'any'` to force calling that tool.

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

        tool_choice_value = None if not self.has_tool_choice else "any"
        model_with_tools = model.bind_tools(
            [magic_function], tool_choice=tool_choice_value
        )

        # Test invoke
        query = "What is the value of magic_function(3)? Use the tool."
        result = model_with_tools.invoke(query)
        _validate_tool_call_message(result)

        # Test stream
        full: BaseMessage | None = None
        for chunk in model_with_tools.stream(query):
            full = chunk if full is None else full + chunk  # type: ignore[assignment]
        assert isinstance(full, AIMessage)
        _validate_tool_call_message(full)

Domain

Subdomains

Frequently Asked Questions

What does test_tool_calling() do?
test_tool_calling() is a function in the langchain codebase, defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py.
Where is test_tool_calling() defined?
test_tool_calling() is defined in libs/standard-tests/langchain_tests/integration_tests/chat_models.py at line 1553.
What does test_tool_calling() call?
test_tool_calling() calls 2 function(s): _validate_tool_call_message, magic_function.

Analyze Your Own Codebase

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

Try Supermodel Free