Gemini API 官方中文文档
流式传输
官方来源:https://ai.google.dev/gemini-api/docs/streaming?hl=zh-cn
来源:Google AI for Developers Gemini API 文档。除非另有说明,页面内容采用 CC BY 4.0,代码示例采用 Apache 2.0。
整理时间:2026-07-01 18:50
· Gemini API
流式传输互动
创建 Interaction 时,您可以将 stream: true 设置为使用 服务器发送的事件 (SSE) 逐步流式传输回答。
Python
  1. from google import genai

  2. client = genai.Client()

  3. stream = client.interactions.create(
  4. model="gemini-3-flash-preview",
  5. input="Count to from 1 to 25.",
  6. stream=True,
  7. )
  8. for event in stream:
  9. if event.event_type == "step.delta":
  10. if event.delta.type == "text":
  11. print(event.delta.text, end="", flush=True)
JavaScript
  1. import { GoogleGenAI } from "@google/genai";

  2. const client = new GoogleGenAI({});

  3. const stream = await client.interactions.create({
  4. model: "gemini-3-flash-preview",
  5. input: "Count to from 1 to 25.",
  6. stream: true,
  7. });
  8. for await (const event of stream) {
  9. if (event.event_type === "step.delta") {
  10. if (event.delta.type === "text") {
  11. process.stdout.write(event.delta.text);
  12. }
  13. }
  14. }
REST
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "model": "gemini-3-flash-preview",
  7. "input": "Count to from 1 to 25.",
  8. "stream": true
  9. }'
  1. event: interaction.created
  2. data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","model":"gemini-3-flash-preview"},"event_type":"interaction.created"}

  3. event: interaction.status_update
  4. data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}

  5. event: step.start
  6. data: {"index":0,"step":{"type":"thought"},"event_type":"step.start"}

  7. event: step.delta
  8. data: {"index":0,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}

  9. event: step.stop
  10. data: {"index":0,"event_type":"step.stop"}

  11. event: step.start
  12. data: {"index":1,"step":{"type":"model_output"},"event_type":"step.start"}

  13. event: step.delta
  14. data: {"index":1,"delta":{"text":"1, 2, 3, 4, 5, 6, ","type":"text"},"event_type":"step.delta"}

  15. event: step.delta
  16. data: {"index":1,"delta":{"text":"7, 8, 9, 10, 11, 12, 13,","type":"text"},"event_type":"step.delta"}

  17. ...

  18. event: step.stop
  19. data: {"index":1,"event_type":"step.stop"}

  20. event: interaction.completed
  21. data: {"interaction":{"id":"v1_...","status":"completed","usage":{"total_tokens":346,"total_input_tokens":11,"input_tokens_by_modality":[{"modality":"text","tokens":11}],"total_cached_tokens":0,"total_output_tokens":90,"total_tool_use_tokens":0,"total_thought_tokens":245},"created":"2026-05-12T18:44:51Z","updated":"2026-05-12T18:44:51Z","service_tier":"standard","object":"interaction","model":"gemini-3-flash-preview"},"event_type":"interaction.completed"}

  22. event: done
  23. data: [DONE]
事件类型
每个服务器发送的事件都包含一个名为 event_type 的名称和关联的 JSON 数据。Interactions API 使用对称的流式传输模型,其中所有内容(文本、工具调用、思考)都通过一致的 基于步骤 的事件进行传输。
每个数据流都遵循以下事件流:
· interaction.created :创建互动,包括元数据(ID、模型、状态)。
· 一系列 步骤 ,每个步骤都包含: step.start 事件,用于指示步骤类型(例如 model_output 、 thought 、 function_call )。 一个或多个 step.delta 事件,其中包含相应步骤的增量数据。 用于将步骤标记为已完成的 step.stop 事件。
· step.start 事件,用于指示步骤类型(例如 model_output 、 thought 、 function_call )。
· 一个或多个 step.delta 事件,其中包含相应步骤的增量数据。
· 用于将步骤标记为已完成的 step.stop 事件。
· 具有最终 usage 统计信息的 interaction.completed 事件。
设置 stream: false 后,API 会返回一个包含 steps 数组的 interaction 对象。 steps 中的每个元素都是一个 step.start → step.delta (s) → step.stop 周期的完全组装版本。
interaction.created
在首次创建互动时发送。包含互动 ID、模型和初始状态。
  1. event: interaction.created
  2. data: {"interaction": {"id": "...", "model": "gemini-3-flash-preview", "status": "in_progress", "object": "interaction"}, "event_type": "interaction.created"}
interaction.status_update
表示互动级状态转换。可能会显示在步骤之间。
  1. event: interaction.status_update
  2. data: {"interaction_id": "...", "status": "in_progress", "event_type": "interaction.status_update"}
step.start
标记新步骤的开始。包含步骤 type 和 index 。步数类型决定了要预期哪些增量类型,以及步数在非流式响应中的显示方式:
表格内容: 步骤类型 | 预期增量类型 | 说明 model_output | text 、 image 、 audio | 模型的最终回答内容。 thought | thought_signature 、 thought_summary | 思维链推理。仅当 thinking_summaries 处于启用状态时,才会显示 summary 。 function_call | arguments_delta | 客户端执行函数的请求。将互动状态设置为 requires_action 。 服务器端工具 | 因工具而异 | 由 API 执行的工具(例如 google_search_call 、 google_search_result 、 code_execution_call 、 code_execution_result )。
如需查看完整列表,请参阅 Interactions API 参考文档 。
  1. event: step.start
  2. data: {"index": 0, "step": {"type": "model_output"}, "event_type": "step.start"}
对于函数调用,该步骤包括函数名称、ID 和空实参 {}
  1. event: step.start
  2. data: {"index": 0, "step": {"type": "function_call", "id":"un6k8t18", "name": "get_weather", "arguments":{}}, "event_type": "step.start"}
step.delta
当前步的增量数据。 delta 对象包含一个用于确定其形状的 type 字段。
text :来自 model_output 步骤的增量文本令牌:
  1. event: step.delta
  2. data: {"index": 0, "delta": {"type": "text", "text": "Hello, my name is Phil"}, "event_type": "step.delta"}

  3. event: step.delta
  4. data: {"index": 0, "delta": {"type": "text", "text": ", and I live in Germany." }, "event_type": "step.delta"}
image :来自 model_output 步骤的 Base64 编码图片数据:
  1. event: step.delta
  2. data: {"index": 0, "delta": {"type": "image", "mime_type": "image/jpeg", "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCg..."}, "event_type": "step.delta"}
thought_summary :来自 thought 步骤的思考总结内容:
  1. event: step.delta
  2. data: {"index": 0, "delta": {"type": "thought_summary", "content": {"type": "text", "text": "I need to find the GCD..."}}, "event_type": "step.delta"}
arguments_delta :函数调用实参的(部分)JSON 字符串。必须在多个增量中累积:
  1. event: step.delta
  2. data: {"index": 0, "delta": {"type": "arguments_delta", "arguments": "{\"location\": \"San Francisco, CA\"}"}, "event_type": "step.delta"}
以下是一些最常见的增量类型。如需查看所有增量类型的完整列表,请参阅 Interactions API 参考文档 。
step.stop
标记步骤的结束。包含步骤 index 。
  1. event: step.stop
  2. data: {"index": 0, "event_type": "step.stop"}
interaction.completed
互动结束时发送。包含具有 usage 统计信息的最终互动对象。在非流式模式下,这是顶级响应对象本身。不在响应中包含 steps 。
  1. event: interaction.completed
  2. data: {"interaction": {"id": "v1_abc123", "status": "completed", "usage": {"total_input_tokens": 7, "total_output_tokens": 12, "total_tokens": 19}}, "event_type": "interaction.completed"}
error
在互动期间发生错误时发送。包含一个带有消息和代码的错误对象。
  1. event: error
  2. data: {"error":{"message":"Deadline expired before operation could complete.","code":"gateway_timeout"},"event_type":"error"}
使用工具进行流式传输
Interactions API 支持在单个请求中通过客户端工具(函数调用)和服务器端工具(Google 搜索、代码执行等)进行流式传输。在流式传输期间,工具调用会以输入步骤的形式显示在事件流中。对于函数调用, step.start 事件会传递函数名称,而 step.delta 事件会以 JSON 字符串 ( arguments_delta ) 的形式流式传输实参。您必须累积这些增量才能获得完整的实参。Google 搜索等服务器端工具由 API 自动执行,从而生成 google_search_call 和 google_search_result 步骤。
使用函数调用进行流式传输
如需使用流式处理执行函数调用,客户端必须处理多轮对话:
· 第 1 轮(函数请求) :使用 stream: true 和您定义的 tools 调用 interactions.create 。该 API 将以流式传输 function_call 步。您必须从 step.delta 事件中累积增量实参 JSON 字符串 ( arguments_delta ),直到互动以状态 requires_action 完成。
· 第 2 轮(发送结果) :再次调用 interactions.create ,传递 previous_interaction_id (与第一次互动的 ID 匹配),并在 input 数组中发送 function_result 块。这会恢复流,使模型能够生成最终回答。
  1. from google import genai

  2. client = genai.Client()

  3. weather_tool = {
  4. "type": "function",
  5. "name": "get_weather",
  6. "description": "Get the current weather in a given location",
  7. "parameters": {
  8. "type": "object",
  9. "properties": {
  10. "location": {
  11. "type": "string",
  12. "description": "The city and state, e.g. San Francisco, CA"
  13. }
  14. },
  15. "required": ["location"]
  16. }
  17. }

  18. # Turn 1: Request function call
  19. stream = client.interactions.create(
  20. model="gemini-3-flash-preview",
  21. tools=[weather_tool],
  22. input="What is the weather in Paris right now?",
  23. stream=True,
  24. )

  25. first_interaction_id = None
  26. func_call_id = None
  27. func_call_name = None
  28. func_args_accumulated = ""

  29. for event in stream:
  30. if event.event_type == "interaction.created":
  31. first_interaction_id = event.interaction.id
  32. elif event.event_type == "step.start":
  33. step = event.step
  34. if step.type == "function_call":
  35. func_call_id = step.id
  36. func_call_name = step.name
  37. elif event.event_type == "step.delta":
  38. if event.delta.type == "arguments_delta":
  39. func_args_accumulated += event.delta.arguments

  40. # Turn 2: Execute tool and send the result back to resume stream
  41. if func_call_id:
  42. # Execute weather_tool using accumulated arguments
  43. # args = json.loads(func_args_accumulated)
  44. dummy_result = {
  45. "content": [{"type": "text", "text": '{"weather": "Sunny and 22°C"}'}]
  46. }

  47. stream2 = client.interactions.create(
  48. model="gemini-3-flash-preview",
  49. previous_interaction_id=first_interaction_id,
  50. input=[{
  51. "type": "function_result",
  52. "name": func_call_name,
  53. "call_id": func_call_id,
  54. "result": dummy_result
  55. }],
  56. stream=True,
  57. )

  58. for event in stream2:
  59. if event.event_type == "step.delta":
  60. if event.delta.type == "text":
  61. print(event.delta.text, end="", flush=True)
  1. import { GoogleGenAI } from "@google/genai";

  2. const client = new GoogleGenAI({});

  3. const weatherTool = {
  4. type: "function",
  5. name: "get_weather",
  6. description: "Get the current weather in a given location",
  7. parameters: {
  8. type: "object",
  9. properties: {
  10. location: {
  11. type: "string",
  12. description: "The city and state, e.g. San Francisco, CA"
  13. }
  14. },
  15. required: ["location"]
  16. }
  17. };

  18. // Turn 1: Request function call
  19. const stream = await client.interactions.create({
  20. model: "gemini-3-flash-preview",
  21. tools: [weatherTool],
  22. input: "What is the weather in Paris right now?",
  23. stream: true,
  24. });

  25. let firstInteractionId = null;
  26. let funcCallId = null;
  27. let funcCallName = null;
  28. let funcArgsAccumulated = "";

  29. for await (const event of stream) {
  30. if (event.event_type === "interaction.created") {
  31. firstInteractionId = event.interaction.id;
  32. } else if (event.event_type === "step.start") {
  33. const step = event.step;
  34. if (step.type === "function_call") {
  35. funcCallId = step.id;
  36. funcCallName = step.name;
  37. }
  38. } else if (event.event_type === "step.delta") {
  39. if (event.delta.type === "arguments_delta") {
  40. funcArgsAccumulated += event.delta.arguments;
  41. }
  42. }
  43. }

  44. // Turn 2: Execute tool and send the result back to resume stream
  45. if (funcCallId && firstInteractionId && funcCallName) {
  46. // const args = JSON.parse(funcArgsAccumulated);
  47. const dummyResult = {
  48. content: [{ type: "text", text: '{"weather": "Sunny and 22°C"}' }]
  49. };

  50. const stream2 = await client.interactions.create({
  51. model: "gemini-3-flash-preview",
  52. previous_interaction_id: firstInteractionId,
  53. input: [{
  54. type: "function_result",
  55. name: funcCallName,
  56. call_id: funcCallId,
  57. result: dummyResult
  58. }],
  59. stream: true,
  60. });

  61. for await (const event of stream2) {
  62. if (event.event_type === "step.delta") {
  63. if (event.delta.type === "text") {
  64. process.stdout.write(event.delta.text);
  65. }
  66. }
  67. }
  68. }
第 1 轮 :请求函数调用
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "model": "gemini-3-flash-preview",
  7. "input": "What is the weather in Paris right now?",
  8. "stream": true,
  9. "tools": [
  10. {
  11. "type": "function",
  12. "name": "get_weather",
  13. "description": "Get the current weather in a given location",
  14. "parameters": {
  15. "type": "object",
  16. "properties": {
  17. "location": {
  18. "type": "string",
  19. "description": "The city and state, e.g. San Francisco, CA"
  20. }
  21. },
  22. "required": ["location"]
  23. }
  24. }
  25. ]
  26. }'
第 2 轮 :使用第 1 轮中的 previous_interaction_id 和 call_id 发送函数结果
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "model": "gemini-3-flash-preview",
  7. "previous_interaction_id": "v1_ChdGUVFJYXBXVUdLVEF4TjhQ...",
  8. "stream": true,
  9. "input": [
  10. {
  11. "type": "function_result",
  12. "name": "get_weather",
  13. "call_id": "CALL_ID",
  14. "result": {
  15. "content": [
  16. {
  17. "type": "text",
  18. "text": "{\"weather\": \"Sunny and 22°C\"}"
  19. }
  20. ]
  21. }
  22. }
  23. ]
  24. }'
使用多种工具进行直播
以下示例在一个请求中同时使用了 function 工具和 google_search :
  1. from google import genai

  2. client = genai.Client()

  3. tools = [
  4. {"type": "google_search"},
  5. {
  6. "type": "function",
  7. "name": "get_weather",
  8. "description": "Get the current weather in a given location",
  9. "parameters": {
  10. "type": "object",
  11. "properties": {
  12. "location": {
  13. "type": "string",
  14. "description": "The city and state, e.g. San Francisco, CA"
  15. }
  16. },
  17. "required": ["location"]
  18. }
  19. }
  20. ]

  21. stream = client.interactions.create(
  22. model="gemini-3-flash-preview",
  23. tools=tools,
  24. input="Search what it the largest mountain in Europe and what the weather is there right now?",
  25. stream=True,
  26. )
  27. for event in stream:
  28. if event.event_type == "step.start":
  29. step = event.step
  30. print(f"\n--- Step {event.index}: {step.type} ---")
  31. # Show details for tool steps
  32. if step.type == "google_search_call":
  33. print(f" Search ID: {step.id}")
  34. elif step.type == "google_search_result":
  35. print(f" Result for: {step.call_id}")
  36. elif step.type == "function_call":
  37. print(f" Function: {step.name}({step.arguments})")
  38. elif event.event_type == "step.delta":
  39. if event.delta.type == "text":
  40. print(event.delta.text, end="", flush=True)
  41. elif event.delta.type == "google_search_call":
  42. print(f" Queries: {event.delta.arguments}")
  43. elif event.delta.type == "arguments_delta":
  44. print(f" Args chunk: {event.delta.arguments}", end="", flush=True)
  45. elif event.event_type == "interaction.completed":
  46. print(f"\n\nStatus: {event.interaction.status}")
  47. if event.interaction.status == "requires_action":
  48. print("Action required: provide function call results to continue.")
  1. import { GoogleGenAI } from "@google/genai";

  2. const client = new GoogleGenAI({});

  3. const tools = [
  4. { type: "google_search" },
  5. {
  6. type: "function",
  7. name: "get_weather",
  8. description: "Get the current weather in a given location",
  9. parameters: {
  10. type: "object",
  11. properties: {
  12. location: {
  13. type: "string",
  14. description: "The city and state, e.g. San Francisco, CA"
  15. }
  16. },
  17. required: ["location"]
  18. }
  19. }
  20. ];

  21. const stream = await client.interactions.create({
  22. model: "gemini-3-flash-preview",
  23. tools: tools,
  24. input: "Search what it the largest mountain in Europe and what the weather is there right now?",
  25. stream: true,
  26. });
  27. for await (const event of stream) {
  28. if (event.event_type === "step.start") {
  29. const step = event.step;
  30. console.log(`\n--- Step ${event.index}: ${step.type} ---`);
  31. // Show details for tool steps
  32. if (step.type === "google_search_call") {
  33. console.log(` Search ID: ${step.id}`);
  34. } else if (step.type === "google_search_result") {
  35. console.log(` Result for: ${step.call_id}`);
  36. } else if (step.type === "function_call") {
  37. console.log(` Function: ${step.name}(${JSON.stringify(step.arguments)})`);
  38. }
  39. } else if (event.event_type === "step.delta") {
  40. if (event.delta.type === "text") {
  41. process.stdout.write(event.delta.text);
  42. } else if (event.delta.type === "google_search_call") {
  43. console.log(` Queries: ${JSON.stringify(event.delta.arguments?.queries)}`);
  44. } else if (event.step.type === "google_search_result") {
  45. console.log(` Result for: ${event.step.call_id}`);
  46. } else if (event.delta.type === "arguments_delta") {
  47. process.stdout.write(` Args chunk: ${event.delta.arguments}`);
  48. }
  49. } else if (event.event_type === "interaction.completed") {
  50. console.log(`\n\nStatus: ${event.interaction.status}`);
  51. if (event.interaction.status === "requires_action") {
  52. console.log("Action required: provide function call results to continue.");
  53. }
  54. }
  55. }
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "model": "gemini-3-flash-preview",
  7. "input": "Search what it the largest mountain in Europe and what the weather is there right now?",
  8. "stream": true,
  9. "tools": [
  10. { "type": "google_search" },
  11. {
  12. "type": "function",
  13. "name": "get_weather",
  14. "description": "Get the current weather in a given location",
  15. "parameters": {
  16. "type": "object",
  17. "properties": {
  18. "location": {
  19. "type": "string",
  20. "description": "The city and state, e.g. San Francisco, CA"
  21. }
  22. },
  23. "required": ["location"]
  24. }
  25. }
  26. ]
  27. }'
包含思考的流式传输
当模型使用思考功能时,您会收到 thought 步,其中包含两种不同的增量类型: thought_summary (增量文本或图片摘要内容)和 thought_signature (模型内部推理的加密表示形式,在 step.stop 之前作为最后一个增量发送)。如果启用 thinking_summaries , thought_summary 增量会流式传输模型推理的摘要。如需详细了解思考,请参阅 思考指南 。
  1. from google import genai

  2. client = genai.Client()

  3. stream = client.interactions.create(
  4. model="gemini-3-flash-preview",
  5. input="What is the greatest common divisor of 1071 and 462?",
  6. generation_config={
  7. "thinking_summaries": "auto"
  8. },
  9. stream=True,
  10. )
  11. for event in stream:
  12. if event.event_type == "step.start":
  13. print(f"\n--- Step: {event.step.type} ---")
  14. elif event.event_type == "step.delta":
  15. if event.delta.type == "thought_summary":
  16. if event.delta.content.type == "text":
  17. print(event.delta.content.text, end="", flush=True)
  18. elif event.delta.type == "text":
  19. print(event.delta.text, end="", flush=True)
  1. import { GoogleGenAI } from "@google/genai";

  2. const client = new GoogleGenAI({});

  3. const stream = await client.interactions.create({
  4. model: "gemini-3-flash-preview",
  5. input: "What is the greatest common divisor of 1071 and 462?",
  6. generation_config: {
  7. thinking_summaries: "auto",
  8. },
  9. stream: true,
  10. });
  11. for await (const event of stream) {
  12. if (event.event_type === "step.start") {
  13. console.log(`\n--- Step: ${event.step.type} ---`);
  14. } else if (event.event_type === "step.delta") {
  15. if (event.delta.type === "thought_summary") {
  16. if (event.delta.content.type === "text") {
  17. process.stdout.write(event.delta.content.text);
  18. }
  19. } else if (event.delta.type === "text") {
  20. process.stdout.write(event.delta.text);
  21. }
  22. }
  23. }
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "model": "gemini-3-flash-preview",
  7. "input": "What is the greatest common divisor of 1071 and 462?",
  8. "stream": true,
  9. "generation_config": {
  10. "thinking_summaries": "auto"
  11. }
  12. }'
使用代理进行流式传输
Interactions API 支持 Deep Research 等智能体。代理使用 background=True 并异步返回结果,但您也可以流式传输代理互动,以便在互动发生时接收进度更新和中间步骤。如需了解详情,请参阅 后台执行指南 和 Deep Research 指南 。
  1. from google import genai

  2. client = genai.Client()

  3. stream = client.interactions.create(
  4. agent="deep-research-preview-04-2026",
  5. input="Research the latest advances in quantum computing.",
  6. stream=True,
  7. background=True,
  8. agent_config={
  9. "type": "deep-research",
  10. "thinking_summaries": "auto"
  11. }
  12. )
  13. for event in stream:
  14. if event.event_type == "step.start":
  15. print(f"\n--- Step: {event.step.type} ---")
  16. elif event.event_type == "step.delta":
  17. if event.delta.type == "text":
  18. print(event.delta.text, end="", flush=True)
  19. elif event.delta.type == "thought_summary":
  20. if event.delta.content.type == "text":
  21. print(event.delta.content.text, end="", flush=True)
  22. elif event.event_type == "interaction.completed":
  23. print(f"\n\nTotal Tokens: {event.interaction.usage.total_tokens}")
  1. import { GoogleGenAI } from "@google/genai";

  2. const client = new GoogleGenAI({});

  3. const stream = await client.interactions.create({
  4. agent: "deep-research-preview-04-2026",
  5. input: "Research the latest advances in quantum computing.",
  6. stream: true,
  7. background: true,
  8. agent_config: {
  9. type: "deep-research",
  10. thinking_summaries: "auto"
  11. }
  12. });
  13. for await (const event of stream) {
  14. if (event.event_type === "step.start") {
  15. console.log(`\n--- Step: ${event.step.type} ---`);
  16. } else if (event.event_type === "step.delta") {
  17. if (event.delta.type === "text") {
  18. process.stdout.write(event.delta.text);
  19. } else if (event.delta.type === "thought_summary") {
  20. if (event.delta.content.type === "text") {
  21. process.stdout.write(event.delta.content.text);
  22. }
  23. }
  24. } else if (event.event_type === "interaction.completed") {
  25. console.log(`\n\nTotal Tokens: ${event.interaction.usage.total_tokens}`);
  26. }
  27. }
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "agent": "deep-research-preview-04-2026",
  7. "input": "Research the latest advances in quantum computing.",
  8. "stream": true,
  9. "background": true,
  10. "agent_config": {
  11. "type": "deep-research",
  12. "thinking_summaries": "auto"
  13. }
  14. }'
  1. event: interaction.created
  2. data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","agent":"deep-research-preview-04-2026"},"event_type":"interaction.created"}

  3. event: interaction.status_update
  4. data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}

  5. event: step.start
  6. data: {"index":0,"step":{"type":"thought"},"event_type":"step.start"}

  7. event: step.delta
  8. data: {"index":0,"delta":{"content":{"text":"***Generating research plan***\n\nTo best answer your request, I'm starting by constructing a comprehensive research plan. This will outline the key areas I need to investigate and the strategy I'll use to connect them."},"type":"thought_summary"},"event_type":"step.delta"}

  9. ... (additional thought steps) ...

  10. event: step.stop
  11. data: {"index":0,"event_type":"step.stop"}

  12. event: step.start
  13. data: {"index":1,"step":{"type":"model_output"},"event_type":"step.start"}

  14. event: step.delta
  15. data: {"index":1,"delta":{"text":"# The Quantum Inflection Point: Exhaustive Analysis of Hardware, Algorithms, and Market Dynamics in 2026\n\n## Executive Summary\n\n..."},"event_type":"step.delta"}

  16. event: step.stop
  17. data: {"index":1,"event_type":"step.stop"}

  18. event: interaction.completed
  19. data: {"interaction":{"id":"v1_...","status":"completed","usage":{"total_tokens":1117031,"total_input_tokens":428865,"total_output_tokens":22294,"total_thought_tokens":26213},"created":"2026-05-12T17:24:27Z","updated":"2026-05-12T17:24:27Z","object":"interaction","agent":"deep-research-preview-04-2026"},"event_type":"interaction.completed"}

  20. event: done
  21. data: [DONE]
流式图片生成
Interactions API 支持同时以流式传输多种输出模态。通过在 response_format 中同时请求 text 和 image ,您可以在同一数据流中接收交织的文本和生成的图片。
以下示例使用 gemini-3.1-flash-image-preview (Nano Banana 2) 搜索信息并生成包含插图的故事。
  1. from google import genai

  2. client = genai.Client()

  3. stream = client.interactions.create(
  4. model="gemini-3.1-flash-image-preview",
  5. tools=[{"type": "google_search", "search_types": ["web_search", "image_search"]}],
  6. input="Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images.",
  7. response_format=[
  8. {"type": "text"},
  9. {"type": "image"}
  10. ],
  11. stream=True,
  12. )

  13. for event in stream:
  14. if event.event_type == "step.delta":
  15. if event.delta.type == "text":
  16. print(event.delta.text, end="", flush=True)
  17. elif event.delta.type == "image":
  18. print(f"\n[Image chunk: {len(event.delta.data)} bytes]", end="", flush=True)
  1. import { GoogleGenAI } from "@google/genai";

  2. const client = new GoogleGenAI({});

  3. const stream = await client.interactions.create({
  4. model: "gemini-3.1-flash-image-preview",
  5. tools: [{ type: "google_search", search_types: ["web_search", "image_search"] }],
  6. input: "Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images.",
  7. response_format: [
  8. { type: "text" },
  9. { type: "image" }
  10. ],
  11. stream: true,
  12. });

  13. for await (const event of stream) {
  14. if (event.event_type === "step.delta") {
  15. if (event.delta.type === "text") {
  16. process.stdout.write(event.delta.text);
  17. } else if (event.delta.type === "image") {
  18. console.log(`\n[Image chunk: ${event.delta.data.length} bytes]`);
  19. }
  20. }
  21. }
  1. curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  2. -H "x-goog-api-key: $GEMINI_API_KEY" \
  3. -H "Content-Type: application/json" \
  4. --no-buffer \
  5. -d '{
  6. "model": "gemini-3.1-flash-image-preview",
  7. "input": "Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images.",
  8. "stream": true,
  9. "tools": [
  10. { "type": "google_search",
  11. "search_types": ["web_search", "image_search"]
  12. }
  13. ],
  14. "generation_config": {
  15. "thinking_summaries": "auto"
  16. },
  17. "response_format": [
  18. { "type": "text" }, { "type": "image"}
  19. ]
  20. }'
  1. event: interaction.created
  2. data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","model":"gemini-3.1-flash-image-preview"},"event_type":"interaction.created"}

  3. event: interaction.status_update
  4. data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}

  5. event: step.start
  6. data: {"index":0,"step":{"type":"model_output"},"event_type":"step.start"}

  7. event: step.delta
  8. data: {"index":0,"delta":{"text":"Here is a short illustrated story about the Colosseum...\n\n### Part 1: The New Flavian Amphitheater\n\n...","type":"text"},"event_type":"step.delta"}

  9. ...

  10. event: step.stop
  11. data: {"index":0,"event_type":"step.stop"}

  12. event: step.start
  13. data: {"index":1,"step":{"type":"thought"},"event_type":"step.start"}

  14. event: step.delta
  15. data: {"index":1,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}

  16. event: step.stop
  17. data: {"index":1,"event_type":"step.stop"}

  18. event: step.start
  19. data: {"index":2,"step":{"type":"model_output"},"event_type":"step.start"}

  20. event: step.delta
  21. data: {"index":2,"delta":{"mime_type":"image/jpeg","data":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCg...","type":"image"},"event_type":"step.delta"}

  22. event: step.delta
  23. data: {"index":2,"delta":{"text":"### Part 2: The Hypogeum and the Wait\n\n...","type":"text"},"event_type":"step.delta"}

  24. ...

  25. event: step.stop
  26. data: {"index":2,"event_type":"step.stop"}

  27. event: step.start
  28. data: {"index":3,"step":{"type":"thought"},"event_type":"step.start"}

  29. event: step.delta
  30. data: {"index":3,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}

  31. event: step.stop
  32. data: {"index":3,"event_type":"step.stop"}

  33. event: step.start
  34. data: {"index":4,"step":{"type":"model_output"},"event_type":"step.start"}

  35. event: step.delta
  36. data: {"index":4,"delta":{"mime_type":"image/jpeg","data":"/9j/4AAQSkZJRgABAQAAAQABAAD/...","type":"image"},"event_type":"step.delta"}

  37. event: step.delta
  38. data: {"index":4,"delta":{"text":"### Part 3: The Moment of Spectacle\n\n...","type":"text"},"event_type":"step.delta"}

  39. ...

  40. event: step.stop
  41. data: {"index":4,"event_type":"step.stop"}

  42. event: interaction.completed
  43. data: {"interaction":{"id":"v1_...","status":"completed","usage":{"total_tokens":6128,"total_input_tokens":29,"total_output_tokens":6099,"output_tokens_by_modality":[{"modality":"image","tokens":4480}]}},"event_type":"interaction.completed"}

  44. event: done
  45. data: [DONE]
处理未知事件
根据 API 的版本控制政策,随着时间的推移,可能会添加新的事件类型和增量类型。您的代码应妥善处理未知事件类型,记录并跳过任何无法识别的事件,而不是抛出错误。
后续步骤
· 详细了解 Interactions API 。
· 探索使用工具进行 函数调用 。
· 了解如何通过 思考 来增强推理能力。
· 对于长时间运行的任务,请尝试使用 Deep Research 代理 。
· 如需查看所有事件类型和增量类型,请参阅 Interactions API 参考文档 。