Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 15, 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 exists for other providers (DeepSeek, Z.ai, MiniMax) and 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 (all 27 tests pass)
  5. The OpenRouter handler tests pass (all 24 tests pass)

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 is already well-tested and used by other providers for similar message ordering requirements.

Note: PR #10630 was previously opened for this issue but this is a fresh implementation with the latest codebase.


Important

Fixes message ordering error for Mistral/Devstral models in OpenRouter 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 into the last tool message.
    • Extends model detection to include "devstral" in addition to "mistral".
  • Implementation:
    • Updates createMessage() in openrouter.ts to use mergeToolResultText for Mistral family models.
    • Uses existing mergeToolResultText mechanism for message conversion.
  • Testing:
    • Existing mergeToolResultText tests in openai-format.spec.ts cover functionality.
    • OpenRouter handler tests pass.

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

Fixes #10618

Extended model detection to include "devstral" models (mistralai/devstral-2512
contains "devstral" not "mistral") and added mergeToolResultText: true to
prevent "Unexpected role user after role tool" errors.

Root cause: Mistral/Devstral models enforce strict message ordering and reject
a "user" role message immediately after a "tool" role message.
@roomote
Copy link
Contributor Author

roomote bot commented Jan 15, 2026

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The fix correctly addresses the "Unexpected role 'user' after role 'tool'" error for Mistral/Devstral models by:

  • Extending model detection to include "devstral" models
  • Adding mergeToolResultText: true to merge text content into tool messages instead of creating separate user messages

This approach is consistent with how other providers (DeepSeek, Z.ai) handle similar message ordering requirements.

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

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

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

2 participants