Umesh Yadav
b8c1b54f9e
language_models: Fix Mistral tool->user message sequence handling ( #31736 )
...
Closes #31491
### Problem
Mistral API enforces strict conversation flow requirements that other
providers don't. Specifically, after a `tool` message, the next message
**must** be from the `assistant` role, not `user`. This causes the
error:
```
"Unexpected role 'user' after role 'tool'"
```
This can also occur in normal conversation flow where mistral doesn't
return the assistant message but that is something which can't be
reproduce reliably.
### Root Cause
When users interrupt an ongoing tool call sequence by sending a new
message, we insert a `user` message directly after a `tool` message,
violating Mistral's protocol.
**Expected Mistral flow:**
```
user → assistant (with tool_calls) → tool (results) → assistant (processes results) → user (next input)
```
**What we were doing:**
```
user → assistant (with tool_calls) → tool (results) → user (interruption) ❌
```
### Solution
Insert an empty `assistant` message between any `tool` → `user` sequence
in the Mistral provider's request construction. This satisfies Mistral's
API requirements without affecting other providers or requiring UX
changes.
### Testing
To reproduce the original error:
1. Start agent chat with `codestral-latest`
2. Send: "Describe this project using tool call only"
3. Once tool calls begin, send: "stop this"
4. Main branch: API error
5. This fix: Works correctly
Release Notes:
- Fixed Mistral tool calling in some cases
2025-06-06 12:35:22 +03:00
..
2025-05-29 01:58:40 +00:00
2025-06-05 19:15:06 +00:00
2025-06-03 15:59:46 +00:00
2025-05-22 21:00:54 +00:00
2025-05-16 05:04:36 -04:00
2025-05-20 23:06:07 +00:00
2025-06-05 19:15:06 +00:00
2025-05-21 09:27:54 +00:00
2025-05-26 11:48:50 -04:00
2025-06-06 05:23:25 +00:00
2025-06-05 21:30:05 +00:00
2025-05-26 21:20:41 -03:00
2025-05-28 11:51:21 -04:00
2025-05-20 23:06:07 +00:00
2025-05-30 08:29:52 +00:00
2025-04-02 13:26:34 -07:00
2025-06-04 10:41:12 +03:00
2025-05-30 08:32:54 -07:00
2025-05-26 11:48:50 -04:00
2025-05-26 23:04:31 -04:00
2025-06-04 16:56:33 +00:00
2025-05-20 23:06:07 +00:00
2025-05-26 23:04:31 -04:00
2025-05-04 08:28:39 +00:00
2025-06-05 19:42:52 +00:00
2025-06-05 19:15:06 +00:00
2025-04-02 13:26:34 -07:00
2025-06-04 19:18:12 +00:00
2025-04-02 13:26:34 -07:00
2025-06-05 12:59:22 +00:00
2025-05-26 10:29:45 +02:00
2025-06-05 19:42:52 +00:00
2025-04-02 13:26:34 -07:00
2025-06-04 01:37:25 +02:00
2025-06-04 01:37:25 +02:00
2025-05-20 00:20:00 +00:00
2025-05-22 07:24:46 -04:00
2025-05-14 11:15:27 +02:00
2025-06-05 19:15:06 +00:00
2025-05-20 23:06:07 +00:00
2025-06-05 19:42:52 +00:00
2025-06-04 19:18:12 +00:00
2025-06-06 09:30:57 +00:00
2025-06-05 10:36:55 +00:00
2025-05-26 16:06:38 +02:00
2025-05-26 16:06:38 +02:00
2025-05-20 23:06:07 +00:00
2025-06-02 22:19:09 +00:00
2025-06-03 20:35:25 +00:00
2025-05-17 16:42:45 +00:00
2025-05-23 14:53:53 +00:00
2025-06-03 20:35:25 +00:00
2025-04-02 13:26:34 -07:00
2025-06-04 19:54:24 +02:00
2025-04-02 13:26:34 -07:00
2025-05-12 14:43:14 +00:00
2025-06-03 12:23:01 -04:00
2025-06-03 12:23:01 -04:00
2025-06-05 17:12:17 +00:00
2025-06-03 09:41:45 +02:00
2025-06-05 19:30:34 +00:00
2025-06-06 07:31:45 +00:00
2025-05-26 17:43:57 +00:00
2025-04-02 13:26:34 -07:00
2025-04-02 13:26:34 -07:00
2025-05-20 23:06:07 +00:00
2025-04-02 13:26:34 -07:00
2025-06-03 15:20:25 -03:00
2025-06-03 13:18:29 +02:00
2025-05-20 23:06:07 +00:00
2025-05-20 23:06:07 +00:00
2025-05-27 21:52:42 +00:00
2025-06-05 19:15:06 +00:00
2025-05-20 23:06:07 +00:00
2025-05-17 16:42:45 +00:00
2025-05-17 16:42:45 +00:00
2025-04-23 20:54:09 +00:00
2025-06-05 19:42:52 +00:00
2025-05-08 14:01:39 +02:00
2025-06-04 14:07:01 +00:00
2025-06-06 12:35:22 +03:00
2025-05-20 23:06:07 +00:00
2025-06-03 02:12:58 +00:00
2025-06-05 18:10:22 -07:00
2025-05-20 23:06:07 +00:00
2025-05-20 23:06:07 +00:00
2025-06-04 14:07:01 +00:00
2025-06-05 19:42:52 +00:00
2025-05-29 21:41:15 +00:00
2025-05-30 08:29:52 +00:00
2025-05-20 23:06:07 +00:00
2025-04-29 21:58:45 -03:00
2025-05-30 08:10:12 -03:00
2025-05-22 14:22:35 -04:00
2025-06-05 20:24:56 +00:00
2025-06-05 23:35:43 +00:00
2025-06-06 06:23:09 +00:00
2025-06-04 14:07:01 +00:00
2025-05-26 11:46:35 +00:00
2025-06-03 15:59:46 +00:00
2025-05-26 23:04:31 -04:00
2025-05-26 23:04:31 -04:00
2025-04-02 13:26:34 -07:00
2025-06-02 21:24:08 +00:00
2025-05-29 12:55:47 +00:00
2025-06-05 19:42:52 +00:00
2025-06-05 19:42:52 +00:00
2025-06-05 03:53:59 +05:30
2025-05-26 23:04:31 -04:00
2025-05-20 23:06:07 +00:00
2025-06-05 19:42:52 +00:00
2025-06-05 07:09:09 +00:00
2025-05-26 17:43:57 +00:00
2025-05-23 14:53:53 +00:00
2025-05-27 16:56:27 +00:00
2025-06-04 19:54:24 +02:00
2025-06-02 16:29:06 +00:00
2025-05-20 23:06:07 +00:00
2025-04-02 13:26:34 -07:00
2025-05-26 11:48:50 -04:00
2025-05-26 11:48:50 -04:00
2025-06-03 14:59:17 -03:00
2025-04-02 13:26:34 -07:00
2025-05-26 23:04:31 -04:00
2025-05-29 20:43:12 +00:00
2025-05-20 23:06:07 +00:00
2025-04-25 17:41:49 +03:00
2025-06-03 12:23:01 -04:00
2025-06-04 17:29:08 +00:00
2025-05-20 23:06:07 +00:00
2025-05-26 23:04:31 -04:00
2025-05-26 13:44:09 +00:00
2025-05-20 23:06:07 +00:00
2025-04-02 13:26:34 -07:00
2025-05-16 17:35:44 +02:00
2025-05-20 23:06:07 +00:00
2025-05-01 17:37:43 +02:00
2025-05-26 11:48:50 -04:00
2025-04-02 13:26:34 -07:00
2025-05-23 20:45:32 +00:00
2025-05-29 16:09:07 +00:00
2025-05-30 14:32:59 +02:00
2025-06-05 13:25:51 -04:00
2025-04-02 13:26:34 -07:00
2025-04-21 21:30:21 -07:00
2025-06-02 21:12:28 -06:00
2025-06-03 17:43:06 -04:00
2025-06-04 02:37:27 +00:00
2025-05-26 18:23:41 +00:00
2025-04-02 13:26:34 -07:00
2025-05-26 18:23:41 +00:00
2025-06-04 17:29:08 +00:00
2025-05-06 10:18:48 +00:00
2025-06-05 07:09:09 +00:00
2025-06-02 16:29:06 +00:00
2025-06-06 06:24:59 +00:00
2025-05-14 23:29:11 +02:00
2025-05-23 19:31:25 +00:00
2025-05-30 15:26:27 +00:00
2025-06-04 02:37:27 +00:00
2025-05-01 19:24:14 +00:00
2025-06-05 18:10:22 -07:00
2025-04-23 20:54:09 +00:00
2025-04-23 15:30:20 +00:00
2025-05-20 23:06:07 +00:00
2025-05-29 15:20:58 -04:00
2025-06-06 05:09:48 -04:00
2025-06-03 22:30:59 +00:00
2025-06-05 19:38:19 +08:00
2025-06-06 06:23:09 +00:00
2025-05-26 11:48:50 -04:00
2025-06-06 00:49:30 -06:00
2025-04-23 20:54:09 +00:00