Skip to content

Comments

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

Open
roomote[bot] wants to merge 2 commits intomainfrom
feature/fix-mistral-devstral-tool-message-ordering
Open

fix: add mergeToolResultText for Mistral/Devstral models in OpenRouter#10630
roomote[bot] wants to merge 2 commits intomainfrom
feature/fix-mistral-devstral-tool-message-ordering

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 12, 2026

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.

Start a new Roo Code Cloud session on this branch

@roomote
Copy link
Contributor Author

roomote bot commented Jan 12, 2026

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

@roomote What are the next steps to merge this?

@roomote
Copy link
Contributor Author

roomote bot commented Jan 22, 2026

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.

@roomote roomote bot marked this pull request as ready for review January 22, 2026 06:19
@roomote roomote bot 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
@roomote
Copy link
Contributor Author

roomote bot commented Jan 22, 2026

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

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

@PeterSmarthome
Copy link

@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?

@roomote
Copy link
Contributor Author

roomote bot commented Feb 20, 2026

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

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
@roomote roomote bot force-pushed the feature/fix-mistral-devstral-tool-message-ordering branch from ce38fd4 to cb3f1e0 Compare 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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