Skip to content

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

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),  # This is the default and can be omitted
)
page = client.chat.completions.messages.list(
    completion_id="completion_id",
)
page = page.data[0]
print(page)
{
  "data": [
    {
      "content": "content",
      "refusal": "refusal",
      "role": "assistant",
      "annotations": [
        {
          "type": "url_citation",
          "url_citation": {
            "end_index": 0,
            "start_index": 0,
            "title": "title",
            "url": "url"
          }
        }
      ],
      "audio": {
        "id": "id",
        "data": "data",
        "expires_at": 0,
        "transcript": "transcript"
      },
      "function_call": {
        "arguments": "arguments",
        "name": "name"
      },
      "tool_calls": [
        {
          "id": "id",
          "function": {
            "arguments": "arguments",
            "name": "name"
          },
          "type": "function"
        }
      ],
      "id": "id",
      "content_parts": [
        {
          "text": "text",
          "type": "text"
        }
      ]
    }
  ],
  "first_id": "first_id",
  "has_more": true,
  "last_id": "last_id",
  "object": "list"
}
Returns Examples
{
  "data": [
    {
      "content": "content",
      "refusal": "refusal",
      "role": "assistant",
      "annotations": [
        {
          "type": "url_citation",
          "url_citation": {
            "end_index": 0,
            "start_index": 0,
            "title": "title",
            "url": "url"
          }
        }
      ],
      "audio": {
        "id": "id",
        "data": "data",
        "expires_at": 0,
        "transcript": "transcript"
      },
      "function_call": {
        "arguments": "arguments",
        "name": "name"
      },
      "tool_calls": [
        {
          "id": "id",
          "function": {
            "arguments": "arguments",
            "name": "name"
          },
          "type": "function"
        }
      ],
      "id": "id",
      "content_parts": [
        {
          "text": "text",
          "type": "text"
        }
      ]
    }
  ],
  "first_id": "first_id",
  "has_more": true,
  "last_id": "last_id",
  "object": "list"
}