Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

fix: add mergeToolResultText for Mistral/Devstral models in OpenRouter - #10630

Open
ghost wants to merge 2 commits into
mainfrom
feature/fix-mistral-devstral-tool-message-ordering
Open

fix: add mergeToolResultText for Mistral/Devstral models in OpenRouter#10630
ghost wants to merge 2 commits into
mainfrom
feature/fix-mistral-devstral-tool-message-ordering

Conversation

@ghost

@ghost ghost commented Jan 12, 2026

Copy link
Copy Markdown

Related GitHub Issue

Closes: #10618

Related: #10540 (same root cause, different provider)

Description

This PR attempts to address Issue #10618. Feedback and guidance are welcome.

Root Cause:
The error "Unexpected role 'user' after role 'tool'" occurs because Mistral/Devstral models have strict message ordering requirements. After a tool role message, the next message cannot be a user role message.

Solution:

  1. Extended model detection to include "devstral" (the model mistralai/devstral-2512 contains "devstral" not "mistral")
  2. Added mergeToolResultText: true to the conversion options, which merges text content (like environment_details) into the last tool message instead of creating a separate user message

This uses the same mergeToolResultText mechanism that PR #10598 exposes as a setting for OpenAI Compatible providers, but applies it automatically for Mistral family models in OpenRouter since we can detect them by model ID.

Test Procedure

  1. Configure OpenRouter with a Mistral/Devstral model (e.g., mistralai/devstral-2512)
  2. Start a task that uses tools
  3. Verify that tool calls complete without the "Unexpected role user after role tool" error
  4. The existing mergeToolResultText tests in openai-format.spec.ts cover the underlying functionality

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: The underlying mergeToolResultText functionality is already tested.
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

This is a minimal fix that leverages existing infrastructure. The mergeToolResultText option was added in a previous PR and is already used by DeepSeek, Z.ai, and MiniMax providers for similar message ordering issues.


Important

Fixes message ordering error for Mistral/Devstral models in OpenRouterHandler by merging tool result text and extending model detection.

  • Behavior:
    • Fixes message ordering error for Mistral/Devstral models in OpenRouterHandler by merging tool result text.
    • Extends model detection to include "devstral" in addition to "mistral".
  • Code Changes:
    • Updates createMessage() in openrouter.ts to use mergeToolResultText for Mistral/Devstral models.
    • Checks modelId for "mistral" or "devstral" to apply the fix.
  • Testing:
    • Relies on existing mergeToolResultText tests in openai-format.spec.ts.

This description was created by Ellipsis for ce38fd4. You can customize this summary. It will automatically update as commits are pushed.

Interactively review PR in Roo Code Cloud

@ghost

ghost commented Jan 12, 2026

Copy link
Copy Markdown
Author

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The change correctly extends Mistral model detection to include Devstral models and adds mergeToolResultText: true to prevent the "Unexpected role 'user' after role 'tool'" error. The fix leverages existing tested infrastructure.

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@CullenShane

Copy link
Copy Markdown

@roomote What are the next steps to merge this?

@ghost

ghost commented Jan 22, 2026

Copy link
Copy Markdown
Author

Fixaroo Clock   See task on Roo Cloud

The PR was in draft status. I've marked it as ready for review.

The code changes look good - the fix extends Mistral model detection to include Devstral models and adds mergeToolResultText: true to prevent the "Unexpected role 'user' after role 'tool'" error. The PR reviewer already approved with no issues found.

Next steps: A maintainer can now review and merge this PR.

@ghost
ghost marked this pull request as ready for review January 22, 2026 06:19
@ghost
ghost requested review from cte, jr and mrubens as code owners January 22, 2026 06:19
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Jan 22, 2026
@ghost

ghost commented Jan 22, 2026

Copy link
Copy Markdown
Author

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The change correctly extends Mistral model detection to include Devstral models and adds mergeToolResultText: true to prevent the "Unexpected role 'user' after role 'tool'" error. The fix leverages existing, well-tested infrastructure in convertToOpenAiMessages().

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@jkrauss82

Copy link
Copy Markdown

Please fix and merge this, Devstral 2512 is still not working in recent Roo code

@PeterSmarthome

Copy link
Copy Markdown

@roomote

I do still get:

`Date/time: 2026-02-20T18:55:54.621Z
Extension version: 3.50.2
Provider: openrouter
Model: mistralai/devstral-2512

Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output.`

What are next steps to fix it?

@ghost

ghost commented Feb 20, 2026

Copy link
Copy Markdown
Author

Fixaroo Clock   See task

Rebased the PR branch on main (was 370 commits behind) and added tests for Mistral/Devstral model detection with mergeToolResultText. All lint, type-check, and tests pass.

Note to @PeterSmarthome: The error you're seeing on v3.50.2 is expected because this PR hasn't been merged yet. Once merged, mistralai/devstral-2512 should work correctly on OpenRouter.

View commit | Revert commit

@ghost
ghost force-pushed the feature/fix-mistral-devstral-tool-message-ordering branch from ce38fd4 to cb3f1e0 Compare February 20, 2026 19:11
@ghost
ghost requested review from daniel-lxs and hannesrudolph as code owners February 20, 2026 19:11
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Feb 20, 2026
@PeterSmarthome

Copy link
Copy Markdown

@roomote ist the timeline about when the fix can be expected? I'd be happy to see it deployed soon. Thank you very much.

@ghost

ghost commented Feb 25, 2026

Copy link
Copy Markdown
Author

Proofessor Clock   See task

Rebased the PR onto the latest main branch. All 27 tests pass and type checks are clean. The PR is ready for maintainer review and merge.

View changes | Revert commit

@ghost
ghost force-pushed the feature/fix-mistral-devstral-tool-message-ordering branch from cb3f1e0 to 98f3ccb Compare February 25, 2026 09:31
@PeterSmarthome

Copy link
Copy Markdown

@roomote is there a timeline about when the fix can be expected? I'd be happy to see it deployed soon. Thank you very much.

@ghost

ghost commented Mar 6, 2026

Copy link
Copy Markdown
Author

Fixaroo Clock   See task

Rebased this PR on the latest main branch. All 27 tests pass and type checks succeed across all 14 packages. The PR is ready for review and merge.

View commit | Revert commit

@ghost
ghost force-pushed the feature/fix-mistral-devstral-tool-message-ordering branch from 98f3ccb to 1ce2309 Compare March 6, 2026 18:34
@PeterSmarthome

Copy link
Copy Markdown

@roomote is there a timeline about when the fix can be expected? I'd be happy to see it deployed soon. Thank you very much.

@ghost

ghost commented Mar 10, 2026

Copy link
Copy Markdown
Author

Proofessor Clock   See task

Rebased this PR onto the latest main. All tests (27/27) and type checks pass.

View commit | Revert commit

roomote added 2 commits March 10, 2026 14:13
Fixes the "Unexpected role user after role tool" error for Mistral and
Devstral models when using OpenRouter.

Changes:
- Extended model detection to include "devstral" (e.g. mistralai/devstral-2512)
- Added mergeToolResultText: true to merge text content into tool messages
  instead of creating a separate user message that violates Mistral ordering

Closes #10618
@ghost
ghost force-pushed the feature/fix-mistral-devstral-tool-message-ordering branch from 1ce2309 to 56ddbe0 Compare March 10, 2026 14:15
@PeterSmarthome

Copy link
Copy Markdown

@roomote has this been fixed with #10684 ?

@ghost

ghost commented Mar 10, 2026

Copy link
Copy Markdown
Author

Proofessor Clock   See task

No, #10684 did not fix this. That issue was about OpenAI provider with local servers (vLLM/Ollama) -- a different provider and root cause.

This PR's fix is still needed. The current main branch still only checks for "mistral" in the model ID (missing "devstral" detection) and does not pass mergeToolResultText: true for Mistral-family models on OpenRouter. Both changes in this PR are required to resolve #10618.

@PeterSmarthome

Copy link
Copy Markdown

@roomote is there a timeline about when the fix can be expected? mistral.ai just startet rolling out mistral small 4 and I'd be happy to use it on open router as soon as it's available there...

@ghost

ghost commented Mar 17, 2026

Copy link
Copy Markdown
Author

Proofessor Clock   See task

This PR is already up to date with main and all 27 tests pass. The existing model detection (modelIdLower.includes("mistral")) already covers newer Mistral models like Mistral Small 4, since their OpenRouter IDs (e.g. mistralai/mistral-small-2503) contain "mistral". This PR is ready for maintainer review and merge.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

No open projects
Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] openrouter:mistralai/devstral-2512 -> "completion error"

5 participants