Skip to content

fix: resolve Mistral API compatibility issues#2440

Merged
rekram1-node merged 10 commits intoanomalyco:devfrom
NicolaivdSmagt:fix-mistral-message-sequence
Nov 6, 2025
Merged

fix: resolve Mistral API compatibility issues#2440
rekram1-node merged 10 commits intoanomalyco:devfrom
NicolaivdSmagt:fix-mistral-message-sequence

Conversation

@NicolaivdSmagt
Copy link
Copy Markdown
Contributor

@NicolaivdSmagt NicolaivdSmagt commented Sep 5, 2025

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

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.
@uloco
Copy link
Copy Markdown

uloco commented Sep 23, 2025

I don't want to be that guy but can we please get this in? I really want to use mistral instead of copilot with gtp4.1 but I am facing too many issues with it. What is the blocker here?

@ErwanLegrand
Copy link
Copy Markdown

Fixes #526 , #1680 and #2865 .

@rekram1-node
Copy link
Copy Markdown
Collaborator

ill check this out tmr, sorry for delays

@rekram1-node rekram1-node merged commit a2ab019 into anomalyco:dev Nov 6, 2025
3 checks passed
@rekram1-node
Copy link
Copy Markdown
Collaborator

thank u!

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

Labels

None yet

Projects

None yet

4 participants