Skip to content
Primary navigation

Create thread

Deprecated
client.beta.threads.create(ThreadCreateParams { messages, metadata, tool_resources } body?, RequestOptionsoptions?): Thread { id, created_at, metadata, 2 more }
POST/threads

Create a thread.

ParametersExpand Collapse
body: ThreadCreateParams { messages, metadata, tool_resources }
messages?: Array<Message>

A list of messages to start the thread with.

content: string | Array<MessageContentPartParam>

The text contents of the message.

One of the following:
string
ImageFileContentBlock { image_file, type }

References an image File in the content of a message.

image_file: ImageFile { file_id, detail }
file_id: string

The File ID of the image in the message content. Set purpose="vision" when uploading the File if you need to later display the file content.

detail?: "auto" | "low" | "high"

Specifies the detail level of the image if specified by the user. low uses fewer tokens, you can opt in to high resolution using high.

One of the following:
"auto"
"low"
"high"
type: "image_file"

Always image_file.

ImageURLContentBlock { image_url, type }

References an image URL in the content of a message.

image_url: ImageURL { url, detail }
url: string

The external URL of the image, must be a supported image types: jpeg, jpg, png, gif, webp.

formaturi
detail?: "auto" | "low" | "high"

Specifies the detail level of the image. low uses fewer tokens, you can opt in to high resolution using high. Default value is auto

One of the following:
"auto"
"low"
"high"
type: "image_url"

The type of the content part.

TextContentBlockParam { text, type }

The text content that is part of a message.

text: string

Text content to be sent to the model

type: "text"

Always text.

role: "user" | "assistant"

The role of the entity that is creating the message. Allowed values include:

  • user: Indicates the message is sent by an actual user and should be used in most cases to represent user-generated messages.
  • assistant: Indicates the message is generated by the assistant. Use this value to insert messages from the assistant into the conversation.
One of the following:
"user"
"assistant"
attachments?: Array<Attachment> | null

A list of files attached to the message, and the tools they should be added to.

file_id?: string

The ID of the file to attach to the message.

tools?: Array<CodeInterpreterTool { type } | FileSearch { type } >

The tools to add this file to.

One of the following:
CodeInterpreterTool { type }
type: "code_interpreter"

The type of tool being defined: code_interpreter

FileSearch { type }
type: "file_search"

The type of tool being defined: file_search

metadata?: Metadata | null

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard.

Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.

metadata?: Metadata | null

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard.

Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.

tool_resources?: ToolResources | null

A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the code_interpreter tool requires a list of file IDs, while the file_search tool requires a list of vector store IDs.

code_interpreter?: CodeInterpreter { file_ids }
file_ids?: Array<string>

A list of file IDs made available to the code_interpreter tool. There can be a maximum of 20 files associated with the tool.

One of the following:
ReturnsExpand Collapse
Thread { id, created_at, metadata, 2 more }

Represents a thread that contains messages.

id: string

The identifier, which can be referenced in API endpoints.

created_at: number

The Unix timestamp (in seconds) for when the thread was created.

metadata: Metadata | null

Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format, and querying for objects via API or the dashboard.

Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.

object: "thread"

The object type, which is always thread.

tool_resources: ToolResources | null

A set of resources that are made available to the assistant's tools in this thread. The resources are specific to the type of tool. For example, the code_interpreter tool requires a list of file IDs, while the file_search tool requires a list of vector store IDs.

code_interpreter?: CodeInterpreter { file_ids }
file_ids?: Array<string>

A list of file IDs made available to the code_interpreter tool. There can be a maximum of 20 files associated with the tool.

Create thread

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const emptyThread = await openai.beta.threads.create();

  console.log(emptyThread);
}

main();
{
  "id": "thread_abc123",
  "object": "thread",
  "created_at": 1699012949,
  "metadata": {},
  "tool_resources": {}
}

Create thread

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const messageThread = await openai.beta.threads.create({
    messages: [
      {
        role: "user",
        content: "Hello, what is AI?"
      },
      {
        role: "user",
        content: "How does AI work? Explain it in simple terms.",
      },
    ],
  });

  console.log(messageThread);
}

main();
{
  "id": "thread_abc123",
  "object": "thread",
  "created_at": 1699014083,
  "metadata": {},
  "tool_resources": {}
}
Returns Examples
{
  "id": "id",
  "created_at": 0,
  "metadata": {
    "foo": "string"
  },
  "object": "thread",
  "tool_resources": {
    "code_interpreter": {
      "file_ids": [
        "string"
      ]
    },
    "file_search": {
      "vector_store_ids": [
        "string"
      ]
    }
  }
}