> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inputapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Legacy text completions

> Use prompt-based completions with JSON and SSE responses.

`POST /v1/completions` accepts a text `prompt`, not a `messages` conversation. Confirm endpoint support with the deployment operator; new applications generally start with [Chat](/en/api-reference/chat-completions) or [Responses](/en/api-reference/responses). An SDK method does not prove a chat model supports this endpoint.

This example requests one text completion and reads `choices[0].text`, requiring `finish_reason=stop`. Streaming increments also use `text`, not Chat's `delta.content`. Length limits, filtering, truncated streams, and empty text fail validation. Output is buffered until completion. The SDK parses SSE; the example validates completion fields and provides no stream resumption.

Install dependencies and set `API_BASE_URL` (including `/v1`), `API_KEY`, and `API_MODEL` as in [Python SDK](/en/guides/python).

```bash theme={null}
.venv/bin/python examples/legacy-completions.py sync
.venv/bin/python examples/legacy-completions.py stream
```

See the [API reference](/en/api-reference/completions). Preservation of legacy fields such as `suffix`, `echo`, `logprobs`, or `best_of` depends on the converter. Protocol reference: [OpenAI Completions](https://developers.openai.com/api/reference/resources/completions/methods/create); gateway behavior follows this project's implementation.

```python theme={null}
import os
import sys
from openai import OpenAI, APIConnectionError, APIStatusError

mode = sys.argv[1] if len(sys.argv) == 2 else "sync"
if mode not in ("sync", "stream"):
    raise SystemExit("Usage: python legacy-completions.py [sync|stream]")
with OpenAI(base_url=os.environ["API_BASE_URL"].rstrip("/"),
            api_key=os.environ["API_KEY"], timeout=60.0, max_retries=0) as client:
    try:
        result = client.completions.create(model=os.environ["API_MODEL"],
            prompt="Complete this sentence: A good API guide should", stream=mode == "stream")
        text, finished = "", False
        if mode == "sync":
            if len(result.choices) != 1 or result.choices[0].index != 0:
                raise ValueError("Expected one completion.")
            choice = result.choices[0]
            if choice.finish_reason != "stop" or not isinstance(choice.text, str):
                raise ValueError("Incomplete completion.")
            text, finished = choice.text, True
        else:
            with result:
                for chunk in result:
                    if not chunk.choices:
                        continue  # Usage-only chunk, not a completion marker.
                    if finished or len(chunk.choices) != 1 or chunk.choices[0].index != 0:
                        raise ValueError("Unexpected completion chunk.")
                    choice = chunk.choices[0]
                    if not isinstance(choice.text, str) or choice.finish_reason not in (None, "stop"):
                        raise ValueError("Incomplete completion.")
                    text += choice.text
                    if len(text) > 1_000_000:
                        raise ValueError("Example output limit exceeded.")
                    finished = choice.finish_reason == "stop"
        if not finished or not text.strip():
            raise ValueError("Missing completed text.")
        print(text)
    except APIStatusError as error:
        raise SystemExit(f"Request failed (HTTP {error.status_code}). No automatic retry.")
    except APIConnectionError:
        raise SystemExit("Connection failed. No automatic retry.")
    except Exception:
        raise SystemExit("Completion did not produce valid complete text.")
```

[阅读中文版](/zh/guides/legacy-completions)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.