Solution skill
Role 'tool' must be a response to a preceding message with 'tool_calls'
Complete solution
Llama Index: role 'tool' must be a response to a preceding message with 'tool_calls'. The verified fix is Fix: upgrade the chat-store package (pip install -U llama-index-chat-store-redis) and core (pip install -U llama-index-core); he noted 'I'm like 86% sure this is fixed'. Use this when you hit the symptoms above in llama_index; not for unrelated llama_index errors or general setup questions.
TL;DR
Fix: upgrade the chat-store package (pip install -U llama-index-chat-store-redis) and core (pip install -U llama-index-core); he noted 'I'm like 86% sure this is fixed'.
The error
Issue run-llama/llama_index#13715 (closed, 23 comments): ### Bug Description getting `openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'` error when using [json chat store](https://docs.llamaindex.ai/en/stable/module_guides/storing/chat_stores/#simplechatstore) with persistent paths, but when i checked the stored json tool_calls is saved before role tool and the chats are also saved by chat_store. I am receiving this error in long chats but only when i am loading the chat store again for specific key, when i tested separately in whFix
- Fix: upgrade the chat-store package (pip install -U llama-index-chat-store-redis) and core (pip install -U llama-index-core); he noted 'I'm like 86% sure this is fixed'.
Success check: the symptom above is gone and the original operation completes.
When to use
- You hit this exact symptom in llama_index.
- You want the verified fix without digging through the thread.
When not to use
- A different error in llama_index, even a similar-looking one.
- General llama_index setup or credential questions.
Compatibility
- Llama Index (run-llama/llama_index)
Variant phrasings
- How to fix "role 'tool' must be a response to a preceding message with 'toolcalls'" in llamaindex
- llamaindex: role 'tool' must be a response to a preceding message with 'toolcalls'
Root cause
Collaborator logan-markewich: this was a known bug in the chat-store memory handling - the memory buffer dropped the assistant/tool message pairs, so a 'tool' role message was sent to OpenAI without a preceding 'tool_calls' assistant message.
Source
- https://github.com/run-llama/llama_index/issues/13715
Evidence
In gh:run-llama/llamaindex#13715 (https://github.com/run-llama/llamaindex/issues/13715), someone reported: Issue run-llama/llamaindex#13715 (closed, 23 comments): ### Bug Description getting `openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'toolcalls'` error when using json chat store with persistent paths, but when i checked the stored json toolcalls is saved before role tool and the chats are also saved by chatstore. I am receiving this error in long chats but only when i am loading the chat store again for specific key, when i tested separately in wh The verified resolution from the thread: Collaborator logan-markewich: this was a known bug in the chat-store memory handling - the memory buffer dropped the assistant/tool message pairs, so a 'tool' role message was sent to OpenAI without a preceding 'toolcalls' assistant message. Fix: upgrade the chat-store package (pip install -U llama-index-chat-store-redis) and core (pip install -U llama-index-core); he noted 'I'm like 86% sure this is fixed'. Related reporter tip: removing/trimming chat history can also restore working pairs since truncation can split toolcall pairs.
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_VPr9FQikUDPuk3kId0QRFw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Role 'tool' must be a response to a preceding message with 'tool_calls' <span>— Vectle</span></a>