跳到主要内容

流式响应

当您创建响应并将 stream 设置为 true 时,服务器会在生成 Response 的过程中,通过 Server-Sent Events(SSE)实时向客户端推送事件。本文介绍服务器会推送的各类事件类型及其参数说明。

事件类型概览​

流式响应包含以下主要事件类型:

  • 响应生命周期事件:response.created、response.in_progress、response.completed、response.failed、response.incomplete
  • 输出项事件:response.output_item.added、response.output_item.done
  • 内容部分事件:response.content_part.added、response.content_part.done
  • 文本增量事件:response.output_text.delta、response.output_text.done
  • 函数调用事件:response.function_call_arguments.delta、response.function_call_arguments.done
  • 思维链事件:response.reasoning_summary_part.added、response.reasoning_summary_part.done、response.reasoning_summary_text.delta、response.reasoning_summary_text.done
  • 错误事件:error

响应生命周期事件​

response.created​

当响应被创建时触发的事件。

字段类型说明
typestring事件的类型,固定为 response.created。
responseobject创建状态的响应。包含参数与创建模型请求时,非流式调用返回的参数一致。详见 Response 对象。
sequence_numberinteger事件的序列号。

示例:

JSON
{
"type": "response.created",
"sequence_number": 0,
"response": {
"id": "resp_123456",
"object": "response",
"status": "in_progress",
"created_at": 1672531200
}
}

response.in_progress​

当响应正在进行中时触发的事件。

字段类型说明
typestring事件的类型,固定为 response.in_progress。
responseobject进行中状态的响应。包含参数与创建模型请求时,非流式调用返回的参数一致。
sequence_numberinteger事件的序列号。

response.completed​

当响应已完成时触发的事件。

字段类型说明
typestring事件的类型,固定为 response.completed。
responseobject已完成状态的响应。包含参数与创建模型请求时,非流式调用返回的参数一致。
sequence_numberinteger事件的序列号。

response.failed​

当响应失败时触发的事件。

字段类型说明
typestring事件的类型,固定为 response.failed。
responseobject失败状态的响应。包含参数与创建模型请求时,非流式调用返回的参数一致。
sequence_numberinteger事件的序列号。

response.incomplete​

当响应以未完成状态结束时触发的事件。

字段类型说明
typestring事件的类型,固定为 response.incomplete。
responseobject未完成状态的响应。包含参数与创建模型请求时,非流式调用返回的参数一致。
sequence_numberinteger事件的序列号。

输出项事件​

response.output_item.added​

表示添加了新的输出项。

字段类型说明
typestring事件的类型,固定为 response.output_item.added。
itemobject模型输出内容。详见下方 item 参数说明。
output_indexinteger被添加的输出项的索引。
sequence_numberinteger事件的序列号。

item 参数说明:

文本输出(message)​

字段类型说明
typestring输出消息的类型,固定为 message。
idstring输出消息请求的唯一标识。
rolestring输出信息的角色,固定为 assistant。
statusstring输出消息的状态。
contentarray输出消息的内容。包含 type(固定为 output_text)和 text(模型的文本输出)字段。

思维链输出(reasoning)​

字段类型说明
typestring对象的类型,固定为 reasoning。
idstring请求的唯一标识。
statusstring该内容项的状态。
summaryarray推理文本内容。包含 type(固定为 summary_text)和 text(模型生成答复时的推理内容)字段。

工具调用输出(function_call)​

字段类型说明
typestring工具调用的类型,固定为 function_call。
idstring工具调用请求的唯一标识。
call_idstring模型生成的函数工具调用的唯一 ID。
namestring要运行的函数的名称。
argumentsstring要传递给函数的参数的 JSON 字符串。
statusstring该项的状态。

示例:

JSON
{
"type": "response.output_item.added",
"sequence_number": 1,
"output_index": 0,
"item": {
"id": "item_001",
"type": "message",
"role": "assistant",
"status": "in_progress",
"content": []
}
}

response.output_item.done​

表示输出项已完成。

字段类型说明
typestring事件的类型,固定为 response.output_item.done。
itemobject已完成的输出项。结构与 response.output_item.added 中的 item 相同。
output_indexinteger已完成的输出项的索引。
sequence_numberinteger事件的序列号。

内容部分事件​

response.content_part.added​

当有新的内容部分被添加时触发。

字段类型说明
typestring事件的类型,固定为 response.content_part.added。
partobject所添加的内容部分。包含 type(固定为 output_text)和 text(模型输出的文本内容)字段。
item_idstring内容部分所添加的输出项的 ID。
output_indexinteger内容部分所添加的输出项的索引。
content_indexinteger内容部分的索引。
sequence_numberinteger事件的序列号。

示例:

JSON
{
"type": "response.content_part.added",
"sequence_number": 2,
"item_id": "item_001",
"output_index": 0,
"content_index": 0,
"part": {
"type": "output_text",
"text": ""
}
}

response.content_part.done​

当内容部分完成时触发。

字段类型说明
typestring事件的类型,固定为 response.content_part.done。
partobject所完成的内容部分。包含 type(固定为 output_text)和 text(模型输出的文本内容)字段。
item_idstring内容部分所添加的输出项的 ID。
output_indexinteger内容部分所添加的输出项的索引。
content_indexinteger内容部分的索引。
sequence_numberinteger事件的序列号。

文本增量事件​

response.output_text.delta​

当有新增文本片段时触发。

字段类型说明
typestring事件的类型,固定为 response.output_text.delta。
deltastring新增的文本片段内容。
item_idstring增量文本所属输出项的唯一 ID。
output_indexinteger增量文本所属输出项的列表索引。
content_indexinteger增量文本所属内容块的索引。
sequence_numberinteger事件的序列号。

示例:

JSON
{
"type": "response.output_text.delta",
"sequence_number": 3,
"item_id": "item_001",
"output_index": 0,
"content_index": 0,
"delta": "融云"
}

response.output_text.done​

文本内容完成时触发。

字段类型说明
typestring事件的类型,固定为 response.output_text.done。
textstring完成的文本内容。
item_idstring文本内容所属输出项的唯一 ID。
output_indexinteger文本内容所属输出项的列表索引。
content_indexinteger文本内容所属内容块的索引。
sequence_numberinteger事件的序列号。

函数调用事件​

response.function_call_arguments.delta​

存在函数调用参数片段时触发。

字段类型说明
typestring事件的类型,固定为 response.function_call_arguments.delta。
deltastring本次新增的函数调用参数增量片段。
item_idstring所属输出项的唯一 ID。
output_indexinteger所属输出项的列表索引。
sequence_numberinteger事件的序列号。

示例:

JSON
{
"type": "response.function_call_arguments.delta",
"sequence_number": 10,
"item_id": "item_002",
"output_index": 1,
"delta": "{\"city\":"
}

response.function_call_arguments.done​

当函数调用参数完成时触发。

字段类型说明
typestring事件的类型,固定为 response.function_call_arguments.done。
argumentsstring函数调用的完整参数。
item_idstring所属输出项的唯一 ID。
output_indexinteger所属输出项的列表索引。
sequence_numberinteger事件的序列号。

思维链事件​

response.reasoning_summary_part.added​

当存在思维链新增部分时触发。

字段类型说明
typestring事件的类型,固定为 response.reasoning_summary_part.added。
partobject所添加的内容部分。包含 type(固定为 summary_text)和 text(输出的思维链文本)字段。
item_idstring所属输出项的 ID。
output_indexinteger所属输出项的索引。
summary_indexinteger输出项内,推理总结部分的子索引(若有多个总结)。
sequence_numberinteger事件的序列号。

示例:

JSON
{
"type": "response.reasoning_summary_part.added",
"sequence_number": 5,
"item_id": "item_003",
"output_index": 0,
"summary_index": 0,
"part": {
"type": "summary_text",
"text": ""
}
}

response.reasoning_summary_part.done​

当思维链部分完成时触发。

字段类型说明
typestring事件的类型,固定为 response.reasoning_summary_part.done。
partobject所完成的内容部分。包含 type(固定为 summary_text)和 text(输出的思维链文本)字段。
item_idstring所属输出项的 ID。
output_indexinteger所属输出项的索引。
summary_indexinteger输出项内,推理总结部分的子索引(若有多个总结)。
sequence_numberinteger事件的序列号。

response.reasoning_summary_text.delta​

当存在思维链新增文本时触发。

字段类型说明
typestring事件的类型,固定为 response.reasoning_summary_text.delta。
deltastring输出的思维链文本增量片段。
item_idstring所属输出项的 ID。
output_indexinteger所属输出项的索引。
summary_indexinteger输出项内,推理总结部分的子索引(若有多个总结)。
sequence_numberinteger事件的序列号。

response.reasoning_summary_text.done​

思维链文本完成时触发。

字段类型说明
typestring事件的类型,固定为 response.reasoning_summary_text.done。
textstring思维链文本完整内容。
item_idstring所属输出项的 ID。
output_indexinteger所属输出项的索引。
summary_indexinteger输出项内,推理总结部分的子索引(若有多个总结)。
sequence_numberinteger事件的序列号。

错误事件​

error​

发生错误时触发。

字段类型说明
typestring事件的类型,固定为 error。
codestring/null错误码。
messagestring错误原因描述。
paramstring/null引发错误的参数名称。
sequence_numberinteger事件的序列号。

示例:

JSON
{
"type": "error",
"sequence_number": 15,
"code": "rate_limit_exceeded",
"message": "请求频率超过限制,请稍后重试",
"param": null
}

流式响应示例​

完整的流式响应示例:

data: {"type":"response.created","sequence_number":0,"response":{"id":"resp_123456","object":"response","status":"in_progress","created_at":1672531200}}

data: {"type":"response.output_item.added","sequence_number":1,"output_index":0,"item":{"id":"item_001","type":"message","role":"assistant","status":"in_progress","content":[]}}

data: {"type":"response.content_part.added","sequence_number":2,"item_id":"item_001","output_index":0,"content_index":0,"part":{"type":"output_text","text":""}}

data: {"type":"response.output_text.delta","sequence_number":3,"item_id":"item_001","output_index":0,"content_index":0,"delta":"融云"}

data: {"type":"response.output_text.delta","sequence_number":4,"item_id":"item_001","output_index":0,"content_index":0,"delta":" AI API "}

data: {"type":"response.output_text.delta","sequence_number":5,"item_id":"item_001","output_index":0,"content_index":0,"delta":"服务..."}

data: {"type":"response.output_text.done","sequence_number":6,"item_id":"item_001","output_index":0,"content_index":0,"text":"融云 AI API 服务是一个强大的人工智能接口平台..."}

data: {"type":"response.content_part.done","sequence_number":7,"item_id":"item_001","output_index":0,"content_index":0,"part":{"type":"output_text","text":"融云 AI API 服务是一个强大的人工智能接口平台..."}}

data: {"type":"response.output_item.done","sequence_number":8,"output_index":0,"item":{"id":"item_001","type":"message","role":"assistant","status":"completed","content":[{"type":"output_text","text":"融云 AI API 服务是一个强大的人工智能接口平台..."}]}}

data: {"type":"response.completed","sequence_number":9,"response":{"id":"resp_123456","object":"response","status":"completed","created_at":1672531200,"output":[...],"usage":{...}}}

data: [DONE]