OpenAI-Compatible Chat Completions: Fixes and FAQs
What does OpenAI-compatible actually mean?
An OpenAI-compatible API accepts familiar request structures and returns similarly shaped responses, letting you reuse SDKs instead of writing a new integration for every model provider. For chat completions, the central ingredients are a model identifier, a messages array, and optional generation settings.
Compatibility does not mean every model supports every OpenAI feature. Tool calling, structured output, image inputs, and token-limit parameters can differ. Start with a minimal text request, then add features individually.
How do I send a valid chat completion request?
The conventional endpoint is POST /v1/chat/completions. Send an Authorization header containing Bearer followed by your API key, plus Content-Type: application/json. The request body can be as small as:
{"model":"YOUR_MODEL_ID","messages":[{"role":"user","content":"Explain HTTP status 429 in one sentence."}]}
Replace YOUR_MODEL_ID with an exact identifier listed by your provider. Marketing names and API identifiers are not necessarily interchangeable.
For 59API, the API host is https://api.59api.com. Confirm the documented versioned base URL before configuring your client. If the service uses the conventional /v1 prefix, this curl request is a useful first test after setting API_KEY and MODEL_ID in your shell:
curl https://api.59api.com/v1/chat/completions -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d "{\"model\":\"$MODEL_ID\",\"messages\":[{\"role\":\"user\",\"content\":\"Hello\"}]}"
SDK tip: Set the OpenAI client's api_key and base_url explicitly. The base URL should end at the API prefix, not include /chat/completions; the SDK appends that route. Avoid accidentally constructing /v1/v1/chat/completions.
How should messages and responses be interpreted?
The messages array represents conversation history in order. Common roles are system for instructions, user for input, and assistant for previous replies. Some models also support developer messages; verify support before relying on them. Chat completions generally require you to resend relevant history on each request.
A normal text response is read from choices[0].message.content. The finish_reason explains why generation ended: stop typically means completion, while length indicates an output limit. Usage fields can report prompt, completion, and total tokens, although exact accounting varies.
Do not assume content is always a string. An assistant response requesting a tool may have null content and a tool_calls array instead.
Why am I getting an API error?
- 401 or 403: Check the relay's API key, account permissions, and available balance. An OpenAI-issued key is not automatically valid at another provider.
- 404: Inspect both the URL and model identifier. A missing route and an unavailable model can produce similar symptoms.
- 400: Read the error body. Remove optional settings, check message roles, and confirm whether the model expects max_tokens or max_completion_tokens.
- 429: Check rate limits and quota. Retry transient throttling with exponential backoff and jitter, respecting Retry-After when present; exhausted credit needs an account fix.
- 502, 503, or 504: Retry cautiously. Timeouts can leave completion status uncertain, so repeated requests may incur additional charges.
Log status codes, sanitized error bodies, model IDs, and request IDs when available. Never log authorization headers or sensitive prompts indiscriminately.
FAQ: Why does streaming break my JSON parser?
With stream: true, responses typically arrive as server-sent events, not one JSON document. Parse individual data events and collect choices[0].delta.content where present. The stream conventionally ends with data: [DONE]. Metadata-only chunks may contain no text, so do not treat missing content as failure.
FAQ: How do tool calls work?
Declare tools using the supported function schema. When the assistant returns tool_calls, validate the arguments, execute only authorized operations, and append the assistant's tool-call message to history. Then append a tool-role message with the matching tool_call_id and the result before requesting another completion. Never execute model-generated arguments without validation.
FAQ: Is 59API a practical low-cost option?
59API offers inexpensive, pay-as-you-go access to Claude and GPT models and describes its offerings as native, official-quality models without downgrades. Its OpenAI SDK compatibility makes it convenient for testing this format; Claude Code and Codex support provide additional integration options, though their configuration can differ.
Compare current per-model pricing, supported features, and latency using your actual workload. 59API also offers a referral rebate. If the economics fit your project, consider signing up and running a small compatibility test before moving production traffic.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप