Error in Sqlite : sqlite3_prepare_v2 failure: near \\\"UNION\\\" : syntax error\},\"code : 5}

I am using sqlite to fetch data from chat table but it keeps giving me error at UNION.
Here is my query:

SELECT a.id, a.timeDate, a.message, fromUser, toUser FROM ((SELECT id, message, fromUser, toUser, toUser AS uid, timeDate FROM chat WHERE fromUser = ?) UNION (SELECT id, message, fromUser, toUser, fromUser AS uid, timeDate FROM chat WHERE toUser = ? )) AS a GROUP BY a.uid ORDER BY a.id DESC , a.uid DESC

I tried with brackets before UNION and without bracket like
(query1) UNION (query2) and (query1 UNION query2) but still that’ not working.

This query is working fine in mysql with same structured table.

How to solve this problem?

Does the query work if you just execute the stuff inside the bracket? (without GROUP BY etc)