Skip to content
Primary navigation

Get chat messages

chat.completions.messages.list(completion_id, **kwargs) -> CursorPage<ChatCompletionStoreMessage { id, content_parts } >
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: String
after: String

Identifier for the last message from the previous pagination request.

limit: Integer

Number of messages to retrieve.

order: :asc | :desc

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

One of the following:
:asc
:desc
ReturnsExpand Collapse
class ChatCompletionStoreMessage { id, content_parts }

A chat completion message generated by the model.

id: String

The identifier of the chat message.

content_parts: Array[ChatCompletionContentPartText { text, type } | ChatCompletionContentPartImage { image_url, type } ]

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

One of the following:
class ChatCompletionContentPartText { text, type }

Learn about text inputs.

text: String

The text content.

type: :text

The type of the content part.

class ChatCompletionContentPartImage { image_url, type }

Learn about image inputs.

image_url: { url, detail}
url: String

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

formaturi
detail: :auto | :low | :high

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

One of the following:
:auto
:low
:high
type: :image_url

The type of the content part.

Get chat messages

require "openai"

openai = OpenAI::Client.new(api_key: "My API Key")

page = openai.chat.completions.messages.list("completion_id")

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