vercel/ai · error
Unsupported role: ${_exhaustiveCheck}
Error message
Unsupported role: ${_exhaustiveCheck} What it means
convertToMistralChatMessages exhaustively switches over prompt message roles (system/user/assistant/tool). The default branch uses TypeScript's never-exhaustiveness check and throws for any role outside those, so this only fires with a role the converter doesn't know.
Source
Thrown at packages/mistral/src/convert-to-mistral-chat-messages.ts:193
case 'content':
case 'json':
case 'error-json':
contentValue = JSON.stringify(output.value);
break;
}
messages.push({
role: 'tool',
name: toolResponse.toolName,
tool_call_id: toolResponse.toolCallId,
content: contentValue,
});
}
break;
}
default: {
const _exhaustiveCheck: never = role;
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
}
}
}
return messages;
}
View on GitHub (pinned to 69428b1f8b)
Solutions
- Only use system/user/assistant/tool roles in prompts sent to Mistral.
- Update both `ai` and `@ai-sdk/mistral` to matching latest versions.
- Fix type-unsafe prompt construction (remove `as any` casts).
- Sanitize roles before converting legacy chat histories.
Example fix
// before
const prompt = [{ role: 'developer', content: 'hi' }] as any;
// after
const prompt = [{ role: 'system', content: 'hi' }]; Defensive patterns
Strategy: type-guard
Validate before calling
const roles = ['system', 'user', 'assistant', 'tool'] as const;
function hasValidRoles(prompt: { role: string }[]): boolean {
return prompt.every(m => (roles as readonly string[]).includes(m.role));
} Type guard
type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
function isSupportedRole(role: string): role is MessageRole {
return ['system', 'user', 'assistant', 'tool'].includes(role);
} Try / catch
try {
await generateText({ model: mistral(modelId), prompt });
} catch (error) {
if (error instanceof Error && error.message.startsWith('Unsupported role:')) {
console.error('Invalid role:', error.message);
// fix prompt construction / versions, then retry
} else {
throw error;
}
} Prevention
- Type prompts as LanguageModelV4Prompt instead of casting with `as any`.
- Keep `ai` and `@ai-sdk/mistral` on compatible versions.
- Validate legacy chat-history roles when importing into prompts.
When it happens
Trigger: A LanguageModelV4Prompt containing a message role other than 'system', 'user', 'assistant', or 'tool' — typically only possible with type-unsafe prompt construction or a version mismatch between prompt types and the Mistral package.
Common situations: Casting untyped/legacy message arrays into prompts; building prompts programmatically with wrong role strings; running mismatched versions of `ai` core and `@ai-sdk/mistral`.
Related errors
- file part data type ${part.data.type}
- file parts with provider references
- text file parts
- Unsupported content type in assistant message: ${part.type}
- Only images and PDF file parts are supported
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/66e777badd4daf38.
Report an issue: GitHub.