Skip to main content

normalize_chat_completion_body

Function normalize_chat_completion_body 

Source
pub fn normalize_chat_completion_body(
    body: &mut Value,
    dialect: Option<&DialectSpec>,
) -> Vec<NormalizationError>
Expand description

Normalize a complete (non-streaming) chat.completion response body in place, using the parser for the model’s resolved dialect.

Only message.content strings are processed; a null, absent, or non-string content is left untouched, as is everything else in the body. For models with no dialect the parser is the identity passthrough and the body comes back byte-identical.

When markup is extracted:

  • message.content becomes the remaining text, or null when a tool call consumed all of it (the OpenAI shape for tool-call turns);
  • extracted calls are appended to message.tool_calls, arguments serialized to a compact JSON string exactly as the streaming encoder does;
  • reasoning captured by the parser is appended to message.reasoning_content;
  • a finish_reason of stop/null is upgraded to "tool_calls".

Returns every NormalizationError the parser surfaced; the caller decides whether to log them or surface the raw bytes to the client, as the streaming path does.