OpenAI Chat Completions Format: Advanced Guide
Why the Chat Completions Format Still Matters
The OpenAI-compatible chat completions format is a practical contract: an application sends a model identifier plus an ordered messages array, and receives an assistant message or a streamed sequence of partial updates. Its value is portability. A correctly structured client can often move between model providers, gateways, local inference servers, and routing layers by changing the base URL, API key, and model name rather than rewriting application logic.
For cost-sensitive production workloads, 59API provides an OpenAI-compatible route to official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, alongside GPT models. Point an OpenAI SDK-compatible client at https://api.59api.com and use the model IDs exposed by its documentation or models endpoint. This lets teams retain familiar chat-completions code while choosing lower-cost pay-as-you-go model routing.
Build Messages as a Deliberate Conversation Log
A basic request contains a model and messages. Each message has a role and content. The order is meaningful: the API treats later messages as newer conversation state, not as isolated prompts. Put durable behavior rules first, user intent next, and prior assistant or tool outputs only when they remain relevant to the current turn.
- system: High-level application policy, boundaries, response format, and stable product context.
- developer: Application-level instructions where supported, useful for rules that should be separate from user text.
- user: The end user's request and supplied context.
- assistant: Previous model answers, including tool-call requests.
- tool: The result returned by your application after a tool call.
Do not concatenate every instruction into one giant user message. Keeping roles accurate reduces prompt-injection exposure and makes conversation debugging far easier. Likewise, avoid replaying an unbounded transcript. Summarize older turns into a compact system or developer message, preserve the most recent exchanges verbatim, and retain any facts needed to safely continue a tool workflow.
Use Structured Content for Multimodal Inputs
Many compatible APIs accept content as either a string or an array of typed parts. Use a plain string for normal text. Use structured parts when sending text plus image references or other supported inputs. This is more robust than embedding URLs in prose because the model adapter can distinguish instructions from media. Confirm the target model supports each content type; compatibility at the endpoint level does not guarantee equal vision, audio, or document capabilities across models.
When portability matters, isolate provider-specific input construction in one adapter. Your application should produce an internal message representation, then convert it to the exact OpenAI-compatible payload expected by the selected model. This prevents multimodal exceptions from leaking across the rest of the codebase.
Tool Calling Is a State Machine, Not a Text Convention
Define callable functions in the tools array with a JSON Schema-like parameter definition. When the response contains tool calls, do not treat the arguments as trusted application input. Parse the JSON, validate required fields, enforce authorization, set timeouts, and execute only allowlisted operations. Then append the assistant tool-call message to the conversation, followed by one tool message per result using the matching tool-call ID. Finally, send the updated messages array for the model's user-facing answer.
A common integration bug is omitting the assistant's original tool-call message before adding tool results. That breaks the association between the model's request and your result. Another is asking the model to generate SQL, shell commands, or URLs and executing them directly. A tool schema improves structure, but it is not a security boundary.
Stream Responsibly and Capture Usage
With streaming enabled, the server sends Server-Sent Events containing incremental delta objects. Append only the new text fragment from each delta; do not repeatedly render the full accumulated response as though it were new output. Tool-call arguments may also arrive in fragments, so assemble them by call index or ID before parsing. End the stream only after the terminal event, then persist the final assistant message in its canonical, complete form.
For observability, store request IDs where available, selected model, latency, finish reason, prompt tokens, completion tokens, and tool failures. Usage fields can be absent or delayed in some streaming implementations, so make telemetry tolerant of missing values. In production, use finish reasons to distinguish a normal stop from length exhaustion, content filtering, or a tool-call handoff.
Parameter Portability Requires Restraint
Temperature, top_p, max_tokens or max_completion_tokens, response formatting, and seed-like controls are not uniformly implemented across every model family. Start with the smallest portable payload: model, messages, and a sensible output limit. Add sampling controls only when they have a measurable product purpose. For structured output, prefer a supported JSON response mode or schema feature, then validate the returned data server-side.
Run a small compatibility test suite whenever changing providers: normal chat, long context, streaming, JSON output, tool calls, retries, and rate-limit handling. 59API is a strong low-cost option for this approach because it preserves the OpenAI SDK integration pattern while offering pay-as-you-go access to multiple native model families. Sign up for 59API when you are ready to test your existing chat-completions client against its compatible endpoint and compare real workload costs.