SPB Git

Repository Public

streaming.md 59.8 KB 1,450 lines markdown

1When creating an Interaction, you can set `stream: true` to incrementally stream the response using [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) (SSE).23### Python45    from google import genai67    client = genai.Client()89    stream = client.interactions.create(10        model="gemini-3.8-flash",11        input="Count from 1 to 25.",12        stream=True,13    )14    for event in stream:15        if event.event_type == "step.delta":16            if event.delta.type == "text":17                print(event.delta.text, end="", flush=True)1819### JavaScript2021    import { GoogleGenAI } from "@google/genai";2223    const client = new GoogleGenAI({});2425    const stream = await client.interactions.create({26        model: "gemini-3.8-flash",27        input: "Count from 1 to 25.",28        stream: true,29    });30    for await (const event of stream) {31        if (event.event_type === "step.delta") {32            if (event.delta.type === "text") {33                process.stdout.write(event.delta.text);34            }35        }36    }3738### Java3940    import com.google.genai.Client;41    import com.google.genai.gaos.models.interactions.CreateModelInteraction;42    import com.google.genai.gaos.models.interactions.InteractionSSEEvent;43    import com.google.genai.gaos.models.interactions.InteractionSSEStreamEvent;44    import com.google.genai.gaos.models.interactions.InteractionsInput;45    import com.google.genai.gaos.models.interactions.Model;46    import com.google.genai.gaos.models.interactions.StepDelta;47    import com.google.genai.gaos.models.interactions.StepDeltaData;48    import com.google.genai.gaos.models.interactions.TextDelta;49    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;50    import com.google.genai.gaos.models.operations.CreateInteractionResponse;51    import com.google.genai.gaos.utils.EventStream;5253    Client client = new Client();5455    CreateModelInteraction params =56        CreateModelInteraction.builder()57            .model(Model.of("gemini-3.8-flash"))58            .input(InteractionsInput.of("Count from 1 to 25."))59            .stream(true)60            .build();6162    CreateInteractionResponse response =63        client.interactions.create(CreateInteractionRequestBody.of(params));6465    try (EventStream<InteractionSSEStreamEvent> events = response.events()) {66      for (InteractionSSEStreamEvent streamEvent : events) {67        InteractionSSEEvent event = streamEvent.data().orElse(null);68        if (event instanceof StepDelta stepDelta) {69          StepDeltaData data = stepDelta.delta().orElse(null);70          if (data instanceof TextDelta textDelta) {71            textDelta.text().ifPresent(System.out::print);72          }73        }74      }75    }7677### REST7879    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \80      -H "x-goog-api-key: $GEMINI_API_KEY" \81      -H "Content-Type: application/json" \82      --no-buffer \83      -d '{84        "model": "gemini-3.8-flash",85        "input": "Count from 1 to 25.",86        "stream": true87      }'8889    event: interaction.created90    data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","model":"gemini-3.8-flash"},"event_type":"interaction.created"}9192    event: interaction.status_update93    data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}9495    event: step.start96    data: {"index":0,"step":{"type":"thought"},"event_type":"step.start"}9798    event: step.delta99    data: {"index":0,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}100101    event: step.stop102    data: {"index":0,"event_type":"step.stop"}103104    event: step.start105    data: {"index":1,"step":{"type":"model_output"},"event_type":"step.start"}106107    event: step.delta108    data: {"index":1,"delta":{"text":"1, 2, 3, 4, 5, 6, ","type":"text"},"event_type":"step.delta"}109110    event: step.delta111    data: {"index":1,"delta":{"text":"7, 8, 9, 10, 11, 12, 13,","type":"text"},"event_type":"step.delta"}112113    ...114115    event: step.stop116    data: {"index":1,"event_type":"step.stop"}117118    event: interaction.completed119    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.8-flash"},"event_type":"interaction.completed"}120121    event: done122    data: [DONE]123124## Event types125126Each server-sent event includes a named `event_type` and associated JSON data. The Interactions API uses a symmetric streaming model where all content---text, tool calls, thinking---flows through a consistent **step-based** event.127128Each stream follows this event flow:1291301. `interaction.created`: The interaction is created, includes metadata (ID, model, status).1312. A series of **steps** , each consisting of:132   - A `step.start` event, indicating the step type (e.g., `model_output`, `thought`, `function_call`).133   - One or more `step.delta` events with incremental data for that step.134   - A `step.stop` event marking the step as complete.1353. An `interaction.completed` event with final `usage` statistics.136137When you set `stream: false`, the API returns a single `interaction` object with a `steps` array. Each element in `steps` is the fully assembled version of one `step.start` → `step.delta`(s) → `step.stop` cycle.138139### `interaction.created`140141Sent when the interaction is first created. Contains the interaction ID, model, and initial status.142143    event: interaction.created144    data: {"interaction": {"id": "...", "model": "gemini-3.8-flash", "status": "in_progress", "object": "interaction"}, "event_type": "interaction.created"}145146### `interaction.status_update`147148Signals an interaction-level status transition. May appear between steps.149150    event: interaction.status_update151    data: {"interaction_id": "...", "status": "in_progress", "event_type": "interaction.status_update"}152153### `step.start`154155Marks the beginning of a new step. Contains the step `type` and `index`. The step type determines which delta types to expect and how the step appears in a non-streaming response:156157| Step Type | Expected Delta Types | Description |158|---|---|---|159| `model_output` | `text`, `image`, `audio` | The model's final response content. |160| `thought` | `thought_signature`, `thought_summary` | Chain-of-thought reasoning. `summary` is only present when `thinking_summaries` is enabled. |161| `function_call` | `arguments_delta` | A request for the client to execute a function. Sets interaction status to `requires_action`. |162| Server-side tools | Varies by tool | Tools executed by the API (e.g., `google_search_call`, `google_search_result`, `code_execution_call`, `code_execution_result`). |163164See the [Interactions API reference](https://ai.google.dev/api/interactions-api) for the full list.165166    event: step.start167    data: {"index": 0, "step": {"type": "model_output"}, "event_type": "step.start"}168169For function calls, the step includes the function name, id and empty arguments `{}`.170171    event: step.start172    data: {"index": 0, "step": {"type": "function_call", "id":"un6k8t18", "name": "get_weather", "arguments":{}}, "event_type": "step.start"}173174### `step.delta`175176Incremental data for the current step. The `delta` object contains a `type` field that determines its shape.177178**Examples:**179180**`text`:** Incremental text token from a `model_output` step:181182    event: step.delta183    data: {"index": 0, "delta": {"type": "text", "text": "Hello, my name is Phil"}, "event_type": "step.delta"}184185    event: step.delta186    data: {"index": 0, "delta": {"type": "text", "text": ", and I live in Germany." }, "event_type": "step.delta"}187188**`image`:** Base64-encoded image data from a `model_output` step:189190    event: step.delta191    data: {"index": 0, "delta": {"type": "image", "mime_type": "image/jpeg", "data": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCg..."}, "event_type": "step.delta"}192193**`thought_summary`:** Thinking summary content from a `thought` step:194195    event: step.delta196    data: {"index": 0, "delta": {"type": "thought_summary", "content": {"type": "text", "text": "I need to find the GCD..."}}, "event_type": "step.delta"}197198**`arguments_delta`:** (Partial) JSON string for function call arguments. Must be accumulated across deltas:199200    event: step.delta201    data: {"index": 0, "delta": {"type": "arguments_delta", "arguments": "{\"location\": \"San Francisco, CA\"}"}, "event_type": "step.delta"}202203These are some of the most common delta types. For the complete list of all delta types, see the [Interactions API reference](https://ai.google.dev/api/interactions-api).204205### `step.stop`206207Marks the end of a step. Contains the step `index`.208209    event: step.stop210    data: {"index": 0, "event_type": "step.stop"}211212When using the [Antigravity Agent](https://ai.google.dev/gemini-api/docs/antigravity-agent), the213`step.stop` event may also include usage statistics:214215- **`usage`**: The accumulated usage (running total) since the start of the interaction.216- **`step_usage`**: The usage of this specific step.217218    event: step.stop219    data: {"index": 2, "event_type": "step.stop", "usage": {"total_tokens": 4650, "total_input_tokens": 3577, "total_output_tokens": 305, "total_cached_tokens": 0}, "step_usage": {"total_tokens": 303, "total_input_tokens": 31, "total_output_tokens": 3, "total_cached_tokens": 0}}220221### `interaction.completed`222223Sent when the interaction is finished. Contains the final interaction object with `usage` statistics. In non-streaming mode, this is the top-level response object itself. Does not include `steps` in the response.224225    event: interaction.completed226    data: {"interaction": {"id": "v1_abc123", "status": "completed", "usage": {"total_input_tokens": 7, "total_output_tokens": 12, "total_tokens": 19}}, "event_type": "interaction.completed"}227228### `error`229230Sent when an error occurs during the interaction. Contains an error object with a message and code.231232    event: error233    data: {"error":{"message":"Deadline expired before operation could complete.","code":"gateway_timeout"},"event_type":"error"}234235## Streaming with tools236237The Interactions API supports streaming with both client-side tools (function238calling) and server-side tools (Google Search, Code Execution, etc.) in a single239request. During streaming, tool invocations appear as typed steps in the event240stream. For function calls, the `step.start` event delivers the function name,241and `step.delta` events stream the arguments as JSON strings242(`arguments_delta`). You must accumulate these deltas to get the full arguments.243Server-side tools like Google Search are executed automatically by the API,244producing `google_search_call` and `google_search_result` steps.245246### Streaming with function calling247248To perform function calling with streaming, the client must handle a multi-turn249conversation:2502511. **Turn 1 (Function Request):** Call `interactions.create` with `stream: true` and your defined `tools`. The API will stream a `function_call` step. You must accumulate the incremental argument JSON strings (`arguments_delta`) from `step.delta` events until the interaction completes with the status `requires_action`.2522. **Turn 2 (Sending Result):** Call `interactions.create` again, passing the `previous_interaction_id` (matching the ID of the first interaction) and sending a `function_result` block within the `input` array. This resumes the stream, allowing the model to generate its final response.253254### Python255256    from google import genai257258    client = genai.Client()259260    weather_tool = {261        "type": "function",262        "name": "get_weather",263        "description": "Get the current weather in a given location",264        "parameters": {265            "type": "object",266            "properties": {267                "location": {268                    "type": "string",269                    "description": "The city and state, e.g. San Francisco, CA"270                }271            },272            "required": ["location"]273        }274    }275276    # Turn 1: Request function call277    stream = client.interactions.create(278        model="gemini-3.8-flash",279        tools=[weather_tool],280        input="What is the weather in Paris right now?",281        stream=True,282    )283284    first_interaction_id = None285    func_call_id = None286    func_call_name = None287    func_args_accumulated = ""288289    for event in stream:290        if event.event_type == "interaction.created":291            first_interaction_id = event.interaction.id292        elif event.event_type == "step.start":293            step = event.step294            if step.type == "function_call":295                func_call_id = step.id296                func_call_name = step.name297        elif event.event_type == "step.delta":298            if event.delta.type == "arguments_delta":299                func_args_accumulated += event.delta.arguments300301    # Turn 2: Execute tool and send the result back to resume stream302    if func_call_id:303        # Execute weather_tool using accumulated arguments304        dummy_result = {305            "content": [{"type": "text", "text": '{"weather": "Sunny and 22°C"}'}]306        }307308        stream2 = client.interactions.create(309            model="gemini-3.8-flash",310            previous_interaction_id=first_interaction_id,311            input=[{312                "type": "function_result",313                "name": func_call_name,314                "call_id": func_call_id,315                "result": dummy_result316            }],317            stream=True,318        )319320        for event in stream2:321            if event.event_type == "step.delta":322                if event.delta.type == "text":323                    print(event.delta.text, end="", flush=True)324325### JavaScript326327    import { GoogleGenAI } from "@google/genai";328329    const client = new GoogleGenAI({});330331    const weatherTool = {332        type: "function",333        name: "get_weather",334        description: "Get the current weather in a given location",335        parameters: {336            type: "object",337            properties: {338                location: {339                    type: "string",340                    description: "The city and state, e.g. San Francisco, CA"341                }342            },343            required: ["location"]344        }345    };346347    // Turn 1: Request function call348    const stream = await client.interactions.create({349        model: "gemini-3.8-flash",350        tools: [weatherTool],351        input: "What is the weather in Paris right now?",352        stream: true,353    });354355    let firstInteractionId = null;356    let funcCallId = null;357    let funcCallName = null;358    let funcArgsAccumulated = "";359360    for await (const event of stream) {361        if (event.event_type === "interaction.created") {362            firstInteractionId = event.interaction.id;363        } else if (event.event_type === "step.start") {364            const step = event.step;365            if (step.type === "function_call") {366                funcCallId = step.id;367                funcCallName = step.name;368            }369        } else if (event.event_type === "step.delta") {370            if (event.delta.type === "arguments_delta") {371                funcArgsAccumulated += event.delta.arguments;372            }373        }374    }375376    // Turn 2: Execute tool and send the result back to resume stream377    if (funcCallId && firstInteractionId && funcCallName) {378        const dummyResult = {379            content: [{ type: "text", text: '{"weather": "Sunny and 22°C"}' }]380        };381382        const stream2 = await client.interactions.create({383            model: "gemini-3.8-flash",384            previous_interaction_id: firstInteractionId,385            input: [{386                type: "function_result",387                name: funcCallName,388                call_id: funcCallId,389                result: dummyResult390            }],391            stream: true,392        });393394        for await (const event of stream2) {395            if (event.event_type === "step.delta") {396                if (event.delta.type === "text") {397                    process.stdout.write(event.delta.text);398                }399            }400        }401    }402403### Java404405    import com.google.genai.Client;406    import com.google.genai.gaos.models.interactions.ArgumentsDelta;407    import com.google.genai.gaos.models.interactions.CreateModelInteraction;408    import com.google.genai.gaos.models.interactions.Function;409    import com.google.genai.gaos.models.interactions.FunctionCallStep;410    import com.google.genai.gaos.models.interactions.FunctionResultStep;411    import com.google.genai.gaos.models.interactions.FunctionResultStepResultUnion;412    import com.google.genai.gaos.models.interactions.InteractionCreatedEvent;413    import com.google.genai.gaos.models.interactions.InteractionSSEEvent;414    import com.google.genai.gaos.models.interactions.InteractionSSEStreamEvent;415    import com.google.genai.gaos.models.interactions.InteractionSseEventInteraction;416    import com.google.genai.gaos.models.interactions.InteractionsInput;417    import com.google.genai.gaos.models.interactions.Model;418    import com.google.genai.gaos.models.interactions.Step;419    import com.google.genai.gaos.models.interactions.StepDelta;420    import com.google.genai.gaos.models.interactions.StepDeltaData;421    import com.google.genai.gaos.models.interactions.StepStart;422    import com.google.genai.gaos.models.interactions.TextContent;423    import com.google.genai.gaos.models.interactions.TextDelta;424    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;425    import com.google.genai.gaos.models.operations.CreateInteractionResponse;426    import com.google.genai.gaos.utils.EventStream;427    import java.util.Arrays;428    import java.util.HashMap;429    import java.util.Map;430431    Client client = new Client();432433    Map<String, Object> locationProp = new HashMap<>();434    locationProp.put("type", "string");435    locationProp.put("description", "The city and state, e.g. San Francisco, CA");436437    Map<String, Object> properties = new HashMap<>();438    properties.put("location", locationProp);439440    Map<String, Object> parameters = new HashMap<>();441    parameters.put("type", "object");442    parameters.put("properties", properties);443    parameters.put("required", Arrays.asList("location"));444445    Function weatherTool =446        Function.builder()447            .name("get_weather")448            .description("Get the current weather in a given location")449            .parameters(parameters)450            .build();451452    // Turn 1: Request function call453    CreateModelInteraction params =454        CreateModelInteraction.builder()455            .model(Model.of("gemini-3.8-flash"))456            .tools(Arrays.asList(weatherTool))457            .input(InteractionsInput.of("What is the weather in Paris right now?"))458            .stream(true)459            .build();460461    CreateInteractionResponse response =462        client.interactions.create(CreateInteractionRequestBody.of(params));463464    String firstInteractionId = null;465    String funcCallId = null;466    String funcCallName = null;467    StringBuilder funcArgsAccumulated = new StringBuilder();468469    try (EventStream<InteractionSSEStreamEvent> stream = response.events()) {470      for (InteractionSSEStreamEvent streamEvent : stream) {471        InteractionSSEEvent event = streamEvent.data().orElse(null);472        if (event instanceof InteractionCreatedEvent createdEvent) {473          firstInteractionId =474              createdEvent.interaction().flatMap(InteractionSseEventInteraction::id).orElse(null);475        } else if (event instanceof StepStart stepStart) {476          Step step = stepStart.step().orElse(null);477          if (step instanceof FunctionCallStep fcStep) {478            funcCallId = fcStep.id().orElse(null);479            funcCallName = fcStep.name().orElse(null);480          }481        } else if (event instanceof StepDelta stepDelta) {482          StepDeltaData delta = stepDelta.delta().orElse(null);483          if (delta instanceof ArgumentsDelta argsDelta) {484            funcArgsAccumulated.append(argsDelta.arguments().orElse(""));485          }486        }487      }488    }489490    // Turn 2: Execute tool and send the result back to resume stream491    if (funcCallId != null && firstInteractionId != null && funcCallName != null) {492      FunctionResultStep resultStep =493          FunctionResultStep.builder()494              .name(funcCallName)495              .callId(funcCallId)496              .result(497                  FunctionResultStepResultUnion.of(498                      Arrays.asList(TextContent.builder().text("{\"weather\": \"Sunny and 22°C\"}").build())))499              .build();500501      CreateModelInteraction params2 =502          CreateModelInteraction.builder()503              .model(Model.of("gemini-3.8-flash"))504              .previousInteractionId(firstInteractionId)505              .input(InteractionsInput.ofStep(Arrays.asList(resultStep)))506              .stream(true)507              .build();508509      CreateInteractionResponse response2 =510          client.interactions.create(CreateInteractionRequestBody.of(params2));511512      try (EventStream<InteractionSSEStreamEvent> stream2 = response2.events()) {513        for (InteractionSSEStreamEvent streamEvent : stream2) {514          InteractionSSEEvent event = streamEvent.data().orElse(null);515          if (event instanceof StepDelta stepDelta) {516            StepDeltaData delta = stepDelta.delta().orElse(null);517            if (delta instanceof TextDelta textDelta) {518              textDelta.text().ifPresent(System.out::print);519            }520          }521        }522      }523    }524525### REST526527**Turn 1:** Request function call528529    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \530      -H "x-goog-api-key: $GEMINI_API_KEY" \531      -H "Content-Type: application/json" \532      --no-buffer \533      -d '{534        "model": "gemini-3.8-flash",535        "input": "What is the weather in Paris right now?",536        "stream": true,537        "tools": [538          {539            "type": "function",540            "name": "get_weather",541            "description": "Get the current weather in a given location",542            "parameters": {543              "type": "object",544              "properties": {545                "location": {546                  "type": "string",547                  "description": "The city and state, e.g. San Francisco, CA"548                }549              },550              "required": ["location"]551            }552          }553        ]554      }'555556**Turn 2:** Send the function result using the `previous_interaction_id` and `call_id` from Turn 1557558    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \559      -H "x-goog-api-key: $GEMINI_API_KEY" \560      -H "Content-Type: application/json" \561      --no-buffer \562      -d '{563        "model": "gemini-3.8-flash",564        "previous_interaction_id": "v1_ChdGUVFJYXBXVUdLVEF4TjhQ...",565        "stream": true,566        "input": [567          {568            "type": "function_result",569            "name": "get_weather",570            "call_id": "CALL_ID",571            "result": {572              "content": [573                {574                  "type": "text",575                  "text": "{\"weather\": \"Sunny and 22°C\"}"576                }577              ]578            }579          }580        ]581      }'582583### Streaming with multiple tools584585The following example uses both a `function` tool and `google_search` in one request:586587### Python588589    from google import genai590591    client = genai.Client()592593    tools = [594        {"type": "google_search"},595        {596            "type": "function",597            "name": "get_weather",598            "description": "Get the current weather in a given location",599            "parameters": {600                "type": "object",601                "properties": {602                    "location": {603                        "type": "string",604                        "description": "The city and state, e.g. San Francisco, CA"605                    }606                },607                "required": ["location"]608            }609        }610    ]611612    stream = client.interactions.create(613        model="gemini-3.8-flash",614        tools=tools,615        input="Search what is the largest mountain in Europe and what the weather is there right now?",616        stream=True,617    )618    for event in stream:619        if event.event_type == "step.start":620            step = event.step621            print(f"\n--- Step {event.index}: {step.type} ---")622            # Show details for tool steps623            if step.type == "google_search_call":624                print(f"  Search ID: {step.id}")625            elif step.type == "google_search_result":626                print(f"  Result for: {step.call_id}")627            elif step.type == "function_call":628                print(f"  Function: {step.name}({step.arguments})")629        elif event.event_type == "step.delta":630            if event.delta.type == "text":631                print(event.delta.text, end="", flush=True)632            elif event.delta.type == "google_search_call":633                print(f"  Queries: {event.delta.arguments}")634            elif event.delta.type == "arguments_delta":635                print(f"  Args chunk: {event.delta.arguments}", end="", flush=True)636        elif event.event_type == "interaction.completed":637            print(f"\n\nStatus: {event.interaction.status}")638            if event.interaction.status == "requires_action":639                print("Action required: provide function call results to continue.")640641### JavaScript642643    import { GoogleGenAI } from "@google/genai";644645    const client = new GoogleGenAI({});646647    const tools = [648        { type: "google_search" },649        {650            type: "function",651            name: "get_weather",652            description: "Get the current weather in a given location",653            parameters: {654                type: "object",655                properties: {656                    location: {657                        type: "string",658                        description: "The city and state, e.g. San Francisco, CA"659                    }660                },661                required: ["location"]662            }663        }664    ];665666    const stream = await client.interactions.create({667        model: "gemini-3.8-flash",668        tools: tools,669        input: "Search what is the largest mountain in Europe and what the weather is there right now?",670        stream: true,671    });672    for await (const event of stream) {673        if (event.event_type === "step.start") {674            const step = event.step;675            console.log(`\n--- Step ${event.index}: ${step.type} ---`);676            // Show details for tool steps677            if (step.type === "google_search_call") {678                console.log(`  Search ID: ${step.id}`);679            } else if (step.type === "google_search_result") {680                console.log(`  Result for: ${step.call_id}`);681            } else if (step.type === "function_call") {682                console.log(`  Function: ${step.name}(${JSON.stringify(step.arguments)})`);683            }684        } else if (event.event_type === "step.delta") {685            if (event.delta.type === "text") {686                process.stdout.write(event.delta.text);687            } else if (event.delta.type === "google_search_call") {688                console.log(`  Queries: ${JSON.stringify(event.delta.arguments?.queries)}`);689            } else if (event.delta.type === "arguments_delta") {690                process.stdout.write(`  Args chunk: ${event.delta.arguments}`);691            }692        } else if (event.event_type === "interaction.completed") {693            console.log(`\n\nStatus: ${event.interaction.status}`);694            if (event.interaction.status === "requires_action") {695                console.log("Action required: provide function call results to continue.");696            }697        }698    }699700### Java701702    import com.google.genai.Client;703    import com.google.genai.gaos.models.interactions.ArgumentsDelta;704    import com.google.genai.gaos.models.interactions.CreateModelInteraction;705    import com.google.genai.gaos.models.interactions.Function;706    import com.google.genai.gaos.models.interactions.FunctionCallStep;707    import com.google.genai.gaos.models.interactions.GoogleSearch;708    import com.google.genai.gaos.models.interactions.GoogleSearchCallDelta;709    import com.google.genai.gaos.models.interactions.GoogleSearchCallStep;710    import com.google.genai.gaos.models.interactions.GoogleSearchResultStep;711    import com.google.genai.gaos.models.interactions.InteractionCompletedEvent;712    import com.google.genai.gaos.models.interactions.InteractionSSEEvent;713    import com.google.genai.gaos.models.interactions.InteractionSSEStreamEvent;714    import com.google.genai.gaos.models.interactions.InteractionSseEventInteractionStatus;715    import com.google.genai.gaos.models.interactions.InteractionsInput;716    import com.google.genai.gaos.models.interactions.Model;717    import com.google.genai.gaos.models.interactions.Step;718    import com.google.genai.gaos.models.interactions.StepDelta;719    import com.google.genai.gaos.models.interactions.StepDeltaData;720    import com.google.genai.gaos.models.interactions.StepStart;721    import com.google.genai.gaos.models.interactions.TextDelta;722    import com.google.genai.gaos.models.interactions.Tool;723    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;724    import com.google.genai.gaos.models.operations.CreateInteractionResponse;725    import com.google.genai.gaos.utils.EventStream;726    import java.util.Arrays;727    import java.util.Collections;728    import java.util.HashMap;729    import java.util.List;730    import java.util.Map;731732    Client client = new Client();733734    Map<String, Object> locationProp = new HashMap<>();735    locationProp.put("type", "string");736    locationProp.put("description", "The city and state, e.g. San Francisco, CA");737738    Map<String, Object> properties = new HashMap<>();739    properties.put("location", locationProp);740741    Map<String, Object> parameters = new HashMap<>();742    parameters.put("type", "object");743    parameters.put("properties", properties);744    parameters.put("required", Arrays.asList("location"));745746    List<Tool> tools =747        Arrays.asList(748            new GoogleSearch(),749            Function.builder()750                .name("get_weather")751                .description("Get the current weather in a given location")752                .parameters(parameters)753                .build());754755    CreateModelInteraction params =756        CreateModelInteraction.builder()757            .model(Model.of("gemini-3.8-flash"))758            .tools(tools)759            .input(760                InteractionsInput.of(761                    "Search what is the largest mountain in Europe and what the weather is there right now?"))762            .stream(true)763            .build();764765    CreateInteractionResponse response =766        client.interactions.create(CreateInteractionRequestBody.of(params));767768    try (EventStream<InteractionSSEStreamEvent> stream = response.events()) {769      for (InteractionSSEStreamEvent streamEvent : stream) {770        InteractionSSEEvent event = streamEvent.data().orElse(null);771        if (event instanceof StepStart stepStart) {772          Step step = stepStart.step().orElse(null);773          if (step != null) {774            System.out.printf("%n--- Step %d: %s ---%n", stepStart.index().orElse(0), step.type());775            if (step instanceof GoogleSearchCallStep searchCall) {776              System.out.println("  Search ID: " + searchCall.id().orElse(""));777            } else if (step instanceof GoogleSearchResultStep searchResult) {778              System.out.println("  Result for: " + searchResult.callId().orElse(""));779            } else if (step instanceof FunctionCallStep fcStep) {780              System.out.printf(781                  "  Function: %s(%s)%n",782                  fcStep.name().orElse(""), fcStep.arguments().orElse(Collections.emptyMap()));783            }784          }785        } else if (event instanceof StepDelta stepDelta) {786          StepDeltaData delta = stepDelta.delta().orElse(null);787          if (delta instanceof TextDelta textDelta) {788            textDelta.text().ifPresent(System.out::print);789          } else if (delta instanceof GoogleSearchCallDelta searchDelta) {790            System.out.println("  Queries: " + searchDelta.arguments().orElse(null));791          } else if (delta instanceof ArgumentsDelta argsDelta) {792            System.out.print("  Args chunk: " + argsDelta.arguments().orElse(""));793          }794        } else if (event instanceof InteractionCompletedEvent completedEvent) {795          completedEvent796              .interaction()797              .ifPresent(798                  interaction -> {799                    String status =800                        interaction801                            .status()802                            .map(InteractionSseEventInteractionStatus::value)803                            .orElse("");804                    System.out.println("\n\nStatus: " + status);805                    if ("requires_action".equals(status)) {806                      System.out.println("Action required: provide function call results to continue.");807                    }808                  });809        }810      }811    }812813### REST814815    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \816      -H "x-goog-api-key: $GEMINI_API_KEY" \817      -H "Content-Type: application/json" \818      --no-buffer \819      -d '{820        "model": "gemini-3.8-flash",821        "input": "Search what is the largest mountain in Europe and what the weather is there right now?",822        "stream": true,823        "tools": [824          { "type": "google_search" },825          {826            "type": "function",827            "name": "get_weather",828            "description": "Get the current weather in a given location",829            "parameters": {830              "type": "object",831              "properties": {832                "location": {833                  "type": "string",834                  "description": "The city and state, e.g. San Francisco, CA"835                }836              },837              "required": ["location"]838            }839          }840        ]841      }'842843    event: interaction.created844    data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","model":"gemini-3.8-flash"},"event_type":"interaction.created"}845846    event: interaction.status_update847    data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}848849    event: step.start850    data: {"index":0,"step":{"id":"mkutnkgn","signature":"","type":"google_search_call"},"event_type":"step.start"}851852    event: step.delta853    data: {"index":0,"delta":{"signature":"...","type":"google_search_call","arguments":{"queries":["largest mountain in Europe"]}},"event_type":"step.delta"}854855    event: step.stop856    data: {"index":0,"event_type":"step.stop"}857858    event: step.start859    data: {"index":1,"step":{"call_id":"mkutnkgn","signature":"","type":"google_search_result"},"event_type":"step.start"}860861    event: step.delta862    data: {"index":1,"delta":{"signature":"...","type":"google_search_result","is_error":false},"event_type":"step.delta"}863864    event: step.stop865    data: {"index":1,"event_type":"step.stop"}866867    event: step.start868    data: {"index":2,"step":{"type":"thought"},"event_type":"step.start"}869870    event: step.delta871    data: {"index":2,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}872873    event: step.stop874    data: {"index":2,"event_type":"step.stop"}875876    event: step.start877    data: {"index":3,"step":{"id":"ktr5aysg","type":"function_call","name":"get_weather","arguments":{}},"event_type":"step.start"}878879    event: step.delta880    data: {"index":3,"delta":{"arguments":"{\"location\":\"Mount Elbrus, Russia\"}","type":"arguments_delta"},"event_type":"step.delta"}881882    event: step.stop883    data: {"index":3,"event_type":"step.stop"}884885    event: interaction.completed886    data: {"interaction":{"id":"v1_...","status":"requires_action","usage":{"total_tokens":299,"total_input_tokens":138,"input_tokens_by_modality":[{"modality":"text","tokens":138}],"total_cached_tokens":0,"total_output_tokens":20,"total_tool_use_tokens":0,"total_thought_tokens":141},"created":"2026-05-12T17:24:26Z","updated":"2026-05-12T17:24:26Z","service_tier":"standard","object":"interaction","model":"gemini-3.8-flash"},"event_type":"interaction.completed"}887888    event: done889    data: [DONE]890891## Streaming with thinking892893When the model uses thinking, you'll receive `thought` steps with two distinct delta types: `thought_summary` (incremental text or image summary content), and `thought_signature` (an encrypted representation of the model's internal reasoning, sent as the last delta before `step.stop`). If `thinking_summaries` is enabled, `thought_summary` deltas stream a summary of the model's reasoning. For more details on thinking, see the [Thinking guide](https://ai.google.dev/gemini-api/docs/thinking).894895### Python896897    from google import genai898899    client = genai.Client()900901    stream = client.interactions.create(902        model="gemini-3.8-flash",903        input="What is the greatest common divisor of 1071 and 462?",904        generation_config={905            "thinking_summaries": "auto"906        },907        stream=True,908    )909    for event in stream:910        if event.event_type == "step.start":911            print(f"\n--- Step: {event.step.type} ---")912        elif event.event_type == "step.delta":913            if event.delta.type == "thought_summary":914                if event.delta.content.type == "text":915                    print(event.delta.content.text, end="", flush=True)916            elif event.delta.type == "text":917                print(event.delta.text, end="", flush=True)918919### JavaScript920921    import { GoogleGenAI } from "@google/genai";922923    const client = new GoogleGenAI({});924925    const stream = await client.interactions.create({926        model: "gemini-3.8-flash",927        input: "What is the greatest common divisor of 1071 and 462?",928        generation_config: {929            thinking_summaries: "auto",930        },931        stream: true,932    });933    for await (const event of stream) {934        if (event.event_type === "step.start") {935            console.log(`\n--- Step: ${event.step.type} ---`);936        } else if (event.event_type === "step.delta") {937            if (event.delta.type === "thought_summary") {938                if (event.delta.content.type === "text") {939                    process.stdout.write(event.delta.content.text);940                }941            } else if (event.delta.type === "text") {942                process.stdout.write(event.delta.text);943            }944        }945    }946947### Java948949    import com.google.genai.Client;950    import com.google.genai.gaos.models.interactions.Content;951    import com.google.genai.gaos.models.interactions.CreateModelInteraction;952    import com.google.genai.gaos.models.interactions.GenerationConfig;953    import com.google.genai.gaos.models.interactions.InteractionSSEEvent;954    import com.google.genai.gaos.models.interactions.InteractionSSEStreamEvent;955    import com.google.genai.gaos.models.interactions.InteractionsInput;956    import com.google.genai.gaos.models.interactions.Model;957    import com.google.genai.gaos.models.interactions.Step;958    import com.google.genai.gaos.models.interactions.StepDelta;959    import com.google.genai.gaos.models.interactions.StepDeltaData;960    import com.google.genai.gaos.models.interactions.StepStart;961    import com.google.genai.gaos.models.interactions.TextContent;962    import com.google.genai.gaos.models.interactions.TextDelta;963    import com.google.genai.gaos.models.interactions.ThinkingSummaries;964    import com.google.genai.gaos.models.interactions.ThoughtSummaryDelta;965    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;966    import com.google.genai.gaos.models.operations.CreateInteractionResponse;967    import com.google.genai.gaos.utils.EventStream;968969    Client client = new Client();970971    CreateModelInteraction params =972        CreateModelInteraction.builder()973            .model(Model.of("gemini-3.8-flash"))974            .input(InteractionsInput.of("What is the greatest common divisor of 1071 and 462?"))975            .generationConfig(976                GenerationConfig.builder().thinkingSummaries(ThinkingSummaries.AUTO).build())977            .stream(true)978            .build();979980    CreateInteractionResponse response =981        client.interactions.create(CreateInteractionRequestBody.of(params));982983    try (EventStream<InteractionSSEStreamEvent> stream = response.events()) {984      for (InteractionSSEStreamEvent streamEvent : stream) {985        InteractionSSEEvent event = streamEvent.data().orElse(null);986        if (event instanceof StepStart stepStart) {987          Step step = stepStart.step().orElse(null);988          if (step != null) {989            System.out.printf("%n--- Step: %s ---%n", step.type());990          }991        } else if (event instanceof StepDelta stepDelta) {992          StepDeltaData delta = stepDelta.delta().orElse(null);993          if (delta instanceof ThoughtSummaryDelta thoughtDelta) {994            Content content = thoughtDelta.content().orElse(null);995            if (content instanceof TextContent textContent) {996              textContent.text().ifPresent(System.out::print);997            }998          } else if (delta instanceof TextDelta textDelta) {999            textDelta.text().ifPresent(System.out::print);1000          }1001        }1002      }1003    }10041005### REST10061007    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \1008      -H "x-goog-api-key: $GEMINI_API_KEY" \1009      -H "Content-Type: application/json" \1010      --no-buffer \1011      -d '{1012        "model": "gemini-3.8-flash",1013        "input": "What is the greatest common divisor of 1071 and 462?",1014        "stream": true,1015        "generation_config": {1016          "thinking_summaries": "auto"1017        }1018      }'10191020    event: interaction.created1021    data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","model":"gemini-3.8-flash"},"event_type":"interaction.created"}10221023    event: interaction.status_update1024    data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}10251026    event: step.start1027    data: {"index":0,"step":{"type":"thought"},"event_type":"step.start"}10281029    event: step.delta1030    data: {"index":0,"delta":{"content":{"text":"**Implementing Euclidean Algorithm**\n\nI've just worked through a detailed example applying the Euclidean algorithm to find the GCD of 1071 and 462, confirming its step-by-step nature. The calculations went smoothly, tracking the remainders until zero. My focus is now solidifying the implementation logic, ensuring accuracy and considering potential edge cases. I'll translate this example into code.\n\n\n","type":"text"},"type":"thought_summary"},"event_type":"step.delta"}10311032    event: step.delta1033    data: {"index":0,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}10341035    event: step.stop1036    data: {"index":0,"event_type":"step.stop"}10371038    event: step.start1039    data: {"index":1,"step":{"type":"model_output"},"event_type":"step.start"}10401041    ...10421043## Streaming with agents10441045The Interactions API supports agents like Deep Research. Agents use `background=True` and return results asynchronously, but you can also stream agent interactions to receive progress updates and intermediate steps as they happen. For more details, see the [Background execution guide](https://ai.google.dev/gemini-api/docs/background-execution) and the [Deep Research guide](https://ai.google.dev/gemini-api/docs/deep-research).10461047### Python10481049    from google import genai10501051    client = genai.Client()10521053    stream = client.interactions.create(1054        agent="deep-research-preview-04-2026",1055        input="Research the latest advances in quantum computing.",1056        stream=True,1057        background=True,1058        agent_config={1059            "type": "deep-research",1060            "thinking_summaries": "auto"1061        }1062    )1063    for event in stream:1064        if event.event_type == "step.start":1065            print(f"\n--- Step: {event.step.type} ---")1066        elif event.event_type == "step.delta":1067            if event.delta.type == "text":1068                print(event.delta.text, end="", flush=True)1069            elif event.delta.type == "thought_summary":1070                if event.delta.content.type == "text":1071                    print(event.delta.content.text, end="", flush=True)1072        elif event.event_type == "interaction.completed":1073            print(f"\n\nTotal Tokens: {event.interaction.usage.total_tokens}")10741075### JavaScript10761077    import { GoogleGenAI } from "@google/genai";10781079    const client = new GoogleGenAI({});10801081    const stream = await client.interactions.create({1082        agent: "deep-research-preview-04-2026",1083        input: "Research the latest advances in quantum computing.",1084        stream: true,1085        background: true,1086        agent_config: {1087            type: "deep-research",1088            thinking_summaries: "auto"1089        }1090    });1091    for await (const event of stream) {1092        if (event.event_type === "step.start") {1093            console.log(`\n--- Step: ${event.step.type} ---`);1094        } else if (event.event_type === "step.delta") {1095            if (event.delta.type === "text") {1096                process.stdout.write(event.delta.text);1097            } else if (event.delta.type === "thought_summary") {1098                if (event.delta.content.type === "text") {1099                    process.stdout.write(event.delta.content.text);1100                }1101            }1102        } else if (event.event_type === "interaction.completed") {1103            console.log(`\n\nTotal Tokens: ${event.interaction.usage.total_tokens}`);1104        }1105    }11061107### Java11081109    import com.google.genai.Client;1110    import com.google.genai.gaos.models.interactions.Content;1111    import com.google.genai.gaos.models.interactions.CreateAgentInteraction;1112    import com.google.genai.gaos.models.interactions.DeepResearchAgentConfig;1113    import com.google.genai.gaos.models.interactions.InteractionCompletedEvent;1114    import com.google.genai.gaos.models.interactions.InteractionSSEEvent;1115    import com.google.genai.gaos.models.interactions.InteractionSSEStreamEvent;1116    import com.google.genai.gaos.models.interactions.InteractionSseEventInteraction;1117    import com.google.genai.gaos.models.interactions.InteractionsInput;1118    import com.google.genai.gaos.models.interactions.Step;1119    import com.google.genai.gaos.models.interactions.StepDelta;1120    import com.google.genai.gaos.models.interactions.StepDeltaData;1121    import com.google.genai.gaos.models.interactions.StepStart;1122    import com.google.genai.gaos.models.interactions.TextContent;1123    import com.google.genai.gaos.models.interactions.TextDelta;1124    import com.google.genai.gaos.models.interactions.ThinkingSummaries;1125    import com.google.genai.gaos.models.interactions.ThoughtSummaryDelta;1126    import com.google.genai.gaos.models.interactions.Usage;1127    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;1128    import com.google.genai.gaos.models.operations.CreateInteractionResponse;1129    import com.google.genai.gaos.utils.EventStream;11301131    Client client = new Client();11321133    CreateAgentInteraction params =1134        CreateAgentInteraction.builder()1135            .agent("deep-research-preview-04-2026")1136            .input(InteractionsInput.of("Research the latest advances in quantum computing."))1137            .stream(true)1138            .background(true)1139            .agentConfig(1140                DeepResearchAgentConfig.builder()1141                    .thinkingSummaries(ThinkingSummaries.AUTO)1142                    .build())1143            .build();11441145    CreateInteractionResponse response =1146        client.interactions.create(CreateInteractionRequestBody.of(params));11471148    try (EventStream<InteractionSSEStreamEvent> stream = response.events()) {1149      for (InteractionSSEStreamEvent streamEvent : stream) {1150        InteractionSSEEvent event = streamEvent.data().orElse(null);1151        if (event instanceof StepStart stepStart) {1152          Step step = stepStart.step().orElse(null);1153          if (step != null) {1154            System.out.printf("%n--- Step: %s ---%n", step.type());1155          }1156        } else if (event instanceof StepDelta stepDelta) {1157          StepDeltaData delta = stepDelta.delta().orElse(null);1158          if (delta instanceof TextDelta textDelta) {1159            textDelta.text().ifPresent(System.out::print);1160          } else if (delta instanceof ThoughtSummaryDelta thoughtDelta) {1161            Content content = thoughtDelta.content().orElse(null);1162            if (content instanceof TextContent textContent) {1163              textContent.text().ifPresent(System.out::print);1164            }1165          }1166        } else if (event instanceof InteractionCompletedEvent completedEvent) {1167          completedEvent1168              .interaction()1169              .flatMap(InteractionSseEventInteraction::usage)1170              .flatMap(Usage::totalTokens)1171              .ifPresent(tokens -> System.out.println("\n\nTotal Tokens: " + tokens));1172        }1173      }1174    }11751176### REST11771178    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \1179      -H "x-goog-api-key: $GEMINI_API_KEY" \1180      -H "Content-Type: application/json" \1181      --no-buffer \1182      -d '{1183        "agent": "deep-research-preview-04-2026",1184        "input": "Research the latest advances in quantum computing.",1185        "stream": true,1186        "background": true,1187        "agent_config": {1188          "type": "deep-research",1189          "thinking_summaries": "auto"1190        }1191      }'11921193    event: interaction.created1194    data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","agent":"deep-research-preview-04-2026"},"event_type":"interaction.created"}11951196    event: interaction.status_update1197    data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}11981199    event: step.start1200    data: {"index":0,"step":{"type":"thought"},"event_type":"step.start"}12011202    event: step.delta1203    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"}12041205    ... (additional thought steps) ...12061207    event: step.stop1208    data: {"index":0,"event_type":"step.stop"}12091210    event: step.start1211    data: {"index":1,"step":{"type":"model_output"},"event_type":"step.start"}12121213    event: step.delta1214    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"}12151216    event: step.stop1217    data: {"index":1,"event_type":"step.stop"}12181219    event: interaction.completed1220    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"}12211222    event: done1223    data: [DONE]12241225## Streaming image generation12261227The Interactions API supports streaming multiple output modalities simultaneously. By requesting both `text` and `image` in the `response_format`, you can receive interleaved text and generated images in the same stream.12281229The following example uses `gemini-3.1-flash-image` (Nano Banana 2) to search for information and generate a story with interleaved illustrations.12301231### Python12321233    from google import genai12341235    client = genai.Client()12361237    stream = client.interactions.create(1238        model="gemini-3.1-flash-image",1239        tools=[{"type": "google_search", "search_types": ["web_search", "image_search"]}],1240        input="Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images.",1241        response_format=[1242            {"type": "text"},1243            {"type": "image"}1244        ],1245        stream=True,1246    )12471248    for event in stream:1249        if event.event_type == "step.delta":1250            if event.delta.type == "text":1251                print(event.delta.text, end="", flush=True)1252            elif event.delta.type == "image":1253                print(f"\n[Image chunk: {len(event.delta.data)} bytes]", end="", flush=True)12541255### JavaScript12561257    import { GoogleGenAI } from "@google/genai";12581259    const client = new GoogleGenAI({});12601261    const stream = await client.interactions.create({1262        model: "gemini-3.1-flash-image",1263        tools: [{ type: "google_search", search_types: ["web_search", "image_search"] }],1264        input: "Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images.",1265        response_format: [1266            { type: "text" },1267            { type: "image" }1268        ],1269        stream: true,1270    });12711272    for await (const event of stream) {1273        if (event.event_type === "step.delta") {1274            if (event.delta.type === "text") {1275                process.stdout.write(event.delta.text);1276            } else if (event.delta.type === "image") {1277                console.log(`\n[Image chunk: ${event.delta.data.length} bytes]`);1278            }1279        }1280    }12811282### Java12831284    import com.google.genai.Client;1285    import com.google.genai.gaos.models.interactions.CreateModelInteraction;1286    import com.google.genai.gaos.models.interactions.CreateModelInteractionResponseFormat;1287    import com.google.genai.gaos.models.interactions.GoogleSearch;1288    import com.google.genai.gaos.models.interactions.GoogleSearchSearchType;1289    import com.google.genai.gaos.models.interactions.ImageDelta;1290    import com.google.genai.gaos.models.interactions.ImageResponseFormat;1291    import com.google.genai.gaos.models.interactions.InteractionSSEEvent;1292    import com.google.genai.gaos.models.interactions.InteractionSSEStreamEvent;1293    import com.google.genai.gaos.models.interactions.InteractionsInput;1294    import com.google.genai.gaos.models.interactions.Model;1295    import com.google.genai.gaos.models.interactions.ResponseFormat;1296    import com.google.genai.gaos.models.interactions.StepDelta;1297    import com.google.genai.gaos.models.interactions.StepDeltaData;1298    import com.google.genai.gaos.models.interactions.TextDelta;1299    import com.google.genai.gaos.models.interactions.TextResponseFormat;1300    import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;1301    import com.google.genai.gaos.models.operations.CreateInteractionResponse;1302    import com.google.genai.gaos.utils.EventStream;1303    import java.util.Arrays;13041305    Client client = new Client();13061307    CreateModelInteraction params =1308        CreateModelInteraction.builder()1309            .model(Model.of("gemini-3.1-flash-image"))1310            .tools(1311                Arrays.asList(1312                    GoogleSearch.builder()1313                        .searchTypes(1314                            Arrays.asList(1315                                GoogleSearchSearchType.of("web_search"),1316                                GoogleSearchSearchType.of("image_search")))1317                        .build()))1318            .input(1319                InteractionsInput.of(1320                    "Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images."))1321            .responseFormat(1322                CreateModelInteractionResponseFormat.of(1323                    Arrays.asList(1324                        ResponseFormat.of(TextResponseFormat.builder().build()),1325                        ResponseFormat.of(ImageResponseFormat.builder().build()))))1326            .stream(true)1327            .build();13281329    CreateInteractionResponse response =1330        client.interactions.create(CreateInteractionRequestBody.of(params));13311332    try (EventStream<InteractionSSEStreamEvent> stream = response.events()) {1333      for (InteractionSSEStreamEvent streamEvent : stream) {1334        InteractionSSEEvent event = streamEvent.data().orElse(null);1335        if (event instanceof StepDelta stepDelta) {1336          StepDeltaData delta = stepDelta.delta().orElse(null);1337          if (delta instanceof TextDelta textDelta) {1338            textDelta.text().ifPresent(System.out::print);1339          } else if (delta instanceof ImageDelta imageDelta) {1340            imageDelta1341                .data()1342                .ifPresent(data -> System.out.printf("%n[Image chunk: %d bytes]", data.length()));1343          }1344        }1345      }1346    }13471348### REST13491350    curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \1351      -H "x-goog-api-key: $GEMINI_API_KEY" \1352      -H "Content-Type: application/json" \1353      --no-buffer \1354      -d '{1355        "model": "gemini-3.1-flash-image",1356        "input": "Search for the history of the Colosseum and write a short illustrated story about a gladiator named Marcus. Interleave text and generated images.",1357        "stream": true,1358        "tools": [1359          { "type": "google_search",1360            "search_types": ["web_search", "image_search"]1361          }1362        ],1363        "generation_config": {1364          "thinking_summaries": "auto"1365        },1366        "response_format": [1367          { "type": "text" }, { "type": "image"}1368        ]1369      }'13701371    event: interaction.created1372    data: {"interaction":{"id":"v1_...","status":"in_progress","object":"interaction","model":"gemini-3.1-flash-image"},"event_type":"interaction.created"}13731374    event: interaction.status_update1375    data: {"interaction_id":"v1_...","status":"in_progress","event_type":"interaction.status_update"}13761377    event: step.start1378    data: {"index":0,"step":{"type":"model_output"},"event_type":"step.start"}13791380    event: step.delta1381    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"}13821383    ...13841385    event: step.stop1386    data: {"index":0,"event_type":"step.stop"}13871388    event: step.start1389    data: {"index":1,"step":{"type":"thought"},"event_type":"step.start"}13901391    event: step.delta1392    data: {"index":1,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}13931394    event: step.stop1395    data: {"index":1,"event_type":"step.stop"}13961397    event: step.start1398    data: {"index":2,"step":{"type":"model_output"},"event_type":"step.start"}13991400    event: step.delta1401    data: {"index":2,"delta":{"mime_type":"image/jpeg","data":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCg...","type":"image"},"event_type":"step.delta"}14021403    event: step.delta1404    data: {"index":2,"delta":{"text":"### Part 2: The Hypogeum and the Wait\n\n...","type":"text"},"event_type":"step.delta"}14051406    ...14071408    event: step.stop1409    data: {"index":2,"event_type":"step.stop"}14101411    event: step.start1412    data: {"index":3,"step":{"type":"thought"},"event_type":"step.start"}14131414    event: step.delta1415    data: {"index":3,"delta":{"signature":"...","type":"thought_signature"},"event_type":"step.delta"}14161417    event: step.stop1418    data: {"index":3,"event_type":"step.stop"}14191420    event: step.start1421    data: {"index":4,"step":{"type":"model_output"},"event_type":"step.start"}14221423    event: step.delta1424    data: {"index":4,"delta":{"mime_type":"image/jpeg","data":"/9j/4AAQSkZJRgABAQAAAQABAAD/...","type":"image"},"event_type":"step.delta"}14251426    event: step.delta1427    data: {"index":4,"delta":{"text":"### Part 3: The Moment of Spectacle\n\n...","type":"text"},"event_type":"step.delta"}14281429    ...14301431    event: step.stop1432    data: {"index":4,"event_type":"step.stop"}14331434    event: interaction.completed1435    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"}14361437    event: done1438    data: [DONE]14391440## Handling unknown events14411442In accordance with the API's versioning policy, new event types and delta types may be added over time. Your code should handle unknown event types gracefully---log and skip any events you don't recognize rather than throwing an error.14431444## What's next14451446- Learn more about the [Interactions API](https://ai.google.dev/gemini-api/docs/interactions-overview).1447- Explore [Function calling](https://ai.google.dev/gemini-api/docs/function-calling) with tools.1448- Learn about [Thinking](https://ai.google.dev/gemini-api/docs/thinking) for enhanced reasoning.1449- Try the [Deep Research agent](https://ai.google.dev/gemini-api/docs/deep-research) for long-running tasks.1450- See the [Interactions API reference](https://ai.google.dev/api/interactions-api) for all event types and delta types.
Files main
Menu

Theme