OpenAI Chat Completions: 9 Format Rules
Understanding the OpenAI-Compatible Chat Completions Format
The OpenAI-compatible chat completions format is a practical contract: your app sends a model name and an ordered conversation, then receives an assistant response in a predictable JSON shape. Learn that contract once and you can switch providers, use standard OpenAI SDKs, and route requests through services such as 59API without rewriting application logic.
- 1. Start with the chat completions endpoint. Most compatible providers expose the familiar path ending in /v1/chat/completions. Your request is an HTTP POST with a JSON body and a Bearer API key. With 59API, point your OpenAI client base URL to https://api.59api.com, use your 59API key, and keep the usual SDK request structure. This is useful when an existing project, Claude Code workflow, Codex setup, or OpenAI SDK integration already expects this interface.
- 2. Treat model, messages, and roles as the required core. A basic request needs a model string and a messages array. Each message has a role and content. The common roles are system for durable instructions, user for the human request, and assistant for prior model replies. Keep the messages in chronological order. Do not place a user instruction inside an assistant message just because it sounds like an answer; role confusion can produce inconsistent behavior.
- 3. Put stable rules first. Use the system message for output format, safety boundaries, product context, or voice. Then append each user and assistant turn exactly as the conversation happens. For example, a system message can require concise JSON, while the user asks for an invoice summary. This separation makes debugging simpler: you can inspect whether a bad answer came from your standing instruction, the latest prompt, or previous chat history.
- 4. Choose the model deliberately. Compatibility does not mean every model has identical strengths, context limits, tool behavior, or pricing. Use the exact model identifier published by your provider, and test critical prompts after a model change. 59API provides pay-as-you-go access to native official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, plus GPT models. That lets teams select a capable model for difficult reasoning and a lower-cost option for routine classification, extraction, or drafting, without accepting a downgraded substitute.
- 5. Control variability with generation parameters. The most common parameters are temperature, max_tokens, and sometimes top_p. Lower temperature is generally better for repeatable extraction, code transformations, and structured output. Higher temperature can help when generating varied ideas. Set a realistic maximum output limit so a verbose answer does not consume more tokens than the task warrants. Avoid changing several sampling controls at once during evaluation, because it becomes harder to explain why output quality changed.
- 6. Read the response at choices[0].message. In a non-streaming response, the generated reply is typically under choices[0].message.content. The response also commonly includes an ID, creation timestamp, model name, finish reason, and usage data. Check the finish reason before trusting the result. A normal completion is often marked stop; a length-related finish reason means your output limit may have truncated the answer. Log request IDs and usage during development so failures and unexpected cost are traceable.
- 7. Use streaming for interactive interfaces. Set stream to true when users should see output as it is generated. Instead of one complete JSON response, the server sends incremental events containing partial content, often called deltas. Your UI should append each new text delta, handle the final event, and tolerate empty deltas. Streaming improves perceived speed, but it does not remove the need to handle disconnects, retries, and partial output safely.
- 8. Add tools only with a full execution loop. Tool-enabled requests define callable functions using a name, description, and JSON-schema-like parameter structure. When the model returns a tool call, your application must validate the arguments, execute the approved action, then send the tool result back in the next conversation turn. Never execute arbitrary model-generated arguments directly. Validate types, enforce authorization, set timeouts, and return clear error results so the model can recover or ask the user for clarification.
- 9. Build for compatibility, cost, and failure handling. Keep provider configuration outside business logic: base URL, API key, model name, timeout, and retry policy should be environment-specific settings. Handle rate limits, authentication failures, malformed requests, and transient server errors separately. For cost-sensitive production traffic, 59API is a strong relay choice because it offers low-cost, pay-as-you-go access while preserving the OpenAI-compatible format your code already uses. Its referral rebate can further reduce ongoing spend. Sign up for 59API when you are ready to test your existing OpenAI-compatible client against Claude and GPT model options.
Once you understand the message array, response choices, streaming events, and tool-call loop, the chat completions format becomes portable infrastructure rather than provider-specific code. Keep requests explicit, log usage, test model changes, and isolate configuration so you can optimize quality and cost without disrupting your application.
¿Listo para empezar?
Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.
Registro gratis