Implementation:Openai Openai node Legacy Completions Resource
| Knowledge Sources | |
|---|---|
| Domains | SDK, Text Completions |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The legacy Completions class provides access to the OpenAI text completions endpoint (/completions), which generates completions for a given prompt using older models like gpt-3.5-turbo-instruct.
Description
The Completions class at src/resources/completions.ts extends APIResource and wraps the /completions REST endpoint. Unlike the Chat Completions resource, this resource works with raw text prompts rather than structured message arrays. It is accessed via client.completions and provides a single create method with overloads for streaming and non-streaming modes.
The create method accepts a CompletionCreateParams body containing the model and prompt (which can be a string, array of strings, array of tokens, or array of token arrays). The response is a Completion object containing an array of CompletionChoice items, each with text, finish_reason, index, and optional logprobs. The CompletionUsage type tracks token counts including completion_tokens, prompt_tokens, total_tokens, and detailed breakdowns for cached and reasoning tokens.
This resource supports legacy models such as gpt-3.5-turbo-instruct, davinci-002, and babbage-002. Parameters include best_of, echo, frequency_penalty, logit_bias, logprobs, max_tokens, suffix, temperature, top_p, and stop.
Usage
Use this resource for legacy text completion workflows that require raw prompt-in/text-out behavior. For new projects, the Chat Completions API (client.chat.completions) or the Responses API is recommended instead.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/completions.ts
Signature
export class Completions extends APIResource {
create(body: CompletionCreateParamsNonStreaming, options?: RequestOptions): APIPromise<Completion>;
create(body: CompletionCreateParamsStreaming, options?: RequestOptions): APIPromise<Stream<Completion>>;
create(
body: CompletionCreateParamsBase,
options?: RequestOptions,
): APIPromise<Stream<Completion> | Completion>;
}
Import
import OpenAI from 'openai';
// Access via client.completions
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | 'gpt-3.5-turbo-instruct' | 'davinci-002' | 'babbage-002' | Yes | The model ID to use for completion |
| prompt | Array<string> | Array<number> | Array<Array<number>> | null | Yes | The prompt(s) to generate completions for |
| stream | boolean |
No | Whether to stream back partial progress |
| max_tokens | number |
No | Maximum number of tokens to generate |
| temperature | number |
No | Sampling temperature between 0 and 2 |
| top_p | number |
No | Nucleus sampling parameter |
| best_of | number |
No | Generates best_of completions server-side and returns the best |
| echo | boolean |
No | Echo back the prompt in addition to the completion |
| suffix | string |
No | Suffix that comes after a completion of inserted text |
| stop | Array<string> | No | Up to 4 sequences where the API will stop generating |
| logprobs | number |
No | Include log probabilities on the most likely output tokens (max 5) |
Outputs
| Name | Type | Description |
|---|---|---|
| Completion | Completion |
Object with id, choices (array of CompletionChoice with text, finish_reason), created, model, object ('text_completion'), and optional usage |
| CompletionChoice | CompletionChoice |
'length' | 'content_filter'), index, and logprobs |
| CompletionUsage | CompletionUsage |
Token usage statistics: completion_tokens, prompt_tokens, total_tokens, and detail breakdowns |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI();
// Basic text completion
const completion = await client.completions.create({
model: 'gpt-3.5-turbo-instruct',
prompt: 'This is a test.',
max_tokens: 50,
});
console.log(completion.choices[0].text);
// Streaming text completion
const stream = await client.completions.create({
model: 'gpt-3.5-turbo-instruct',
prompt: 'Once upon a time',
max_tokens: 100,
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0].text);
}