TLDR: The dates were included in the query as strings and not as Date objects.
I found the bug. I have a script to parse the JSON response from the LLM, which converts date strings back to Date objects. The regex pattern I used required the milliseconds to be included in the date string to be detected as a date. So whenever the LLM omitted the milliseconds the date would be included in the query as a string, thus causing the error.
The bug was difficult to diagnose. To log the full query you needed to stringify the object so the dates are displayed as strings. Also, when I ran the query in the shell and I did the date conversion manually and so they were converted without any errors.