Skip to content
Primary navigation

List messages

Deprecated
beta.threads.messages.list(strthread_id, MessageListParams**kwargs) -> SyncCursorPage[Message]
GET/threads/{thread_id}/messages

Returns a list of messages for a given thread.

ParametersExpand Collapse
thread_id: str
after: Optional[str]

A cursor for use in pagination. after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.

before: Optional[str]

A cursor for use in pagination. before is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, starting with obj_foo, your subsequent call can include before=obj_foo in order to fetch the previous page of the list.

limit: Optional[int]

A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.

order: Optional[Literal["asc", "desc"]]

Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.

Accepts one of the following:
"asc"
"desc"
run_id: Optional[str]

Filter messages by the run ID that generated them.

ReturnsExpand Collapse
class Message: …

Represents a message within a thread.

id: str

The identifier, which can be referenced in API endpoints.

assistant_id: Optional[str]

If applicable, the ID of the assistant that authored this message.

attachments: Optional[List[Attachment]]

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

file_id: Optional[str]

The ID of the file to attach to the message.

tools: Optional[List[AttachmentTool]]

The tools to add this file to.

Accepts one of the following:
class CodeInterpreterTool: …
type: Literal["code_interpreter"]

The type of tool being defined: code_interpreter

class AttachmentToolAssistantToolsFileSearchTypeOnly: …
type: Literal["file_search"]

The type of tool being defined: file_search

completed_at: Optional[int]

The Unix timestamp (in seconds) for when the message was completed.

content: List[MessageContent]

The content of the message in array of text and/or images.

Accepts one of the following:
class ImageFileContentBlock: …

References an image File in the content of a message.

image_file: ImageFile
file_id: str

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: Optional[Literal["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.

Accepts one of the following:
"auto"
"low"
"high"
type: Literal["image_file"]

Always image_file.

class ImageURLContentBlock: …

References an image URL in the content of a message.

image_url: ImageURL
url: str

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

formaturi
detail: Optional[Literal["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

Accepts one of the following:
"auto"
"low"
"high"
type: Literal["image_url"]

The type of the content part.

class TextContentBlock: …

The text content that is part of a message.

text: Text
annotations: List[Annotation]
Accepts one of the following:
class FileCitationAnnotation: …

A citation within the message that points to a specific quote from a specific File associated with the assistant or the message. Generated when the assistant uses the "file_search" tool to search files.

end_index: int
minimum0
file_citation: FileCitation
file_id: str

The ID of the specific File the citation is from.

start_index: int
minimum0
text: str

The text in the message content that needs to be replaced.

type: Literal["file_citation"]

Always file_citation.

class FilePathAnnotation: …

A URL for the file that's generated when the assistant used the code_interpreter tool to generate a file.

end_index: int
minimum0
file_path: FilePath
file_id: str

The ID of the file that was generated.

start_index: int
minimum0
text: str

The text in the message content that needs to be replaced.

type: Literal["file_path"]

Always file_path.

value: str

The data that makes up the text.

type: Literal["text"]

Always text.

class RefusalContentBlock: …

The refusal content generated by the assistant.

refusal: str
type: Literal["refusal"]

Always refusal.

created_at: int

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

incomplete_at: Optional[int]

The Unix timestamp (in seconds) for when the message was marked as incomplete.

incomplete_details: Optional[IncompleteDetails]

On an incomplete message, details about why the message is incomplete.

reason: Literal["content_filter", "max_tokens", "run_cancelled", 2 more]

The reason the message is incomplete.

Accepts one of the following:
"content_filter"
"max_tokens"
"run_cancelled"
"run_expired"
"run_failed"
metadata: Optional[Metadata]

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: Literal["thread.message"]

The object type, which is always thread.message.

role: Literal["user", "assistant"]

The entity that produced the message. One of user or assistant.

Accepts one of the following:
"user"
"assistant"
run_id: Optional[str]

The ID of the run associated with the creation of this message. Value is null when messages are created manually using the create message or create thread endpoints.

status: Literal["in_progress", "incomplete", "completed"]

The status of the message, which can be either in_progress, incomplete, or completed.

Accepts one of the following:
"in_progress"
"incomplete"
"completed"
thread_id: str

The thread ID that this message belongs to.

List messages

from openai import OpenAI
client = OpenAI()

thread_messages = client.beta.threads.messages.list("thread_abc123")
print(thread_messages.data)
{
  "object": "list",
  "data": [
    {
      "id": "msg_abc123",
      "object": "thread.message",
      "created_at": 1699016383,
      "assistant_id": null,
      "thread_id": "thread_abc123",
      "run_id": null,
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "How does AI work? Explain it in simple terms.",
            "annotations": []
          }
        }
      ],
      "attachments": [],
      "metadata": {}
    },
    {
      "id": "msg_abc456",
      "object": "thread.message",
      "created_at": 1699016383,
      "assistant_id": null,
      "thread_id": "thread_abc123",
      "run_id": null,
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "Hello, what is AI?",
            "annotations": []
          }
        }
      ],
      "attachments": [],
      "metadata": {}
    }
  ],
  "first_id": "msg_abc123",
  "last_id": "msg_abc456",
  "has_more": false
}
Returns Examples
{
  "object": "list",
  "data": [
    {
      "id": "msg_abc123",
      "object": "thread.message",
      "created_at": 1699016383,
      "assistant_id": null,
      "thread_id": "thread_abc123",
      "run_id": null,
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "How does AI work? Explain it in simple terms.",
            "annotations": []
          }
        }
      ],
      "attachments": [],
      "metadata": {}
    },
    {
      "id": "msg_abc456",
      "object": "thread.message",
      "created_at": 1699016383,
      "assistant_id": null,
      "thread_id": "thread_abc123",
      "run_id": null,
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "Hello, what is AI?",
            "annotations": []
          }
        }
      ],
      "attachments": [],
      "metadata": {}
    }
  ],
  "first_id": "msg_abc123",
  "last_id": "msg_abc456",
  "has_more": false
}