Core Response Classes¶
AgentResponse¶
cortex_agents.core.response.AgentResponse ¶
Bases: BaseSSEResponse
Wrapper for agent run response with easy access to results.
Inherits SSE parsing, iteration, and request_id capture from
:class:BaseSSEResponse. Adds Agent-specific convenience properties
for text, SQL, thinking, charts, tables, tools, etc.
Examples:
with CortexAgent(...) as agent:
response = agent.run(
"What's the revenue?",
agent_name="MY_AGENT",
database="MY_DB",
schema="MY_SCHEMA"
)
# Stream events in real-time (response is directly iterable)
for event in response:
if event["type"] == "text.delta":
print(event["data"]["text"], end="", flush=True)
# Or just get final results
print(response.text) # Final text response
print(response.sql) # SQL query if any
print(response.thinking) # Agent reasoning
print(response.query_id) # Snowflake query ID
# Get SQL execution results
result_set = response.get_sql_result()
if result_set:
for row in result_set['data']:
print(row)
# Get structured data
charts = response.get_charts()
tables = response.get_tables()
Initialize response wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_response
|
Any
|
Callable that returns httpx streaming context manager, or a dict for non-streaming responses. |
required |
stream
|
bool
|
Whether response is streaming. Defaults to True. |
True
|
is_async
|
bool
|
Whether this is an async response. Defaults to False. |
False
|
Source code in cortex_agents/core/response.py
text
property
¶
Get final text response (concatenated from all text events by content_index).
Real SSE streams may emit multiple response.text events at different
content_index values (e.g., answer, explanation, methodology).
This property concatenates ALL text blocks in content_index order.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Complete text response |
thinking
property
¶
Get agent's reasoning/thinking tokens (concatenated from all thinking blocks).
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Complete thinking process |
sql
property
¶
Get SQL query from Cortex Analyst tool (if any).
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: SQL query or None |
sql_explanation
property
¶
Get SQL explanation from Cortex Analyst (if any).
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: SQL explanation or None |
query_id
property
¶
Get Snowflake query ID from SQL execution (if any).
Useful for tracking query execution in Snowflake.
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: Query ID or None |
message_id
property
¶
Get the last assistant's message ID (for thread continuity).
Returns:
| Type | Description |
|---|---|
str | None
|
Message ID as provided by the API (typically an integer) or None if not available. |
run_id
property
¶
Get the run ID from metadata events.
The run_id is typically found in the metadata of the final response
or in response.metadata events.
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: The run ID or None |
is_elicitation
property
¶
Check if the response is an elicitation request (asking for more info).
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if any text event has |
request_id
property
¶
Snowflake request ID captured from response headers or event data.
events
property
¶
All accumulated events (triggers full parse if needed).
get_sql_result ¶
Get SQL execution result set from Cortex Analyst (if any).
Returns the full result_set including data, metadata, and statementHandle.
Returns:
| Type | Description |
|---|---|
dict | None
|
Optional[Dict]: Result set or None |
Examples:
result = response.get_sql_result()
if result:
data = result['data'] # Array of rows
metadata = result['resultSetMetaData']
print(f"Returned {metadata['numRows']} rows")
for row in data:
print(row)
Source code in cortex_agents/core/response.py
get_charts ¶
Get all chart specifications (Vega-Lite).
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[Dict]: List of chart specs |
Source code in cortex_agents/core/response.py
get_tables ¶
Get all data tables.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[Dict]: List of tables with result_set data |
Source code in cortex_agents/core/response.py
get_tool_uses ¶
Get all tool invocations.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[Dict]: List of tool use events |
Source code in cortex_agents/core/response.py
get_tool_results ¶
Get all tool execution results.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[Dict]: List of tool results |
Source code in cortex_agents/core/response.py
get_metadata ¶
Get message metadata (includes message_id for follow-ups).
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[dict]: List of metadata dicts with role and message_id |
Source code in cortex_agents/core/response.py
get_final_response ¶
Get the final aggregated response event.
Returns:
| Type | Description |
|---|---|
dict | None
|
Optional[Dict]: Complete response or None |
Source code in cortex_agents/core/response.py
get_token_usage ¶
Get token usage information from the response metadata.
Returns:
| Type | Description |
|---|---|
dict | None
|
Optional[Dict]: Token usage dict (e.g. |
Source code in cortex_agents/core/response.py
get_warnings ¶
Get all warning messages from the response.
Warnings are emitted as response.warning SSE events.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of warning message strings |
Source code in cortex_agents/core/response.py
get_suggested_queries ¶
Get suggested follow-up queries from the response.
These are emitted as response.suggested_queries SSE events.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: List of suggested query strings |
Source code in cortex_agents/core/response.py
get_annotations ¶
Get all annotations from text events.
Annotations (e.g., citations, references) may appear in
response.text or response.text.annotation events.
Returns:
| Type | Description |
|---|---|
list[dict]
|
list[Dict]: List of annotation dicts |
Source code in cortex_agents/core/response.py
stream ¶
astream
async
¶
EventType¶
cortex_agents.core.response.EventType ¶
Bases: str, Enum
Enumeration of SSE event types from agent:run endpoint.
SSE (Server-Sent Events) events include various message types for streaming agent responses including text deltas, tool usage, metadata, and errors.
AnalystResponse¶
cortex_agents._analyst_response.AnalystResponse ¶
AnalystResponse(response: Any, stream: bool = True, is_async: bool = False, request_messages: list[dict[str, Any]] | None = None)
Bases: BaseSSEResponse
Wrapper for Cortex Analyst responses with convenient property access.
Provides easy access to generated SQL statements, interpretation text, suggestions for ambiguous questions, confidence information, and streaming events.
Examples:
response = analyst.message(
"What is the revenue?",
semantic_model_file="@stage/model.yaml"
)
# Stream events in real-time (response is directly iterable)
for event in response:
if event["type"] == "text.delta":
print(event["data"]["text"], end="")
# Or access final properties
print(response.text) # Interpretation
print(response.sql) # Generated SQL
print(response.suggestions) # If ambiguous
Initialize the response wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Any
|
Callable that returns httpx streaming context manager, or a dict for non-streaming responses. |
required |
stream
|
bool
|
Whether this is a streaming response. Defaults to True. |
True
|
is_async
|
bool
|
Whether this is an async response. Defaults to False. |
False
|
request_messages
|
list[dict[str, Any]] | None
|
Original request messages for conversation continuity. |
None
|
Source code in cortex_agents/_analyst_response.py
verified_query_used
property
¶
Get verified query information if one was used.
request_id
property
¶
Get the Snowflake request ID for this response.
Checks HTTP response headers first (via BaseSSEResponse), then falls back to the parsed response data.
response_metadata
property
¶
Get response metadata (models used, question category, etc.).
conversation_messages
property
¶
sql_explanation
property
¶
Get Analyst's explanation of the generated SQL.
semantic_model_selection
property
¶
Get which semantic model was selected (when multiple provided).
cortex_search_retrieval
property
¶
Get Cortex Search retrieval results.
analyst_latency_ms
property
¶
Get analyst processing latency in milliseconds.
question_category
property
¶
Get the question category determined by the analyst.
analyst_orchestration_path
property
¶
Get the orchestration path taken during analysis.
events
property
¶
All accumulated events (triggers full parse if needed).