THREAD_MESSAGES
functiondeferredAI & MLReturns the messages in a Cortex Agent thread as JSON. You can use this function to retrieve all messages in a thread after an agent run completes. This is particularly useful for polling the results of an asynchronous (background) agent run started with background: true. Note SNOWFLAKE.CORTEX.THREAD_MESSAGES is a utility wrapper around the Cortex Agents Threads REST API.
Syntax
supportedpartialnot supportednot scored
SNOWFLAKE.CORTEX.THREAD_MESSAGES( <thread_id> [, <page_size>, <last_message_id> ] )
Parameters
| Name | Required | Description |
|---|---|---|
thread_id | required | The ID of the thread to retrieve messages from. |
page_size (optional) | required | The maximum number of messages to return per page. Use this with last_message_id for pagination. |
last_message_id (optional) | required | The ID of the last message from the previous page. Messages returned will be those after this message ID. Use this with page_size for pagination. |
Returns
Returns a JSON string containing an array of messages in the thread. Each message includes the role, content, and message metadata.
Usage notes
- The function returns a JSON string. Pass this string to TRY_PARSE_JSON to convert the response to a VARIANT value.
- This function is commonly used to poll the results of an asynchronous agent run initiated with background: true in the request body. After kicking off a background run, call THREAD_MESSAGES with the thread ID to check whether the assistant response has been written.
- When paginating, provide both page_size and last_message_id. If you omit both optional arguments, all messages in the thread are returned.
Access control requirements
To retrieve thread messages, you must use a role that can access Cortex Agents. For details, see API access roles.
Known gaps
- The emulator does not run Cortex Agents or keep their conversation threads, so there is no agent response or thread to return.