def test_prepare_chat_with_tools_tool_required_single_tool(
    mock_resolve_credentials, MockModelInference
):
    """Test that tool_required selects the first tool when there's only one tool."""
    mock_resolve_credentials.return_value = {}
    mock_instance = MockModelInference.return_value

    llm = WatsonxLLM(
        model_id="test_model",
        project_id="test_project",
        apikey="test_apikey",
        url="https://test-url.com",
        api_client=MagicMock(),  # Use mock client to bypass credential checks
    )

    # Test with tool_required=True and a single tool
    result = llm._prepare_chat_with_tools(tools=[search_tool], tool_required=True)

    assert "tool_choice" in result
    assert result["tool_choice"]["type"] == "function"
    assert result["tool_choice"]["function"]["name"] == "search_tool"
    assert len(result["tools"]) == 1
    assert result["tools"][0]["function"]["name"] == "search_tool"
Test that tool_required selects the first tool when there's only one tool.
llama-index-integrations/llms/llama-index-llms-ibm/tests/test_tool_required.py