Skip to content
For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
Primary navigation
Theme

Get chat messages

chat.completions.messages.list(completion_id, **kwargs) -> SyncCursorPage[ChatCompletionStoreMessage]
GET
/chat/completions/{completion_id}/messages

Get the messages in a stored chat completion. Only Chat Completions that have been created with the store parameter set to true will be returned.

Parameters
completion_id: str
after: Optional[str]

Identifier for the last message from the previous pagination request.

limit: Optional[int]

Number of messages to retrieve.

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

Sort order for messages by timestamp. Use asc for ascending order or desc for descending order. Defaults to asc.

Returns
class ChatCompletionStoreMessage: …

A chat completion message generated by the model.

id: str

The identifier of the chat message.

content_parts: Optional[List[ChatCompletionStoreMessageContentPart]]

If a content parts array was provided, this is an array of text and image_url parts. Otherwise, null.

Get chat messages

from openai import OpenAI
client = OpenAI()

completions = client.chat.completions.list()
first_id = completions[0].id
first_completion = client.chat.completions.retrieve(completion_id=first_id)
messages = client.chat.completions.messages.list(completion_id=first_id)
print(messages)
Response example
{
  "object": "list",
  "data": [
    {
      "id": "chatcmpl-AyPNinnUqUDYo9SAdA52NobMflmj2-0",
      "role": "user",
      "content": "write a haiku about ai",
      "name": null,
      "content_parts": null
    }
  ],
  "first_id": "chatcmpl-AyPNinnUqUDYo9SAdA52NobMflmj2-0",
  "last_id": "chatcmpl-AyPNinnUqUDYo9SAdA52NobMflmj2-0",
  "has_more": false
}
Returns Examples
Response example
{
  "object": "list",
  "data": [
    {
      "id": "chatcmpl-AyPNinnUqUDYo9SAdA52NobMflmj2-0",
      "role": "user",
      "content": "write a haiku about ai",
      "name": null,
      "content_parts": null
    }
  ],
  "first_id": "chatcmpl-AyPNinnUqUDYo9SAdA52NobMflmj2-0",
  "last_id": "chatcmpl-AyPNinnUqUDYo9SAdA52NobMflmj2-0",
  "has_more": false
}