List messages query parameters - "after is an objectID"?

When using the Node.js SDK to fetch messages for a given run, I’m trying to use the after query parameter to fetch only relevant data:

const message = await openai.beta.threads.messages.list(threadId, {
  after: "msg_id"
})

Where msg_id would be the ID of the last message sent by the User to the thread. Logically this should give me all of the messages that the assistant posted to the thread in response to that user message.

However, in practice this query always seems to return 0 results, whereas when omitting it and just fetching all of the messages in the thread, everything works as expected.

I am slightly confused by this section of the docs:

after is an object ID that defines your place in the list.

What does it mean that it’s an “Object ID” - sounds like something related to MongoDB? I am currently passing in the message ID as just a string , e.g. after: "msg_abc123" - like it is in the Message object. Is that incorrect?

Answer from an experimental Doc-Bot…

The after query parameter is indeed used for pagination. It is an ID that defines your place in the list. When you make a list request and receive a number of objects, the ID of the last object can be used as the after parameter in your next request to fetch the next page of the list.

The term “object ID” here is not related to MongoDB but is a general term used to refer to the unique identifier of an object in a list. In the context of the OpenAI API, an object could be a message, a thread, a run, etc., each with its own unique ID.

In your case, you are passing the message ID as a string, which is the correct format. However, the issue might be with the specific message ID you are using. The after parameter should be the ID of the last object in the previous list you fetched. If you are using the ID of the last message sent by the user, but there are no assistant messages after that user message, then the query would indeed return 0 results.

Here is the relevant extract from the documentation:

after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list.

Source: OpenAI API - Assistants > Methods > listMessageFiles (https://platform.openai.com/docs/api-reference)

2 Likes

Haha, thanks! Is there a way to access this bot on my own? :smiley:

Did you manage to get it working? I am trying to get only the new messages since the last request, but it is returning empty data.

The way we’re doing it now is just fetching the last 5 messages, and filtering those in JS land by the run_id - works well enough.

The after parameter is still a bit of a mystery to me :smiley: