Skip to main content

Text completion

/v1/completions is the OpenAI text completion endpoint. It takes a prompt instead of a list of messages and returns plain text. It exists for older applications and for models that are served as completion models, such as code completion and fill-in-the-middle models. For new work, use chat completions, which every provider supports. The gateway serves both, and can even answer a text completion request with a chat model, so applications keep working while you migrate.

Scenario​

An editor plugin provides inline code completion with a fill-in-the-middle model and sends prompt and suffix to /v1/completions. The platform team moves the plugin from a direct provider key to the gateway so completions are budgeted per developer and logged. Later they replace the completion model with a chat model behind the same name, and the plugin keeps working because the gateway wraps the prompt into a chat message.

Steps​

  1. Register the model. In the Admin UI open Models + Endpoints (/ui/models-and-endpoints), click Add Model, pick the Provider, choose the model, set the Public Model Name and set Mode to Completion - /completions so health checks use this format, then click Add Model.

  2. Create a key in Virtual Keys (/ui/api-keys) and pick the model under Models.

  3. Send a completion.

    curl -X POST http://localhost:4000/v1/completions \
    -H "Authorization: Bearer $SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
    "model": "gpt-3.5-turbo-instruct",
    "prompt": "def fibonacci(n):",
    "max_tokens": 120,
    "temperature": 0
    }'
  4. Check the log. Open Logs (/ui/logs) and find the request with its tokens and cost.

Expected result​

{
"id": "cmpl-6a1f9b2c",
"object": "text_completion",
"created": 1760441234,
"model": "gpt-3.5-turbo-instruct",
"choices": [{"text": "\n if n < 2:\n return n\n return fibonacci(n - 1) + fibonacci(n - 2)", "index": 0, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 5, "completion_tokens": 34, "total_tokens": 39}
}

Request fields​

prompt can be a string or a list of strings, and the parameters max_tokens, temperature, top_p, n, stream, stop, logprobs, echo, suffix, presence_penalty, frequency_penalty, best_of, logit_bias and user are accepted. suffix enables fill-in-the-middle on models that support it.

Chat models through the completion endpoint​

When the deployment behind the public model name is a chat model, the gateway wraps the prompt in a user message, calls the chat API and unwraps the answer, so the completion endpoint keeps working as you migrate models. The reverse also works. A completion model registered with text-completion-openai/ or text-completion-codestral/ can be called through /v1/chat/completions and the gateway renders the messages into a prompt.

Gateway features​

The same gateway features apply as for chat completions, keys, budgets, routing, caching, guardrails and cost tracking.

Errors​

StatusWhat it meansFix
400 BadRequestErrorprompt is missing, or a parameter such as suffix is not supported by the model.Send a prompt, and use a fill-in-the-middle model for suffix.
401 AuthenticationErrorThe key is missing, wrong or expired.Use a live key from Virtual Keys.
403 PermissionDeniedErrorThe key may not call this model.Add the model to the key's Models.
400 Invalid model name passed in model=<name>No deployment carries this model name.Check Models + Endpoints, or call GET /v1/models.
422 budget_exceededA key, team, user or organization budget is used up.Raise the budget or wait for its reset.
429 RateLimitErrorThe rate limit was reached or the provider is throttling.Raise the limit or add a fallback.

Logs​

Each call is one row on the Logs page (/ui/logs) with its key, team, model group, deployment, tokens, cost, latency and status, failed calls included. Prompt and response text is kept only when the gateway is set to store it, see what the gateway stores. Operator log lines for routing, retries and fallbacks are in the log reference.

Providers​

The gateway's endpoint support matrix marks 3 provider integrations as supporting this endpoint:

Azure, OpenAI, Tensormesh (tensormesh).