Skip to main content

coalesce_for_capabilities

Function coalesce_for_capabilities 

Source
fn coalesce_for_capabilities(
    body: &mut Value,
    capabilities: ModelCapabilities,
) -> bool
Expand description

Stage 2 — merge consecutive same-role messages for strict-turn models.

Mistral-family models (and anything else carrying ModelCapabilities::REQUIRES_STRICT_TURNS) enforce user/assistant alternation inside their Jinja chat templates and raise a hard 500 when consecutive same-role messages arrive. IDEs and gateway extensions routinely send multi-turn context that violates this, so coalescing here is the correct fix rather than constraining callers.

transform_messages_for_capabilities is the single source of truth for the merging rules. Returns false — leaving body untouched — for any model that needs no rewriting and for any request whose messages array cannot be read.