Skip to main content

normalize_messages

Function normalize_messages 

Source
pub fn normalize_messages(
    messages: &[Message],
    provider: &str,
    model_id: &str,
) -> Vec<Message>
Expand description

Provider-specific message normalization (empty content filtering, tool ID scrubbing, reasoning injection, tool-use ordering fix). Normalize messages for a specific provider.

This applies provider-specific transforms that the API requires:

  • Anthropic: filter empty text/reasoning parts, add cache_control
  • Claude (via Anthropic): scrub tool IDs to alphanumeric + underscore
  • Mistral: truncate tool IDs to 9 alphanumeric chars
  • DeepSeek: inject empty reasoning parts into assistant messages
  • Anthropic/Vertex: reorder tool_use before text parts