pub fn encode_request(req: &CompletionRequest) -> CreateChatCompletionRequestExpand 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
systemtomessages[0]as a system message — OpenAI has no top-levelsystemfield (unlike Anthropic). - A single
Messagemay be split into multiple wire messages in OpenAI format: if a user message mixesMessageContent::ToolResult, it needs a separate tool message (OpenAI usesrole: tool+tool_call_idfor tool results, which cannot be mixed with user text in the same message). MessageContent::ToolUsein assistant messages maps to thetool_callsfield rather than content blocks.argsis serialized viaserde_json::to_string(the OpenAI protocol requiresfunction.argumentsto be stringified JSON).top_kis absent in the OpenAI protocol; the provider layer handles this.max_tokens: the OpenAI dialect deprecatesmax_tokensin favor ofmax_completion_tokens. The DeepSeek-compatible dialect still usesmax_tokensto align with its OpenAI-compatible endpoint and opencode request format. Neither sets a default like Anthropic — the model decides when not specified.