Skip to content

fix(cortex_agent): fix SSE response parsing for Cortex Agent API#175

Open
shibinmatrix wants to merge 1 commit intoSnowflake-Labs:mainfrom
shibinmatrix:fix/cortex-agent-sse-parsing
Open

fix(cortex_agent): fix SSE response parsing for Cortex Agent API#175
shibinmatrix wants to merge 1 commit intoSnowflake-Labs:mainfrom
shibinmatrix:fix/cortex-agent-sse-parsing

Conversation

@shibinmatrix
Copy link

Summary

  • Remove stream: False from the Cortex Agent request payload. This parameter caused Snowflake to return plain JSON instead of SSE, but parse_agent_response() expects SSE format with event: response markers. Without streaming, the parser found zero events and always returned "No final response found.".
  • Fix content[-1] assumption in parse_agent_response(). The response content array contains multiple types (thinking, tool_use, tool_result, text, suggested_queries). The last item is suggested_queries, not text. The fix iterates the array and explicitly finds the item with type == "text".

Root Cause

The cortex_agent tool always returned {"results": "No final response found."} due to two compounding bugs:

  1. tools.py:79"stream": False was included with the comment "Ignored by Agent API", but it is not ignored. Snowflake returns Content-Type: application/json (a single JSON blob) instead of text/event-stream (SSE). The parser then iterates over JSON lines looking for event: response and never finds it.

  2. utils.py:312-316 — Even when streaming works correctly, content[-1].get("text") grabs the wrong item. The suggested_queries content type appears after text in the array, so the last element has no text field and falls back to the default "No final response found.".

Test plan

  • Verified standalone REST call to Cortex Agent with streaming enabled returns valid SSE with event: response
  • Verified the event: response data payload contains a content array where the text item is NOT the last element
  • Verified the fix end-to-end: cortex_agent MCP tool now returns actual agent responses
  • Verified requests appear in Snowflake Cortex Agent Activity monitoring page

Two bugs prevented the cortex_agent tool from returning agent responses:

1. `stream: False` in the request payload caused Snowflake to return a
   plain JSON response instead of SSE. The `parse_agent_response()`
   parser expects SSE format with `event: response` markers, so it
   found no events and returned "No final response found." Removing
   `stream: False` lets the API default to streaming (SSE), which the
   parser handles correctly.

2. The SSE parser assumed the text response was the last item in the
   `content` array (`content[-1]`). In practice, the array contains
   multiple content types (thinking, tool_use, tool_result, text,
   suggested_queries) and the last item is `suggested_queries`, not
   `text`. The fix iterates the content array and finds the item with
   `type == "text"` explicitly.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant