Implementation:Openai Openai node Chat Completions Messages
| Knowledge Sources | |
|---|---|
| Domains | SDK, Chat, Stored_Completions |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Messages resource class provides the list method for retrieving messages from stored chat completions.
Description
The Messages class extends APIResource and exposes a single list method that retrieves the messages associated with a stored chat completion. It sends a GET request to /chat/completions/{completionID}/messages and returns a paginated list of ChatCompletionStoreMessage objects using cursor-based pagination via CursorPage.
This resource only works with chat completions that were created with the store: true parameter. The MessageListParams interface extends CursorPageParams and adds an optional order parameter that controls the sort direction by timestamp (asc or desc, defaulting to asc).
The paginated response supports automatic iteration using for await...of, which fetches additional pages as needed without manual pagination handling.
Usage
Use the Messages resource when you need to retrieve the message history of a previously stored chat completion. Access it via client.chat.completions.messages.list(completionID). This is useful for auditing, displaying conversation history, or building upon previous completions.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/chat/completions/messages.ts
Signature
export class Messages extends APIResource {
list(
completionID: string,
query?: MessageListParams | null | undefined,
options?: RequestOptions,
): PagePromise<ChatCompletionStoreMessagesPage, ChatCompletionStoreMessage>;
}
export interface MessageListParams extends CursorPageParams {
order?: 'asc' | 'desc';
}
Import
import OpenAI from 'openai';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| completionID | string |
Yes | The ID of the stored chat completion to retrieve messages for |
| order | 'desc' | No | Sort order for messages by timestamp (default: asc)
|
| after | string |
No | Cursor for pagination (inherited from CursorPageParams)
|
| limit | number |
No | Number of results per page (inherited from CursorPageParams)
|
Outputs
| Name | Type | Description |
|---|---|---|
| PagePromise | PagePromise<ChatCompletionStoreMessagesPage, ChatCompletionStoreMessage> |
A paginated async iterable of stored chat completion messages |
| ChatCompletionStoreMessage | ChatCompletionStoreMessage |
Individual message from the stored completion |
Usage Examples
import OpenAI from 'openai';
const client = new OpenAI();
// First, create a stored completion
const completion = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }],
store: true,
});
// Then retrieve the messages from the stored completion
for await (const message of client.chat.completions.messages.list(
completion.id,
)) {
console.log(message);
}
// With pagination options
for await (const message of client.chat.completions.messages.list(
completion.id,
{ order: 'desc', limit: 10 },
)) {
console.log(message);
}