[PR #2440] fix: resolve Mistral API compatibility issues #10309

Closed
opened 2026-02-16 18:14:56 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/2440

State: closed
Merged: Yes


Summary

Fixes multiple Mistral API compatibility issues that occur when using Mistral models with tool calling:

  • Message sequence error: Resolves AI_APICallError: Unexpected role 'user' after role 'tool' by inserting assistant messages between tool and user messages to maintain proper conversation flow
  • Assistant message format: Fixes Error: Unsupported content type in assistant message: undefined by ensuring inserted messages have proper AI SDK structure
  • Tool call ID format: Resolves tool call ID validation errors by normalizing IDs to meet Mistral's alphanumeric 9-character requirement

Root Cause

Mistral API requirements:

  1. Conversation roles must strictly alternate user/assistant/user/assistant after tool usage
  2. Tool call IDs must be exactly 9 alphanumeric characters
  3. Assistant messages require proper structure with toolInvocations field

Solution

Added Mistral-specific transforms in packages/opencode/src/provider/transform.ts:

  • normalizeMistralToolCallIds() - converts tool call IDs to 9-character alphanumeric format
  • fixMistralMessageSequence() - inserts assistant messages to maintain proper sequence
  • Applied transforms for all Mistral models by checking provider ID

Test Plan

  • Verify tool call ID normalization to 9-character alphanumeric format
  • Verify message sequence correction inserts assistant messages between tool and user
  • Verify existing valid sequences remain unchanged
  • Test conversations with tool usage using Mistral models
  • Covers all Mistral model variants via provider ID detection

Files Changed

  • packages/opencode/src/provider/transform.ts - Added Mistral-specific message transforms

Resolves API errors that occur when using Mistral models with tool calling functionality.

closes #526
closes #1680
closes #2865

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/2440 **State:** closed **Merged:** Yes --- ## Summary Fixes multiple Mistral API compatibility issues that occur when using Mistral models with tool calling: - **Message sequence error**: Resolves `AI_APICallError: Unexpected role 'user' after role 'tool'` by inserting assistant messages between tool and user messages to maintain proper conversation flow - **Assistant message format**: Fixes `Error: Unsupported content type in assistant message: undefined` by ensuring inserted messages have proper AI SDK structure - **Tool call ID format**: Resolves tool call ID validation errors by normalizing IDs to meet Mistral's alphanumeric 9-character requirement ## Root Cause Mistral API requirements: 1. Conversation roles must strictly alternate user/assistant/user/assistant after tool usage 2. Tool call IDs must be exactly 9 alphanumeric characters 3. Assistant messages require proper structure with `toolInvocations` field ## Solution Added Mistral-specific transforms in `packages/opencode/src/provider/transform.ts`: - `normalizeMistralToolCallIds()` - converts tool call IDs to 9-character alphanumeric format - `fixMistralMessageSequence()` - inserts assistant messages to maintain proper sequence - Applied transforms for all Mistral models by checking provider ID ## Test Plan - [x] Verify tool call ID normalization to 9-character alphanumeric format - [x] Verify message sequence correction inserts assistant messages between tool and user - [x] Verify existing valid sequences remain unchanged - [x] Test conversations with tool usage using Mistral models - [x] Covers all Mistral model variants via provider ID detection ## Files Changed - `packages/opencode/src/provider/transform.ts` - Added Mistral-specific message transforms Resolves API errors that occur when using Mistral models with tool calling functionality. closes #526 closes #1680 closes #2865
yindo added the pull-request label 2026-02-16 18:14:56 -05:00
yindo closed this issue 2026-02-16 18:14:56 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10309