#!/usr/bin/env python3 """Interactions API basics (REST): create → stream (SSE) → chain with previous_interaction_id → GET → DELETE; store=false variant. STATUS: LIVE_VERIFIED 2026-09-18 (gemini-3.5-flash-lite, tmp-live/gemini-tools/h1..h9*.json, < $0.001). Observed: response keys {id, object:"interaction", status:"completed", model, created, updated, service_tier, steps[], usage}; steps = [{type:"thought", signature}, {type:"model_output", content:[{type:"text", text}]}]; SSE events in order: interaction.created, interaction.status_update, step.start, step.delta, step.stop (per step), interaction.completed; store=false → no `id` in the response; GET after DELETE → 404 "Requested entity was not found"; GET /v1beta/interactions (list) → 404. Run: .venv/bin/python examples/gemini/interactions/interactions_basic.py """ import json, sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[3])) from scripts.live import gemini_request MODEL = "gemini-3.5-flash-lite" st, r, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Reply with OK.", "generation_config": {"max_output_tokens": 16}}, note="interactions_basic.py create") iid = r["id"] print("create:", st, r["status"], [s["type"] for s in r["steps"]], "usage:", r["usage"]["total_tokens"]) st, lines, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Reply with OK.", "stream": True, "previous_interaction_id": iid, "generation_config": {"max_output_tokens": 16}}, stream=True, note="interactions_basic.py stream") events = [json.loads(ln[5:]) for ln in lines if ln.startswith("data:") and ln[5:].strip() != "[DONE]"] # terminal frame: event: done / data: [DONE] print("stream:", st, [e["event_type"] for e in events]) print(" deltas:", [e["delta"] for e in events if e["event_type"] == "step.delta" and e["delta"]["type"] == "text"]) iid2 = events[-1]["interaction"]["id"] st, r3, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "What did I ask you first? 5 words.", "previous_interaction_id": iid2, "generation_config": {"max_output_tokens": 24}}, note="interactions_basic.py chain") print("chain:", st, " ".join(c["text"] for s in r3["steps"] if s["type"] == "model_output" for c in s["content"] if c["type"] == "text")) st, g, _ = gemini_request("GET", f"/v1beta/interactions/{iid}", note="interactions_basic.py get") print("get:", st, sorted(g.keys())) st, r5, _ = gemini_request("POST", "/v1beta/interactions", {"model": MODEL, "input": "Reply with OK.", "store": False, "generation_config": {"max_output_tokens": 8}}, note="interactions_basic.py store=false") print("store=false:", st, "id present:", "id" in r5) for i in (iid, iid2, r3["id"]): st, _, _ = gemini_request("DELETE", f"/v1beta/interactions/{i}", note="interactions_basic.py delete") print("delete", i[:10] + "…", st)