Skip to main content

OpenAI Chat Completions

The dialect the compatible ecosystem speaks. One mapping, many endpoints.

PathPOST <base_url>/chat/completions
AuthAuthorization: Bearer <key>
Extra headernone
Default modelnone, comes from the endpoint
Sourcesrc/dialect/openai_chat/

This is not the same as the OpenAI page. That one covers OpenAI's own Responses API, which is OpenAI-specific. This page covers Chat Completions, which almost every third party vendor implements, and which OpenAI also still serves.

There is no preset for this dialect​

Freyja ships presets only for the three first-party vendors it tests against. Every endpoint speaking this dialect is third party, so you point at it yourself:

let client = Client::custom(
Dialect::OpenAiChat,
"DeepSeek",
"https://api.deepseek.com/v1",
std::env::var("DEEPSEEK_API_KEY")?,
);
let agent = Agent::new(client);

That is not a lesser path. A preset is only a EndpointConfig with the fields filled in, and this dialect works identically either way.

The reason is maintenance honesty rather than effort. A preset is a standing promise that a base URL and a default model are still current, and these vendors change both faster than this crate could verify. A stale preset fails at the vendor with a confusing 404; a missing one fails locally with a clear message, or does not fail at all because you supplied the current URL.

Custom endpoints has a table of base URLs to start from, and covers keyless local runtimes.

Capability support​

CapabilitySupportedNotes
Text generationyes
Images, any roleyesSent as image_url parts. User, system, assistant, and tool all verified live
System and developer turnsyesNot hoisted, they stay as messages
max_tokensyesmax_tokens or max_completion_tokens, see below
temperature, top_pyesForwarded unchanged
reasoning_effortyesForwarded, support varies by endpoint
response_formatyesAll three variants map
Tool declarationsyesNested under a function key
tool_choiceyes
Tool round tripyesVerified live against DeepSeek
previous_response_idnoRejected with UnsupportedCapability
metadatayesForwarded unchanged
Usage reportingyesField names normalized
RefusalsyesFrom the message's refusal field
Streamingyesstream: true, plus stream_options.include_usage so token counts still arrive. Verified live for text

System turns are not hoisted​

This is the one behavioural difference visible to callers, and the only place where Freyja's handling of system turns changes by dialect.

OpenAI Responses hoists them into instructions, Gemini into system_instruction, Anthropic into system. Here system is a real message role, so the turns stay in the array in the position you put them:

{"messages": [
{"role": "system", "content": "Be concise"},
{"role": "user", "content": "Hello"}
]}

Role::Developer maps onto system too. OpenAI's own newer models accept a developer role, but most compatible endpoints do not, and the portable spelling matters more here than the distinction.

Position therefore does matter on this dialect, where it does not on the other three. Keep system turns at the front.

Every role maps to something different across dialects​

Role::Tool gets a real tool role here, with a tool_call_id:

NeutralOpenAI ChatAnthropicGemini
Tooltool roleuser turnuser_input step

All four dialects handle it differently, which is reasonable evidence the neutral Role::Tool was worth having.

One tool result per message​

Anthropic packs several tool_result blocks into one user turn. Chat Completions gives each result its own message with a single tool_call_id, so a Role::Tool message carrying two results is rejected locally:

invalid request for DeepSeek: each tool message may answer only one tool call;
send one message per result

Message::tool_result builds exactly one per call, so Agent already does the right thing. This only bites if you hand-assemble a tool turn.

Reasoning blocks are dropped, not replayed​

This dialect has no standard place for opaque reasoning state, and no replay requirement either. DeepSeek exposes a reasoning_content field and explicitly says not to send it back.

So InputContent::Reasoning is skipped rather than rejected. That matters when a transcript moves between providers: a conversation started on Anthropic or Gemini carries signed blocks that this dialect cannot express, and refusing them would make switching endpoints mid-conversation impossible.

This is the one deliberate exception to the no-silent-degradation rule, and it is narrow. Freyja is dropping state the target format neither accepts nor requires, not a capability you asked for. Anthropic behaves the same way when a Claude thinking block reaches a different model.

The token cap has two spellings​

This format has had two fields for the output cap. max_tokens came first and is what the compatible ecosystem implements. OpenAI later replaced it with max_completion_tokens, and its newer models now reject the old spelling rather than ignoring it:

Unsupported parameter: 'max_tokens' is not supported with this model.
Use 'max_completion_tokens' instead.

That is a rejection of the field's presence, not its value, so sending both to cover the two spellings fails on exactly the endpoint that needs the new one. Freyja sends one or the other, chosen by the endpoint:

use freyja::{EndpointConfig, Dialect, TokenLimitField};

let config = EndpointConfig::new(
Dialect::OpenAiChat,
"OpenAI",
"https://api.openai.com/v1",
)
.token_limit_field(TokenLimitField::MaxCompletionTokens);

The default is max_tokens, because this dialect is reached only through an explicit EndpointConfig, EndpointPreset::OpenAi is the Responses dialect, so by default it is serving one of the compatible vendors it exists for. Set the field when you point it at OpenAI itself.

Both spellings were verified against api.openai.com: the default is rejected there, and MaxCompletionTokens is accepted.

If OpenAI's own endpoint is the target, the Responses API remains the better fit: it names its cap max_output_tokens and has no such history.

Field mapping​

Outbound​

NeutralWire
modelmodel, from the request or the endpoint default
all turns including systemmessages
max_tokensmax_tokens, or max_completion_tokens per token_limit_field
temperaturetemperature
top_ptop_p
reasoning_effortreasoning_effort
response_formatresponse_format
toolstools, nested under function
tool_choicetool_choice
metadatametadata

Inbound​

WireNeutral
idid
modelmodel
choices[0].finish_reasonstatus
choices[0].message.contentOutputContent::Text
choices[0].message.refusalOutputContent::Refusal
choices[0].message.tool_callsOutputContent::ToolCall
usage.prompt_tokensUsage::input_tokens
usage.completion_tokensUsage::output_tokens
everything elseprovider_metadata

Only the first choice is read. The neutral request has no way to ask for more than one, so there is never a second.

Status mapping​

Wire finish_reasonNeutral
stop, absentCompleted
lengthIncomplete
tool_callsRequiresAction
function_callRequiresAction
content_filterOther("content_filter")
anything elseOther(String)

function_call is the pre-2023 spelling and is still emitted by some compatible endpoints, so both map to RequiresAction.

content_filter stays as Other rather than becoming Failed, because the request succeeded and the endpoint chose to withhold part of the answer.

Streaming asks for usage explicitly​

A streaming request on this dialect gets no token counts unless it asks, so Freyja sends stream_options: {"include_usage": true} alongside stream: true. Without it the terminal frame carries no usage at all and StreamEvent::Done would arrive with usage: None on the most widely spoken dialect.

{"model": "...", "messages": [...], "stream": true, "stream_options": {"include_usage": true}}

This dialect also has no end frame beyond [DONE], so a tool call is complete only once the body closes. Freyja flushes pending calls at that point, which is why StreamEvent::ToolCall can arrive later here than on the other dialects. See Streaming.

Rejected before the network​

ConditionError
previous_response_id setUnsupportedCapability
A tool message answering more than one callInvalidRequest
No model on the request and none on the endpointInvalidRequest

Verification status​

Verified live against DeepSeek, full tool round trip:

assistant: I'll add those numbers together for you.
tool call: add({"a": 20, "b": 22})
tool result: 42
assistant: 20 + 22 = 42.
usage: 378 tokens

That covers the message array, the nested function tool schema, tool_calls on the assistant message, the tool role with tool_call_id, and the usage field names.

What it does not cover:

Status
Text generation and tool callingverified live, on DeepSeek only
Streaming, textverified live against DeepSeek
Streaming, tool callsnot exercised live, on this or any dialect
Imagesan image_url part accepted on a user, system, assistant, and tool turn
response_format, reasoning_effortnot exercised, on any endpoint

What one endpoint cannot tell you is how the others behave. "Compatible" is a spectrum, and Groq, Together, OpenRouter, and Ollama are each unverified here.

A text turn has been streamed live through this dialect against DeepSeek: 191 deltas, usage on Done via stream_options.include_usage, and into_response rebuilding the same text. That is one compatible endpoint out of many, and the risk stays highest here, because "compatible" endpoints diverge most in their streaming frames.

Streamed tool calls have not been run anywhere. The frame shapes and the [DONE] sentinel come from vendor documentation and are tested against recorded fixtures, with streamed_response_matches_generate asserting that a drained stream matches what generate builds from the same turn, an offline parity test.