Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jan 13, 2026

Related GitHub Issue

Closes: #10684

Related: #10618, #10540 (same root cause, different providers)

Description

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

Root Cause:
The error "Unexpected role 'user' after role 'tool'" occurs because Mistral/Devstral models (and other strict OpenAI-compatible servers like vLLM, llama.cpp, Ollama) enforce strict message ordering. After a tool role message, the next message cannot be a user role message.

In the current code, when a user message contains both tool results and text content (like environment_details), the conversion logic:

  1. First creates tool role messages for each tool result
  2. Then creates a separate user role message for any text content

This creates the invalid sequence: tool -> user, which strict servers reject.

Solution:
Added { mergeToolResultText: true } to all convertToOpenAiMessages() calls in the OpenAI provider. This option merges text content into the last tool message instead of creating a separate user message, avoiding the invalid message sequence.

This uses the same mergeToolResultText mechanism that:

Test Procedure

  1. Configure OpenAI provider with a local vLLM/llama.cpp/Ollama server serving a Mistral/Devstral model
  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 in openai-format.spec.ts.
  • 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 (DeepSeek, Z.ai, MiniMax) for similar message ordering requirements.


Important

Fixes message ordering in OpenAiHandler by merging tool result text to prevent invalid sequences in strict servers.

  • Behavior:
    • Adds { mergeToolResultText: true } to convertToOpenAiMessages() calls in OpenAiHandler to fix message ordering issues.
    • Prevents invalid tool -> user message sequences in strict OpenAI-compatible servers (e.g., vLLM, llama.cpp, Ollama).
  • Files:
    • Changes in openai.ts to apply mergeToolResultText in both streaming and non-streaming modes.
    • Affects createMessage() and handleO3FamilyMessage() functions.
  • Testing:
    • Relies on existing tests in openai-format.spec.ts for mergeToolResultText functionality.

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

@roomote
Copy link
Contributor Author

roomote bot commented Jan 13, 2026

Rooviewer Clock   See task on Roo Cloud

Code review complete. No issues found.

This PR correctly applies the existing mergeToolResultText option to all convertToOpenAiMessages() calls in the OpenAI provider. The fix prevents the "Unexpected role 'user' after role 'tool'" error with strict OpenAI-compatible servers (vLLM, llama.cpp, Ollama with Mistral). The approach is consistent with patterns in DeepSeek, Z.ai, and MiniMax providers.

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

@daniel-lxs daniel-lxs closed this Jan 14, 2026
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Jan 14, 2026
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jan 14, 2026
@daniel-lxs daniel-lxs deleted the feature/fix-openai-tool-message-ordering branch January 14, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] Local vllm devstral2 -> OpenAI completion error

3 participants