OpenAI-Compatible Chat Completions: A Quick Start
Need to connect an application to different AI models without rewriting its integration for every provider? The OpenAI-compatible chat completions format gives you a familiar request and response structure. This quick-start guide covers the endpoint, message roles, streaming, tool calls, and the details to check when switching models.
What the format does
Chat completions represents a conversation as an ordered list of messages. Your application sends that list to an API endpoint; the service returns one or more assistant responses. An OpenAI-compatible service accepts a similar schema and SDK conventions, even when the selected model comes from another provider.
For 59API, the base URL is https://api.59api.com. With an OpenAI SDK, set that as the base URL and send chat completion requests to /v1/chat/completions. Use an API key from your account as a bearer credential. Do not put the key in browser code or commit it to a repository.
Build a basic request
At minimum, send a model name and a messages array. Each message has a role and content. Common roles are system or developer for instructions, user for the input, and assistant for prior model replies. Preserve conversation order when you include history; the model uses it as context.
A request can also include options such as temperature, max_tokens, and stream. For example, select the current model name GPT-6.1 Sol, include a user message asking for a short summary, and set max_tokens to limit the response length. Check the model documentation for supported parameters: compatibility does not guarantee that every model accepts every optional field or interprets it identically.
Keep the message content in the format your task needs. Plain text works for ordinary chat. If you are sending structured or multimodal content, confirm the selected model and relay support that content type rather than assuming every compatible endpoint handles it.
Read the response
A non-streaming response typically includes an id, a choices array, and usage information. Read the assistant message from the first choice's message.content. The finish_reason indicates why generation stopped; for example, it can distinguish a normal end from a token limit or a tool handoff. Usage reports input and output tokens when provided, which can help you monitor costs.
Handle failures as part of the integration. Check the HTTP status and error body, set reasonable timeouts, and avoid retrying every error blindly. Rate limits and temporary server errors may be retryable with backoff; invalid credentials or malformed requests need a code or configuration fix.
Enable streaming or tools
For incremental output, set stream to true. The server then sends server-sent events rather than one complete JSON response. Read each event as it arrives, append content deltas to the current answer, and stop when the stream signals completion. Make sure your HTTP client supports streaming and that any proxy between your app and the API does not buffer the response.
Tool use follows a request-and-response cycle. Provide tool definitions with names, descriptions, and JSON parameter schemas. When the assistant returns a tool call, validate its arguments, run the corresponding function in your application, then send the tool result back in a message associated with that call. A tool call is a request for your application to act, not an instruction to execute arbitrary model-generated text.
Try it with 59API
59API offers pay-as-you-go access to models from multiple providers through one relay, including Gemini 3.1 Pro and GPT-6.1 Sol. Its OpenAI SDK compatibility lets you keep the familiar chat completions workflow while changing the base URL and model selection. The service is positioned as a low-cost option with no model downgrade, plus a referral rebate. Review current pricing and model availability before deployment, then sign up for 59API to test the integration with your own workload.
Before shipping, verify the chosen model name, test both normal and streaming responses, and confirm how your application handles errors, token usage, and tool calls. Those checks make switching models much less disruptive.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप