Implementation:Openai Openai node Beta Threads Resource
| Knowledge Sources | |
|---|---|
| Domains | SDK, Assistants API |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Threads class is the Beta Threads resource in the openai-node SDK, providing methods to create, retrieve, update, delete, and run threads within the deprecated Assistants API.
Description
The Threads class extends APIResource and is located under the beta.threads namespace of the OpenAI client. It wraps the /threads REST endpoints and provides full CRUD operations for thread objects. Threads represent conversation containers that hold messages for use with the Assistants API. The entire class is marked as @deprecated in favor of the newer Responses API.
The resource exposes two sub-resources: runs (of type Runs) for managing assistant runs within a thread, and messages (of type Messages) for managing individual messages. Key methods include create, retrieve, update, delete, createAndRun (with streaming and non-streaming overloads), createAndRunPoll (a polling helper), and createAndRunStream (returns an AssistantStream).
All requests include the OpenAI-Beta: assistants=v2 header automatically. The Thread interface returned by these methods includes fields for id, created_at, metadata, object, and tool_resources (supporting code_interpreter and file_search).
Usage
Use this resource when working with the Assistants API to manage conversation threads. Note that OpenAI has deprecated the Assistants API in favor of the Responses API, so this resource is primarily for maintaining existing integrations.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/beta/threads/threads.ts
Signature
export class Threads extends APIResource {
runs: RunsAPI.Runs;
messages: MessagesAPI.Messages;
create(body?: ThreadCreateParams | null | undefined, options?: RequestOptions): APIPromise<Thread>;
retrieve(threadID: string, options?: RequestOptions): APIPromise<Thread>;
update(threadID: string, body: ThreadUpdateParams, options?: RequestOptions): APIPromise<Thread>;
delete(threadID: string, options?: RequestOptions): APIPromise<ThreadDeleted>;
createAndRun(body: ThreadCreateAndRunParamsNonStreaming, options?: RequestOptions): APIPromise<Run>;
createAndRun(body: ThreadCreateAndRunParamsStreaming, options?: RequestOptions): APIPromise<Stream<AssistantStreamEvent>>;
createAndRunPoll(
body: ThreadCreateAndRunParamsNonStreaming,
options?: RequestOptions & { pollIntervalMs?: number },
): Promise<Run>;
createAndRunStream(body: ThreadCreateAndRunParamsBaseStream, options?: RequestOptions): AssistantStream;
}
Import
import OpenAI from 'openai';
// Access via client.beta.threads
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| body (create) | ThreadCreateParams |
No | Optional messages, metadata, and tool_resources to initialize the thread |
| threadID (retrieve) | string |
Yes | The ID of the thread to retrieve |
| threadID (update) | string |
Yes | The ID of the thread to modify |
| body (update) | ThreadUpdateParams |
Yes | Metadata and tool_resources to update |
| threadID (delete) | string |
Yes | The ID of the thread to delete |
| body (createAndRun) | ThreadCreateAndRunParams |
Yes | Must include assistant_id; optionally thread, model, instructions, tools, stream, etc. |
Outputs
| Name | Type | Description |
|---|---|---|
| Thread | Thread |
Object with id, created_at, metadata, object ('thread'), and tool_resources |
| ThreadDeleted | ThreadDeleted |
Object with id, deleted (boolean), and object ('thread.deleted') |
| Run | Run |
A run object returned by createAndRun (non-streaming) or createAndRunPoll |
| AssistantStream | AssistantStream |
An event stream returned by createAndRunStream |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI();
// Create a thread
const thread = await client.beta.threads.create({
messages: [
{ role: 'user', content: 'Hello, can you help me?' }
],
});
// Retrieve a thread
const retrieved = await client.beta.threads.retrieve(thread.id);
// Update a thread
const updated = await client.beta.threads.update(thread.id, {
metadata: { topic: 'support' },
});
// Create a thread and run it with an assistant
const run = await client.beta.threads.createAndRun({
assistant_id: 'asst_abc123',
thread: {
messages: [{ role: 'user', content: 'Explain quantum computing.' }],
},
});
// Create and run with streaming
const stream = client.beta.threads.createAndRunStream({
assistant_id: 'asst_abc123',
thread: {
messages: [{ role: 'user', content: 'Write a poem.' }],
},
});
// Delete a thread
const deleted = await client.beta.threads.delete(thread.id);