- Categories:
String & binary functions (AI Functions)
THREAD_ MESSAGES (SNOWFLAKE.CORTEX)¶
Returns 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¶
Arguments¶
thread_idThe ID of the thread to retrieve messages from.
page_size(optional)The maximum number of messages to return per page. Use this with
last_message_idfor pagination.last_message_id(optional)The ID of the last message from the previous page. Messages returned will be those after this message ID. Use this with
page_sizefor pagination.
Returns¶
Returns a JSON string containing an array of messages in the thread. Each message includes the role, content, and message metadata.
Access control requirements¶
To retrieve thread messages, you must use a role that can access Cortex Agents. For details, see API access roles.
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: truein the request body. After kicking off a background run, callTHREAD_MESSAGESwith the thread ID to check whether the assistant response has been written. - When paginating, provide both
page_sizeandlast_message_id. If you omit both optional arguments, all messages in the thread are returned.
Examples¶
Retrieve all messages in a thread:
Retrieve messages with pagination:
Use with an asynchronous agent run: