Skip to content

Comments

fix: add mergeToolResultText for Mistral/Devstral models on OpenRouter#11643

Draft
roomote[bot] wants to merge 1 commit intomainfrom
fix/openrouter-devstral-merge-tool-result-text
Draft

fix: add mergeToolResultText for Mistral/Devstral models on OpenRouter#11643
roomote[bot] wants to merge 1 commit intomainfrom
fix/openrouter-devstral-merge-tool-result-text

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Feb 20, 2026

Related GitHub Issue

Closes: #10618

Description

This PR attempts to address Issue #10618 where mistralai/devstral-2512 on OpenRouter produces errors like "Unexpected role user after role tool" or "The language model did not provide any assistant messages."

Root cause: Mistral/Devstral models enforce strict message ordering and reject a user role message directly after a tool role message. The current code normalizes tool call IDs for Mistral models but does not merge text content that follows tool results into the tool message, causing an invalid tool -> user message sequence.

Changes:

  1. Extended model detection to also match devstral in the model ID (for robustness, even though mistralai/devstral-2512 already matches via mistralai)
  2. Added mergeToolResultText: true to the Mistral options passed to convertToOpenAiMessages(), which merges text content after tool results into the last tool message instead of creating a separate user message

This uses the same mergeToolResultText mechanism already available in the codebase and tested in openai-format.spec.ts.

Feedback and guidance are welcome.

Test Procedure

  • Added 2 new tests in openrouter.spec.ts under "Mistral/Devstral message formatting":
    1. Verifies tool result text is merged into tool messages for devstral models (no user message after tool message)
    2. Verifies devstral model ID detection and basic message creation
  • All 26 OpenRouter tests pass
  • All 44 openai-format tests pass
  • All lint and type checks pass

Run tests: cd src && npx vitest run api/providers/__tests__/openrouter.spec.ts

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: New and/or updated tests have been added to cover my changes.
  • Documentation Impact: No documentation updates are required.
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

Previous PRs #10630 and #10751 addressed the same issue but were never merged. This PR incorporates the same fix on the current main branch.

Start a new Roo Code Cloud session on this branch

Fixes the "Unexpected role user after role tool" and empty response errors
for mistralai/devstral-2512 on OpenRouter by:

1. Extending model detection to also match "devstral" in the model ID
2. Adding mergeToolResultText: true so text content after tool results
   is merged into the last tool message instead of creating a separate
   user message

Closes #10618
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant