Skip to main content
The genai_get_role function retrieves the role of a message at a specific position in a GenAI messages array. This allows you to understand who sent a particular message in the conversation (user, assistant, system, tool, etc.). You can use this function to validate conversation structure, analyze message patterns, verify conversation flow, or process conversations based on role sequences.

For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.
In Splunk SPL, you would use mvindex to access the role field at a specific position.
In ANSI SQL, you would unnest the array and access the role at a specific offset.

Usage

Syntax

Parameters

Returns

Returns a string containing the role of the message at the specified index (such as ‘user’, ‘assistant’, ‘system’, ‘tool’, ‘function’), or an empty string if the index is out of bounds.

Example

Get the role of the first message in a GenAI conversation. Query
Run in Playground Output This query verifies that most conversations are properly initialized with system prompts.
  • genai_get_content_by_index: Gets content at a specific index. Combine with genai_get_role to understand both role and content at positions.
  • genai_message_roles: Lists all roles in the conversation. Use this to get a complete picture of all roles rather than checking individual positions.
  • genai_get_content_by_role: Gets content filtered by role. Use this when you need content from a specific role type.
  • array_length: Returns the total number of messages. Use this to validate index bounds before accessing positions.