跳转到内容
快速开始

Agent API

流式传输

Agent API 的 response.* SSE 事件,含 mirobody_tool_call 旁路事件。

POST /v1/responses 上设 stream: true,回复将以标准 OpenAI Responses response.* SSE 事件到达:每帧都是 event: <type> + data: <json>,并带单调递增的 sequence_number。官方 SDK 的流式循环可原样消费。

from openai import OpenAI
client = OpenAI(api_key="mb_live_...", base_url="https://api.mirobody.ai/v1")
with client.responses.stream(
model="mirobody-flash",
input="How is my fasting glucose trending?",
user="alice",
) as stream:
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
事件含义
response.created运行开始,携带 status: "in_progress" 的响应快照。
response.in_progress紧随其后(对齐 OpenAI)。
response.output_item.added新 output item 开启(reasoning / message / function_call),携带 output_index 与进行中的 item。
response.reasoning_summary_part.added / .done一个推理摘要片段开启 / 结束。
response.reasoning_summary_text.delta / .doneProvider 返回的推理摘要增量,随后是完整摘要。
response.content_part.added / .done一个回答内容片段开启 / 结束。
response.output_text.delta / .done回答文本增量,随后是全文,是大多数客户端渲染的通道
response.function_call_arguments.delta / .done客户端工具交接的参数(见 Function calling)。
response.output_item.done当前 item 完成,携带完成后的 item。
response.mirobody_tool_callMirobody 扩展,旁路通道:一个内置服务端工具被调用。见下文。
response.mirobody_tool_resultMirobody 扩展,旁路通道:一个内置服务端工具的结果落地。见下文。
response.completed终态:携带完整的最终响应对象outputusagetool_stepshealth_recordscitations)。
response.failed终态错误:携带 status: "failed" 的响应快照与 error 对象。

item 严格一次一个地流出:先 reasoning(当档位产出时),然后 message,最后是任何 function_call 交接;output_index 按 item 递增,且与 response.completed 里最终 output 数组的索引一致。

内置服务端工具(数据检索、文献等)不是 output item,官方 SDK 会错误解析未知 item 类型,因此其轨迹走一个标准流式循环会安全忽略的专用事件:

event: response.mirobody_tool_call
data: {"type":"response.mirobody_tool_call","sequence_number":7,
"tool_step":{"id":"mtc_0","call_id":"call_1a2b...","name":"query_health_data",
"arguments":"{\"query\": \"fasting glucose last 90 days\"}"}}

它在工具被调用时触发;不会开启 output item,也不推进 output_index。想做活动信息流(「正在检索你的记录…」)就渲染它;跳过它也不影响任何功能。

response.mirobody_tool_result 是它的另一半,在该工具的结果落地时推送,同属旁路通道、同样可安全忽略:

event: response.mirobody_tool_result
data: {"type":"response.mirobody_tool_result","sequence_number":9,
"tool_step":{"id":"mtc_0","call_id":"call_1a2b...","name":"query_health_data",
"result":"...","truncated":false}}

id / call_id 把结果匹配到它的调用。流式的 result 截断到 4096 字符(被截断时 truncated: true);完整值始终在最终响应对象(response.completed)的 tool_steps[].result 上。

上游出错时,流以 response.failed 结束(而非断管):

event: response.failed
data: {"type":"response.failed","sequence_number":12,
"response":{"id":"resp_...","status":"failed",
"error":{"type":"upstream_error","message":"..."}}}

Answers API 更简单的 chat.completion.chunk 流式见 Answers API → 流式传输