fix: coalesce consecutive same-role messages for strict template models (#241)
Models served through Ollama/vLLM with strict Jinja templates (Devstral, Mistral, etc.) require strict user↔assistant role alternation and reject requests with consecutive messages of the same role. convertMessages() could produce consecutive user or assistant messages in three scenarios: batched user input, text-only + tool_use assistant turns, and tool result remainders followed by another user message. Added a coalescing pass at the end of convertMessages() that merges consecutive same-role messages (string concat or array concat), preserving tool_calls on assistant messages. Tool and system messages are excluded from coalescing as they have their own alternation rules. Includes regression tests for both user and assistant coalescing. Fixes #202
This commit is contained in:
committed by
GitHub
parent
c534aa5771
commit
d5852ca73d
@@ -295,7 +295,41 @@ function convertMessages(
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
// Coalescing pass: merge consecutive messages of the same role.
|
||||
// OpenAI/vLLM/Ollama require strict user↔assistant alternation.
|
||||
// Multiple consecutive tool messages are allowed (assistant → tool* → user).
|
||||
// Consecutive user or assistant messages must be merged to avoid Jinja
|
||||
// template errors like "roles must alternate" (Devstral, Mistral models).
|
||||
const coalesced: OpenAIMessage[] = []
|
||||
for (const msg of result) {
|
||||
const prev = coalesced[coalesced.length - 1]
|
||||
|
||||
if (prev && prev.role === msg.role && msg.role !== 'tool' && msg.role !== 'system') {
|
||||
const prevContent = prev.content
|
||||
const curContent = msg.content
|
||||
|
||||
if (typeof prevContent === 'string' && typeof curContent === 'string') {
|
||||
prev.content = prevContent + (prevContent && curContent ? '\n' : '') + curContent
|
||||
} else {
|
||||
const toArray = (
|
||||
c: string | Array<{ type: string; text?: string; image_url?: { url: string } }> | undefined,
|
||||
): Array<{ type: string; text?: string; image_url?: { url: string } }> => {
|
||||
if (!c) return []
|
||||
if (typeof c === 'string') return c ? [{ type: 'text', text: c }] : []
|
||||
return c
|
||||
}
|
||||
prev.content = [...toArray(prevContent), ...toArray(curContent)]
|
||||
}
|
||||
|
||||
if (msg.tool_calls?.length) {
|
||||
prev.tool_calls = [...(prev.tool_calls ?? []), ...msg.tool_calls]
|
||||
}
|
||||
} else {
|
||||
coalesced.push(msg)
|
||||
}
|
||||
}
|
||||
|
||||
return coalesced
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user