🌍 Global Mirror — Visit original CN site →
Skip to content
Primary navigation

Get chat messages

chat.completions.messages.list(strcompletion_id, MessageListParams**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.

ParametersExpand Collapse
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.

Accepts one of the following:
"asc"
"desc"
ReturnsExpand Collapse
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.

Accepts one of the following:
class ChatCompletionContentPartText:

Learn about text inputs.

text: str

The text content.

type: Literal["text"]

The type of the content part.

class ChatCompletionContentPartImage:

Learn about image inputs.

image_url: ImageURL
url: str

Either a URL of the image or the base64 encoded image data.

formaturi
detail: Optional[Literal["auto", "low", "high"]]

Specifies the detail level of the image. Learn more in the Vision guide.

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

The type of the content part.

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)
{
  "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
{
  "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
}