Skip to content

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.

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
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.

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:S This PR changes 10-29 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

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

4 participants