Skip to main content

Subscribe to Event Stream

Subscribe to session events in real time via SSE

GET
/api/v1/agentstudio/sessions/{session_id}/events/stream
cURL
curl --request GET \
  --url 'https://{workspace_id}.{region}.maas.aliyuncs.com/api/v1/agentstudio/sessions/{session_id}/events/stream' \
  --header 'Authorization: Bearer <YOUR_API_KEY>' \
  --header 'Accept: text/event-stream'
{ "object": "message", "id": "msg_001", "created_at": "2025-10-24T08:15:30.500Z", "role": "assistant", "type": "message", "status": "completed", "content": [ { "type": "text", "text": "开始分析" } ] }

Event types

Values of type in SSE frames:
typeDescription
messageAssistant output message; may be streamed in chunks
reasoningAssistant thinking process; may be streamed in chunks when event_deltas[]=reasoning is enabled
tool_callBuilt-in tool call request
tool_call_outputBuilt-in tool execution result
tool_approval_requestTool approval request; whether a tool requires approval is determined by this event; data carries batch_id, call_id, name, arguments, and tool_type
tool_approval_responseTool approval decision; a bidirectional event. When a plain interrupt is sent under requires_action, the server issues result=deny for each not-yet-executed call in the batch (including decided-but-not-executed ones), accompanied by the corresponding *_call_output (is_error=true); this pairing is for display and audit only and is best-effort — the pending set is still governed by pending_call_ids
function_callSelf-hosted function call request; the client must return function_call_output
function_call_outputServer echo confirming the received function result
mcp_callMCP tool call request
mcp_call_outputMCP tool execution result
session_statusSession state change, including stop_reason
type=error is a separate branch: error details live in the top-level error object of the Message (error.code / error.message), not in content[].data.

is_error

Appears only on tool_call_output / mcp_call_output, written at the top level of the Message (not inside content[].data): true means execution failed, was interrupted, or was denied. data remains ToolCallOutput / McpCallOutput, containing at least call_id and output. Read the top-level is_error for success/failure and data.output for the output.

Delta streaming

By default, each event is delivered as a single complete Message (status: "completed") once it finishes generating. To have text-based events (such as message and reasoning) pushed chunk by chunk as they are generated, enable delta streaming for specific event types with the event_deltas[] query parameter at request time. Repeat the parameter to declare multiple types; event types that are not declared are still delivered as a single complete Message.
curl -N --no-buffer --get \
  'https://{workspace_id}.{region}.maas.aliyuncs.com/api/v1/agentstudio/sessions/'"$SESSION_ID"'/events/stream' \
  -H "Authorization: Bearer $API_KEY" \
  --data-urlencode 'event_deltas[]=message' \
  --data-urlencode 'event_deltas[]=reasoning'
Once enabled, a declared event is pushed as the following three frame kinds in order, which the client merges by event_id:
Frame typeDescription
event_startEvent start frame; event.id is the id of the final complete Message for this event, and event.type is the event type (message / reasoning); this frame carries no content; object is message and status is in_progress
event_deltaDelta frame; event_id matches the event.id from event_start; for message, the newly added chunk is in delta.content; for reasoning, the newly added thinking text is in delta.reasoning; delta.index is the content block index
message / reasoningThe complete Message emitted when the event finishes (object: "message", status: "completed"); content holds the aggregated full content, identical to the non-delta case
Fields of an event_delta frame:
FieldDescription
typeAlways event_delta
objectAlways content
statusAlways in_progress
event_idID of the owning event, equal to event.id from event_start and to the id of the final complete Message; used to merge delta frames into the corresponding event
delta.typecontent_delta for message, reasoning_delta for reasoning
delta.indexIndex of the content block in the content array
delta.contentThe newly added ContentBlock chunk for message, e.g. {"type":"text","text":"…"}
delta.reasoningThe newly added thinking text chunk for reasoning
Example stream (delta enabled for both reasoning and message):
: connected

event: message
data: {"object":"message","status":"completed","id":"sevt_status_running_xxx","type":"session_status","content":[{"type":"data","data":{"session_status":"running"}}]}

event: message
data: {"type":"event_start","event":{"id":"msg_reasoning_xxx","type":"reasoning"},"object":"message","status":"in_progress"}

event: message
data: {"type":"event_delta","event_id":"msg_reasoning_xxx","delta":{"type":"reasoning_delta","index":0,"reasoning":"Split the array into two subarrays, "},"object":"content","status":"in_progress"}

event: message
data: {"type":"event_delta","event_id":"msg_reasoning_xxx","delta":{"type":"reasoning_delta","index":0,"reasoning":"then merge the sorted subarrays."},"object":"content","status":"in_progress"}

event: message
data: {"object":"message","status":"completed","id":"msg_reasoning_xxx","role":"assistant","type":"reasoning","content":[{"type":"text","text":"Split the array into two subarrays, then merge the sorted subarrays."}]}

event: message
data: {"type":"event_start","event":{"id":"msg_answer_xxx","type":"message"},"object":"message","status":"in_progress"}

event: message
data: {"type":"event_delta","event_id":"msg_answer_xxx","delta":{"type":"content_delta","index":0,"content":{"type":"text","text":"Quicksort is a divide-and-conquer algorithm: "}},"object":"content","status":"in_progress"}

event: message
data: {"type":"event_delta","event_id":"msg_answer_xxx","delta":{"type":"content_delta","index":0,"content":{"type":"text","text":"split first, then merge."}},"object":"content","status":"in_progress"}

event: message
data: {"object":"message","status":"completed","id":"msg_answer_xxx","role":"assistant","type":"message","content":[{"type":"text","text":"Quicksort is a divide-and-conquer algorithm: split first, then merge."}]}

event: message
data: {"object":"message","status":"completed","id":"sevt_status_idle_xxx","type":"session_status","content":[{"type":"data","data":{"session_status":"idle","stop_reason":{"type":"end_turn"}}}]}
When reassembling text, accumulate each event_delta's delta.content (message) or delta.reasoning (reasoning) in order by event_id + delta.index; the complete Message emitted at the end carries the aggregated full content and can be used for validation. If a reasoning event has no event_delta, use the complete Message.

Authorizations

string
header
required

DashScope API Key

Header Parameters

enum<string>
required

固定为 text/event-stream

text/event-stream

Path Parameters

string
required

Query Parameters

enum<string>[]

为指定事件类型开启 Delta 增量流,文本内容将在生成过程中逐块推送。可重复传入以声明多个类型(如 message、reasoning);未声明的类型仍以单帧完整 Message 下发。开启后该类型事件先下发 type=event_start 帧(event.id 即最终完整 Message 的 id),随后下发若干 type=event_delta 增量帧(message 的新增片段在 delta.content,reasoning 的新增思考文本在 delta.reasoning,delta.index 为内容块序号),最后下发聚合后的完整 Message;客户端按 event_id + delta.index 顺序累加 delta.content 或 delta.reasoning

Response

200-text/event-stream
普通事件
string

恒为 message

string

事件 ID(前缀 msg_ 或 out_)。客户端用此 ID 在业务层去重

string

ISO 8601 时间戳

enum<string>
user,assistant,tool
enum<string>

服务端推送事件类型,详见事件类型表。type=error 见独立分支

message,reasoning,tool_call,tool_call_output,tool_approval_request,tool_approval_response,function_call,function_call_output,mcp_call,mcp_call_output,session_status
string
object[]

ContentBlock 数组:含发送侧的 text/image/video/file/data;响应侧另可出现 audio(工具输出附件)与 refusal(模型拒绝)

boolean

仅 tool_call_output / mcp_call_output 出现,写在 Message 顶层(不在 content[].data 内):true 表示执行失败/中断/拒绝。读顶层 is_error 判成败,读 data.output 取输出

object

事件附加信息(如 thread_id、call_id 等)