Skip to content

Commit ce38fd4

Browse files
committed
fix: add mergeToolResultText for Mistral/Devstral models in OpenRouter
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
1 parent 632b86c commit ce38fd4

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/api/providers/openrouter.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,17 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
229229
}
230230

231231
// Convert Anthropic messages to OpenAI format.
232-
// Pass normalization function for Mistral compatibility (requires 9-char alphanumeric IDs)
233-
const isMistral = modelId.toLowerCase().includes("mistral")
232+
// Pass normalization function for Mistral/Devstral compatibility (requires 9-char alphanumeric IDs)
233+
// Also merge tool result text to avoid "Unexpected role 'user' after role 'tool'" errors
234+
const modelIdLower = modelId.toLowerCase()
235+
const isMistralFamily = modelIdLower.includes("mistral") || modelIdLower.includes("devstral")
234236
let openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
235237
{ role: "system", content: systemPrompt },
236238
...convertToOpenAiMessages(
237239
messages,
238-
isMistral ? { normalizeToolCallId: normalizeMistralToolCallId } : undefined,
240+
isMistralFamily
241+
? { normalizeToolCallId: normalizeMistralToolCallId, mergeToolResultText: true }
242+
: undefined,
239243
),
240244
]
241245

0 commit comments

Comments
 (0)