Skip to main content

encode_request

Function encode_request 

Source
pub fn encode_request(req: &CompletionRequest) -> CreateChatCompletionRequest
Expand description

Encodes a CompletionRequest into a wire request body.

Key mapping decisions:

  • Forces stream = true + stream_options.include_usage = true: the protocol layer only runs the SSE branch, and must let the upstream send a trailing usage chunk, otherwise token billing is unavailable.
  • Promotes system to messages[0] as a system message — OpenAI has no top-level system field (unlike Anthropic).
  • A single Message may be split into multiple wire messages in OpenAI format: if a user message mixes MessageContent::ToolResult, it needs a separate tool message (OpenAI uses role: tool + tool_call_id for tool results, which cannot be mixed with user text in the same message).
  • MessageContent::ToolUse in assistant messages maps to the tool_calls field rather than content blocks. args is serialized via serde_json::to_string (the OpenAI protocol requires function.arguments to be stringified JSON).
  • top_k is absent in the OpenAI protocol; the provider layer handles this.
  • max_tokens: the OpenAI dialect deprecates max_tokens in favor of max_completion_tokens. The DeepSeek-compatible dialect still uses max_tokens to align with its OpenAI-compatible endpoint and opencode request format. Neither sets a default like Anthropic — the model decides when not specified.