Assistants (Beta)
Thread Messages
POST
/
v1
/
threads
/
{thread_id}
/
messages
from openai import OpenAI
client = OpenAI()
# Create a message
message = client.beta.threads.messages.create(
thread_id="thread_abc123",
role="user",
content="How does AI work? Explain it in simple terms."
)
# List messages
messages = client.beta.threads.messages.list(
thread_id="thread_abc123"
)
for msg in messages:
print(msg.role, msg.content)
import OpenAI from "openai";
const openai = new OpenAI();
// Create a message
const message = await openai.beta.threads.messages.create(
"thread_abc123",
{
role: "user",
content: "How does AI work? Explain it in simple terms."
}
);
// List messages
const messages = await openai.beta.threads.messages.list(
"thread_abc123"
);
for (const msg of messages.data) {
console.log(msg.role, msg.content);
}
curl https://api.openai.com/v1/threads/thread_abc123/messages \\
-H "Authorization: Bearer $OPENAI_API_KEY" \\
-H "Content-Type: application/json" \\
-H "OpenAI-Beta: assistants=v2" \\
-d '{
"role": "user",
"content": "How does AI work? Explain it in simple terms."
}'
{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1699017614,
"thread_id": "thread_abc123",
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "How does AI work? Explain it in simple terms.",
"annotations": []
}
}
],
"attachments": [],
"metadata": {}
}
The Assistants API is deprecated in favor of the Responses API.
Path Parameters
string
required
The ID of the thread to create a message for.
Request Body
string
required
The role of the entity that is creating the message. Allowed values:
user: Indicates the message is sent by an actual userassistant: Indicates the message is generated by the assistant
string
required
The text contents of the message.
array
object
Set of 16 key-value pairs that can be attached to an object.Keys are strings with a maximum length of 64 characters. Values are strings with a maximum length of 512 characters.
Response
Returns aMessage object.
string
The message identifier.
string
The object type, always
thread.message.integer
The Unix timestamp (in seconds) for when the message was created.
string
The thread ID that this message belongs to.
string
The entity that produced the message. One of
user or assistant.array
The content of the message in array of text and/or images.
List Messages
GET https://api.openai.com/v1/threads/{thread_id}/messages
Returns a list of messages for a given thread.
Query Parameters
integer
default:"20"
A limit on the number of objects to be returned. Limit can range between 1 and 100.
string
default:"desc"
Sort order by the
created_at timestamp. asc for ascending order and desc for descending order.string
A cursor for use in pagination.
string
A cursor for use in pagination.
string
Filter messages by the run ID that generated them.
from openai import OpenAI
client = OpenAI()
# Create a message
message = client.beta.threads.messages.create(
thread_id="thread_abc123",
role="user",
content="How does AI work? Explain it in simple terms."
)
# List messages
messages = client.beta.threads.messages.list(
thread_id="thread_abc123"
)
for msg in messages:
print(msg.role, msg.content)
import OpenAI from "openai";
const openai = new OpenAI();
// Create a message
const message = await openai.beta.threads.messages.create(
"thread_abc123",
{
role: "user",
content: "How does AI work? Explain it in simple terms."
}
);
// List messages
const messages = await openai.beta.threads.messages.list(
"thread_abc123"
);
for (const msg of messages.data) {
console.log(msg.role, msg.content);
}
curl https://api.openai.com/v1/threads/thread_abc123/messages \\
-H "Authorization: Bearer $OPENAI_API_KEY" \\
-H "Content-Type: application/json" \\
-H "OpenAI-Beta: assistants=v2" \\
-d '{
"role": "user",
"content": "How does AI work? Explain it in simple terms."
}'
{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1699017614,
"thread_id": "thread_abc123",
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "How does AI work? Explain it in simple terms.",
"annotations": []
}
}
],
"attachments": [],
"metadata": {}
}
from openai import OpenAI
client = OpenAI()
# Create a message
message = client.beta.threads.messages.create(
thread_id="thread_abc123",
role="user",
content="How does AI work? Explain it in simple terms."
)
# List messages
messages = client.beta.threads.messages.list(
thread_id="thread_abc123"
)
for msg in messages:
print(msg.role, msg.content)
import OpenAI from "openai";
const openai = new OpenAI();
// Create a message
const message = await openai.beta.threads.messages.create(
"thread_abc123",
{
role: "user",
content: "How does AI work? Explain it in simple terms."
}
);
// List messages
const messages = await openai.beta.threads.messages.list(
"thread_abc123"
);
for (const msg of messages.data) {
console.log(msg.role, msg.content);
}
curl https://api.openai.com/v1/threads/thread_abc123/messages \\
-H "Authorization: Bearer $OPENAI_API_KEY" \\
-H "Content-Type: application/json" \\
-H "OpenAI-Beta: assistants=v2" \\
-d '{
"role": "user",
"content": "How does AI work? Explain it in simple terms."
}'
{
"id": "msg_abc123",
"object": "thread.message",
"created_at": 1699017614,
"thread_id": "thread_abc123",
"role": "user",
"content": [
{
"type": "text",
"text": {
"value": "How does AI work? Explain it in simple terms.",
"annotations": []
}
}
],
"attachments": [],
"metadata": {}
}