Skip to content

Conversation

NicolaivdSmagt
Copy link

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.

Fixes "Unexpected role 'user' after role 'tool'" error that occurs when
switching from Claude to Mistral models mid-conversation. Mistral has
stricter message sequence requirements than other providers.

- Add fixMistralMessageSequence() function to detect tool→user sequences
- Insert intermediate assistant messages to maintain proper conversation flow
- Apply fix for models containing "mistral" or "devstral" in their name
- Preserve valid sequences unchanged

Resolves issue where users get API errors when switching between providers
during conversations with tool usage.
Resolves 'Unsupported content type in assistant message: undefined' error
by ensuring the inserted assistant message has the proper structure
expected by the AI SDK.
Mistral API requires tool call IDs to be alphanumeric with exactly 9 characters.
Convert IDs like 'toolu_01CBhTTz95qkd9LJMdC9sf8t' to 'toolu01CB'.

Fixes 'Tool call id was toolu_01CBhTTz95qkd9LJMdC9sf8t but must be a-z, A-Z,
0-9, with a length of 9' error.
Filter on providerID rather than individual model names to ensure all
Mistral models receive the necessary API compatibility transforms,
including future model variants.
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.

1 participant