Chat completions
Send messages to a text model and get a reply.
POST /v1/chat/completions takes the OpenAI chat completions request and returns the OpenAI
response shape. Existing OpenAI code works by pointing it at https://api.opinference.com/v1.
curl https://api.opinference.com/v1/chat/completions \
-H "Authorization: Bearer $OP_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-oss-120b",
"messages": [
{"role": "system", "content": "Answer in one sentence."},
{"role": "user", "content": "What is a gateway?"}
],
"max_completion_tokens": 200
}'Request
| Field | Notes |
|---|---|
model | Required. A model id from GET /v1/models |
messages | Required. content is a string or an array of content parts |
stream | true streams the reply. See Streaming |
max_completion_tokens | Upper bound on output tokens. max_tokens is read too; when both are sent the larger wins. Reasoning models count their reasoning against it, so a small cap can leave no answer, which fails the request |
temperature | Passed to the model |
tools, tool_choice | See Tool calling |
response_format | See Structured output |
models | Other models that may answer. See Fallbacks |
provider | Routing preferences. See Provider preferences |
op | Retention, tags and session. See Data retention |
plugins | Not supported. A non-empty plugins (web search, file parsing) is refused with 400 invalid_request and param: "plugins" rather than ignored. For web search, run a search tool and pass its results in messages |
Other fields this table does not list are ignored.
The request is routed only to endpoints that support what it asks for. A request with tools
goes only to endpoints that support tool calling, and a request whose prompt and output limit do
not fit an endpoint's context window is not sent there.
Images, PDFs and video
A message's content may be an array of parts. Four part types are accepted:
type | Form | Limits | The model must accept |
|---|---|---|---|
text | {"text": "..."} | The 25 MiB request body | Any model |
image_url | {"image_url": {"url": "..."}}, an https URL or an image data: URI | The 25 MiB request body | Images |
file | {"file": {"filename": "cv.pdf", "file_data": "data:application/pdf;base64,..."}} | 10 MiB per PDF, 16 MiB of PDFs per request, 5 per request | PDFs |
video_url | {"video_url": {"url": "https://..."}} | 1 per request | Video |
{
"model": "google/gemini-3-flash-preview",
"messages": [{
"role": "user",
"content": [
{"type": "text", "text": "List the job titles in this CV."},
{"type": "file", "file": {"filename": "cv.pdf", "file_data": "data:application/pdf;base64,JVBERi0x..."}}
]
}]
}- A
filepart is a PDF sent inline as base64. Its decoded bytes must begin%PDF-.file_id,file_urland other file types are refused. - A
video_urlmust be anhttpsURL with a host name and no user name or password in it. - A part we do not accept is refused with
400 invalid_request, andparamnames it, for examplemessages[0].content[1]. We never drop a part and send the rest. Over a size limit is413 payload_too_large; over a count limit is400 invalid_request. - A request with a PDF goes only to endpoints that have passed our PDF check, and a request with a
video only to those that have passed our video check. A model listing PDFs or video as an input is
not enough, because some endpoints answer while ignoring the part. With no such endpoint the
answer is
404 no_endpoint_availableand no provider is called. - Before routing, a PDF counts as 10,000 input tokens and a video as 25,000 against an endpoint's context window. These are estimates. Your budget reserves up to the endpoint's full context for such a request. You are billed for the input tokens the provider reports, which can be far fewer or far more for the same PDF.
- If you turn on request logging, the log keeps each PDF and video part as its size and SHA-256 only, never the PDF or the URL.
Response
{
"id": "gen_...",
"object": "chat.completion",
"created": 1790940214,
"model": "openai/gpt-oss-120b",
"provider": "nebius",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "A gateway is a single entry point that forwards requests to other services."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 72,
"completion_tokens": 18,
"total_tokens": 90
}
}idis the generation id. It also arrives in thex-op-generation-idheader and is the id shown on your usage and request pages.modelis the model that answered. Withoutmodelsit is always the model you asked for.provideris the id of the provider that answered, the same id as onGET /v1/models/{id}/endpoints. Every stream chunk carries it too.- A reasoning model may add
message.reasoningand reportusage.completion_tokens_details.reasoning_tokens. Reasoning tokens are output tokens and are billed as such. usage.prompt_tokens_details.cached_tokens, when present, counts input tokens served from a prompt cache.
Retrying safely
Send an Idempotency-Key header to make a retry safe. A chat completion is not replayed: a repeat
of a key that is in progress or finished returns 409 idempotency_conflict instead of running and
billing a second time. See Idempotency. Request ids and the
op_metadata receipt are on Request ids and metadata.
Errors
A failed request returns an HTTP error with a JSON envelope:
{
"error": {
"code": "no_endpoint_available",
"message": "no endpoint serves model \"no-such/model\"",
"kind": "terminal",
"scope": "model",
"type": "invalid_request_error",
"param": null,
"request_id": "req_..."
}
}Switch on code. See Errors for every code and which ones are worth retrying.