adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Session disconnects with 1011 error when session_resumption is configured.

Open kmckenna66 opened this issue 7 months ago • 5 comments

🔴 Required Information

Please ensure all items in this section are completed to allow for efficient triaging. Requests without complete information may be rejected / deprioritized. If an item is not applicable to you - please mark it as N/A

Describe the Bug: When connecting to an adk agent in Agent Engine with bidi_stream_query and session_resumption configured, the session does not reconnect after the 10 minute timeout and gives a 1011 error.

Steps to Reproduce: Please provide a numbered list of steps to reproduce the behavior:

  1. Deploy an agent to Agent Engine using gemini-live-2.5-flash-native-audio.
  2. Start api service connecting to your remote agent. uv run python -m web_api.expose_app --host 0.0.0.0 --port 8000 --mode remote --remote-id {remote_id}
  3. Start session and wait ~10 minutes.

Expected Behavior: Sessions are able to continue after 10 minutes.

Observed Behavior: Session closes with this error. 2026-04-28 09:55:33,071 - main - ERROR - receive from Vertex: received 1011 (internal error) Request trace id: c74aa5a1c5f0bc12, [ORIGINAL ERROR] generic::unknown: Reasoning Engine Execution failed.

Environment Details:

  • ADK Library Version (pip show google-adk): 2.0.0b1
  • Desktop OS: MacOS
  • Python Version (python -V): 3.13.3

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: gemini-live-2.5-flash-native-audio

🟡 Optional Information

Providing this information greatly speeds up the resolution process.

Regression: Did this work in a previous version of ADK? If so, which one? No

Logs: Please attach relevant logs. Wrap them in code blocks (```) or attach a text file.

// Paste logs here

Screenshots / Video: If applicable, add screenshots or screen recordings to help explain your problem.

Additional Context: Add any other context about the problem here.

Minimal Reproduction Code: Please provide a code snippet or a link to a Gist/repo that isolates the issue.

from __future__ import annotations

import asyncio
import json
import logging
import os
import re
import uuid
from pathlib import Path
from typing import Any

import google.auth
import vertexai
from fastapi import FastAPI, WebSocket
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from google.adk.agents.run_config import RunConfig, StreamingMode
from google.genai import types
from websockets.exceptions import ConnectionClosedError

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

app.state.config = {
    "project_id": None,
    "location": "us-central1",
    "remote_agent_engine_id": None,
}

_ADK_TO_SCHEMA_FIELD_MAP: dict[str, str] = {
    "input_transcription": "inputTranscription",
    "output_transcription": "outputTranscription",
    "turn_complete": "turnComplete",
    "usage_metadata": "usageMetadata",
}

_USAGE_METADATA_FIELD_MAP: dict[str, str] = {
    "prompt_token_count": "promptTokenCount",
    "candidates_token_count": "candidatesTokenCount",
    "total_token_count": "totalTokenCount",
    "prompt_tokens_details": "promptTokensDetails",
}

_SESSION_DISCONNECTED_CLIENT_MESSAGE = (
    "Your session was disconnected. Please refresh the page and try again."
)


def reasoning_engine_resource_name(
    project_id: str,
    location: str,
    remote_agent_engine_id: str,
) -> str:
    rid = remote_agent_engine_id.strip()
    if rid.startswith("projects/"):
        return rid
    if rid.startswith("reasoningEngines/"):
        return f"projects/{project_id}/locations/{location}/{rid}"
    return f"projects/{project_id}/locations/{location}/reasoningEngines/{rid}"


def _vertex_sdk_object_to_log_dict(obj: Any) -> Any:
    if obj is None:
        return None
    if isinstance(obj, dict):
        return obj
    model_dump = getattr(obj, "model_dump", None)
    if callable(model_dump):
        try:
            return model_dump(mode="json", default=str)
        except TypeError:
            try:
                return model_dump()
            except Exception:
                pass
    return repr(obj)


def extract_session_id_from_vertex_session_response(response: Any) -> str | None:
    if response is None:
        return None
    data = _vertex_sdk_object_to_log_dict(response)
    if not isinstance(data, dict):
        return None
    for key in ("id", "session_id", "sessionId"):
        val = data.get(key)
        if isinstance(val, str) and val.strip():
            return val.strip()
    name = data.get("name")
    if isinstance(name, str) and name.strip():
        tail = name.rstrip("/").split("/")[-1]
        if tail:
            return tail
    return None


def _create_remote_vertex_session_sync(
    *,
    project_id: str,
    location: str,
    remote_agent_engine_id: str,
    user_id: str,
    session_state: dict[str, Any] | None,
) -> tuple[str | None, dict[str, Any]]:
    vertexai.init(project=project_id, location=location)
    client = vertexai.Client(project=project_id, location=location)
    resource_name = reasoning_engine_resource_name(
        project_id, location, remote_agent_engine_id
    )
    create_config: dict[str, Any] = {"wait_for_completion": True}
    if session_state is not None:
        create_config["session_state"] = session_state

    op = client.agent_engines.sessions.create(
        name=resource_name,
        user_id=str(user_id),
        config=create_config,
    )
    response_obj = getattr(op, "response", None)
    extracted = extract_session_id_from_vertex_session_response(response_obj)
    log_bundle = {
        "reasoning_engine_name": resource_name,
        "user_id": str(user_id),
        "create_session_response": _vertex_sdk_object_to_log_dict(response_obj),
        "extracted_session_id": extracted,
    }
    return extracted, log_bundle


async def create_remote_vertex_session_for_user(
    *,
    project_id: str,
    location: str,
    remote_agent_engine_id: str,
    user_id: str,
    session_state: dict[str, Any] | None,
) -> tuple[str | None, dict[str, Any]]:
    return await asyncio.to_thread(
        _create_remote_vertex_session_sync,
        project_id=str(project_id),
        location=str(location),
        remote_agent_engine_id=str(remote_agent_engine_id),
        user_id=str(user_id),
        session_state=session_state,
    )


def _normalize_event_fields(event: dict) -> dict:
    normalized = {_ADK_TO_SCHEMA_FIELD_MAP.get(k, k): v for k, v in event.items()}
    um = normalized.get("usageMetadata")
    if isinstance(um, dict):
        normalized["usageMetadata"] = {
            _USAGE_METADATA_FIELD_MAP.get(k, k): v for k, v in um.items()
        }
    return normalized


def _unwrap_stream_response_envelope(response: dict[str, Any]) -> dict[str, Any]:
    bidi_output = response.get("bidiStreamOutput")
    if isinstance(bidi_output, dict):
        return bidi_output
    out = response.get("output")
    if isinstance(out, dict):
        nested = out.get("bidiStreamOutput")
        if isinstance(nested, dict):
            return nested
        return out
    if isinstance(out, str):
        try:
            parsed = json.loads(out)
        except json.JSONDecodeError:
            return response
        if isinstance(parsed, dict):
            return _unwrap_stream_response_envelope(parsed)
    return response


def _format_remote_log(obj: Any) -> str:
    try:
        return json.dumps(obj, default=str, ensure_ascii=False, indent=2)
    except (TypeError, ValueError):
        return repr(obj)


def _is_vertex_live_session_cancelled_exception(exc: BaseException, _depth: int = 0) -> bool:
    if _depth > 6:
        return False
    msg = str(exc).lower()
    name = type(exc).__name__
    if "operation was cancelled" in msg or "operation was canceled" in msg:
        return True
    if "connectionclosed" in name.lower() and "cancel" in msg:
        return True
    if "1000" in str(exc) and "cancel" in msg:
        return True
    inner = exc.__cause__
    if isinstance(inner, BaseException):
        return _is_vertex_live_session_cancelled_exception(inner, _depth + 1)
    return False


def _build_run_config_dict(
    model_name: str,
    *,
    proactivity: bool = True,
) -> dict[str, Any]:
    session_resumption = types.SessionResumptionConfig()
    is_native_audio = "native-audio" in model_name.lower()
    if is_native_audio:
        input_transcription = types.AudioTranscriptionConfig()
        output_transcription = types.AudioTranscriptionConfig()
        run_config = RunConfig(
            streaming_mode=StreamingMode.BIDI,
            response_modalities=["AUDIO"],
            input_audio_transcription=input_transcription,
            output_audio_transcription=output_transcription,
            session_resumption=session_resumption,
            context_window_compression=types.ContextWindowCompressionConfig(
                trigger_tokens=100000,
                sliding_window=types.SlidingWindow(target_tokens=80000),
            ),
            proactivity=(
                types.ProactivityConfig(proactive_audio=True) if proactivity else None
            ),
            speech_config=types.SpeechConfig(
                language_code="en-US",
                voice_config=types.VoiceConfig(
                    prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name="Zephyr"),
                ),
            ),
        )
    else:
        run_config = RunConfig(
            streaming_mode=StreamingMode.BIDI,
            response_modalities=["TEXT"],
            input_audio_transcription=None,
            output_audio_transcription=None,
            session_resumption=session_resumption,
            context_window_compression=types.ContextWindowCompressionConfig(
                trigger_tokens=100000,
                sliding_window=types.SlidingWindow(target_tokens=80000),
            ),
        )
    return run_config.model_dump(exclude_none=True, by_alias=False, mode="json")


def _coerce_user_id(raw: Any) -> str:
    s = str(raw).strip() if raw is not None else ""
    return s if s else uuid.uuid4().hex


def _first_turn_from_setup(data: dict[str, Any]) -> dict[str, Any] | None:
    if data.get("type") == "text" and "text" in data:
        return {
            "content": {
                "role": "user",
                "parts": [{"text": str(data.get("text", ""))}],
            }
        }
    content = data.get("content")
    if isinstance(content, dict) and content.get("role") == "user":
        return {"content": content}
    setup_inner = data["setup"] if isinstance(data.get("setup"), dict) else {}
    im = setup_inner.get("initialMessage")
    if isinstance(im, str) and im.strip():
        return {"content": {"role": "user", "parts": [{"text": im}]}}
    ft = setup_inner.get("firstTurn")
    if isinstance(ft, str) and ft.strip():
        return {"content": {"role": "user", "parts": [{"text": ft}]}}
    if isinstance(ft, dict) and ft.get("role") == "user":
        return {"content": ft}
    return None


class RemoteBidiProxy:
    """Queue-based WebSocket client ↔ Vertex Live ``bidi_stream_query``."""

    def __init__(
        self,
        websocket: WebSocket,
        *,
        project_id: str,
        location: str,
        remote_agent_engine_id: str,
    ) -> None:
        self.websocket = websocket
        self.project_id = project_id
        self.location = location
        self.remote_agent_engine_id = remote_agent_engine_id
        self.model_name = (os.environ.get("AGENT_MODEL") or "").strip() or "gemini-live-2.5-flash-native-audio"
        self.input_queue: asyncio.Queue[Any] = asyncio.Queue()
        self._session_ready = False

    async def _send_to_client(self, response: Any) -> None:
        if not isinstance(response, dict):
            await self.websocket.send_json(response)
            return
        transformed = _unwrap_stream_response_envelope(response)
        if not transformed:
            return
        normalized = (
            _normalize_event_fields(transformed)
            if isinstance(transformed, dict)
            else transformed
        )
        logger.debug("Remote bidi → client:\n%s", _format_remote_log(normalized))
        await self.websocket.send_json(normalized)

    async def receive_from_client(self) -> None:
        while True:
            try:
                message = await self.websocket.receive()
                if message.get("type") == "websocket.disconnect":
                    break
                if "text" not in message:
                    if "bytes" in message:
                        if not self._session_ready:
                            await self.websocket.send_json(
                                {"error": "Send setup before binary frames"}
                            )
                            continue
                        await self.input_queue.put({"binary_data": message["bytes"]})
                    continue
                data = json.loads(message["text"])
                if not isinstance(data, dict):
                    continue

                if "setup" in data:
                    setup_inner = data["setup"] if isinstance(data.get("setup"), dict) else {}
                    user_id = _coerce_user_id(
                        data.get("user_id") or setup_inner.get("user_id")
                    )
                    run_config = _build_run_config_dict(self.model_name)
                    setup_state = setup_inner.get("state")
                    state: dict[str, Any] = (
                        dict(setup_state) if isinstance(setup_state, dict) else {}
                    )

                    session_id: str | None = None
                    try:
                        extracted_id, bundle = await create_remote_vertex_session_for_user(
                            project_id=self.project_id,
                            location=self.location,
                            remote_agent_engine_id=self.remote_agent_engine_id,
                            user_id=user_id,
                            session_state=state if state else None,
                        )
                        logger.info(
                            "Vertex sessions.create ok session_id=%r user_id=%s",
                            extracted_id,
                            user_id,
                        )
                        logger.debug(
                            "Vertex sessions.create payload:\n%s",
                            _format_remote_log(bundle),
                        )
                        session_id = extracted_id
                    except Exception as e:
                        logger.exception("sessions.create failed: %s", e)
                        await self.websocket.send_json(
                            {"error": f"Session initialization failed: {e!s}"}
                        )
                        continue

                    if not session_id:
                        await self.websocket.send_json(
                            {"error": "Vertex did not return a session id"}
                        )
                        continue

                    init_msg: dict[str, Any] = {
                        "user_id": user_id,
                        "run_config": run_config,
                        "session_id": session_id,
                        "state": state,
                    }
                    await self.input_queue.put(init_msg)
                    self._session_ready = True

                    first = _first_turn_from_setup(data)
                    if first is not None:
                        await self.input_queue.put(first)
                    continue

                if not self._session_ready:
                    await self.websocket.send_json(
                        {"error": "Send WebSocket setup with user_id before messages"}
                    )
                    continue

                if data.get("type") == "text":
                    await self.input_queue.put(
                        {
                            "content": {
                                "role": "user",
                                "parts": [{"text": data.get("text", "")}],
                            }
                        }
                    )
                    continue

                to_put = data.get("live_request", data)
                await self.input_queue.put(to_put)

            except ConnectionClosedError as e:
                logger.warning("Client closed: %s", e)
                break
            except json.JSONDecodeError as e:
                logger.error("Invalid JSON from client: %s", e)
                break
            except Exception as e:
                logger.error("receive_from_client: %s", e)
                break

    async def run_remote_bidi(self) -> None:
        client = vertexai.Client(project=self.project_id, location=self.location)
        try:
            async with client.aio.live.agent_engines.connect(
                agent_engine=self.remote_agent_engine_id,
                config={"class_method": "bidi_stream_query"},
            ) as session:
                logger.info(
                    "Remote bidi_stream_query connected (engine=%s)",
                    self.remote_agent_engine_id,
                )
                await self.websocket.send_json({"setupComplete": {}})

                async def forward() -> None:
                    while True:
                        try:
                            item = await self.input_queue.get()
                            await session.send(item)
                        except Exception as e:
                            logger.error("forward to Vertex: %s", e)
                            break

                async def receive() -> None:
                    while True:
                        try:
                            response = await session.receive()
                            if response is not None:
                                await self._send_to_client(response)
                            if isinstance(response, dict) and response.get("error"):
                                logger.error("Remote error: %s", response["error"])
                                break
                        except Exception as e:
                            logger.error("receive from Vertex: %s", e)
                            break

                await asyncio.gather(forward(), receive())
        except Exception as e:
            if _is_vertex_live_session_cancelled_exception(e):
                logger.warning("Live session cancelled: %s", e)
                try:
                    await self.websocket.send_json(
                        {
                            "error": _SESSION_DISCONNECTED_CLIENT_MESSAGE,
                            "sessionDisconnected": True,
                        }
                    )
                except Exception:
                    pass
            else:
                logger.exception("run_remote_bidi: %s", e)
                try:
                    await self.websocket.send_json({"error": str(e)})
                except Exception:
                    pass


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket) -> None:
    cfg = app.state.config
    pid = cfg.get("project_id")
    loc = cfg.get("location") or "us-central1"
    rid = cfg.get("remote_agent_engine_id")
    if not pid or not rid:
        # 4401: application-specific WebSocket close (config missing before accept)
        await websocket.close(code=4401)
        return
    await websocket.accept()
    proxy = RemoteBidiProxy(
        websocket,
        project_id=str(pid),
        location=str(loc),
        remote_agent_engine_id=str(rid),
    )
    await asyncio.gather(
        proxy.receive_from_client(),
        proxy.run_remote_bidi(),
    )


@app.get("/")
async def root() -> dict[str, str]:
    return {"service": "minimal-vertex-bidi-proxy", "mode": "remote_bidi_stream_query"}


@app.get("/health")
async def health() -> dict[str, str]:
    return {"status": "ok"}


class SessionBootstrapRequest(BaseModel):
    """Body shape from ``web_app`` SidePanel ``startSessionAndConnect``."""

    activePageContext: str | None = None
    sailorProfile: str | None = None
    sessionContext: str | None = None
    userId: str | None = None


@app.post("/api/session")
async def create_session(payload: SessionBootstrapRequest) -> dict[str, Any]:
    """Bootstrap before WebSocket: UI expects 200 + ``state`` for ``connect(..., sessionData.state)``.

    Session creation and Vertex ``sessions.create`` run on the WebSocket ``setup`` message;
    this endpoint only supplies initial ``state`` (empty here; extend to load mock payloads if needed).
    """
    raw = (payload.userId or "").strip()
    user_id = raw if raw else uuid.uuid4().hex
    return {
        "user_id": user_id,
        "state": {},
        "session_id": None,
    }


@app.post("/feedback")
async def post_feedback() -> dict[str, str]:
    """No-op sink for demo UIs that POST feedback (avoids 404); replace with real storage if needed."""
    return {"status": "accepted"}


if __name__ == "__main__":
    import argparse

    import uvicorn

    parser = argparse.ArgumentParser(
        description="Remote Agent Engine proxy (bidi_stream_query only)"
    )
    parser.add_argument(
        "--remote-id",
        type=str,
        default=os.environ.get("REMOTE_AGENT_ENGINE_ID", "").strip(),
        help="Agent engine id or full projects/.../reasoningEngines/ID",
    )
    parser.add_argument("--project-id", type=str, default="")
    parser.add_argument("--location", type=str, default="us-central1")
    parser.add_argument("--host", type=str, default="localhost")
    parser.add_argument("--port", type=int, default=8000)
    parser.add_argument("--mode", type=str, default="remote")

    args = parser.parse_args()
    remote_id = args.remote_id

    project_id = args.project_id.strip()
    if not project_id:
        m = re.match(
            r"projects/([^/]+)/locations/([^/]+)/reasoningEngines/",
            remote_id,
        )
        if m:
            project_id = m.group(1)
            args.location = m.group(2) if args.location == "us-central1" else args.location
        else:
            try:
                _, project_id = google.auth.default()
            except Exception as e:
                parser.error(f"Could not determine project id: {e}")

    if not os.environ.get("AGENT_MODEL"):
        logger.warning("AGENT_MODEL unset; defaulting to native-audio-style RunConfig branch")

    app.state.config = {
        "project_id": project_id,
        "location": args.location,
        "remote_agent_engine_id": remote_id,
    }
    logger.info(
        "Starting remote bidi proxy project=%s location=%s engine=%s",
        project_id,
        args.location,
        remote_id,
    )
    uvicorn.run(app, host=args.host, port=args.port)

How often has this issue occurred?:

  • Always (100%)

kmckenna66 avatar Apr 28 '26 15:04 kmckenna66

Debugging Logs: expose_app_20260428_154558_utc.log

kmckenna66 avatar Apr 28 '26 16:04 kmckenna66

Thanks for the ongoing investigation here. We did another round of focused testing to separate the documented 10-minute Agent Engine Bidi timeout from the session-resumption recovery behavior.

We understand the 10-minute maximum for Agent Engine bidirectional streaming queries is documented, so we are not treating the timeout itself as the bug. The open question is the intended recovery contract for long-running Live/Bidi sessions on Agent Engine, especially when session_resumption is configured.

Detailed repro and mitigation script:

https://gist.github.com/smwitkowski/25af4cd21a589cee2e6eaf9605ba9a84

What we reproduced

Using a minimal ADK Live Agent Engine resource with:

  • bidi_stream_query
  • gemini-live-2.5-flash-native-audio
  • SessionResumptionConfig() configured
  • google-adk==1.32.0
  • google-cloud-aiplatform[agent_engines]==1.148.1
  • google-genai==1.73.1
  • Python 3.12

we reproduced the issue shape:

  • Agent Engine emitted resumable live_session_resumption_update handles.
  • Resource logs recorded goAway and an ADK inner reconnect attempt.
  • The outer Agent Engine Bidi stream still closed at ~600s.
  • The client received 1011 / Reasoning Engine Execution failed.

We also deployed a dummy Agent Engine Bidi app that does not call ADK or Gemini Live. That app also closed around 600s. Active client traffic every 15 seconds did not extend the stream. That confirms the 600s boundary is the Agent Engine Bidi query lifetime, not Ruby/frontend/API idleness and not caused by Gemini Live or ADK application logic.

Mitigation test that worked

We then tested a proxy-owned reconnect pattern:

  1. Open an Agent Engine Bidi stream.
  2. Send normal init with SessionResumptionConfig().
  3. Send a prompt containing a unique codeword.
  4. Cache live_session_resumption_update.new_handle.
  5. Let the first Agent Engine Bidi stream close with the real 1011.
  6. Open a second Agent Engine Bidi stream.
  7. Replay init with SessionResumptionConfig(handle=<cached>, transparent=True).
  8. Send a follow-up prompt asking for the codeword.

That succeeded. In the timeout-mode run:

  • first stream captured handle hash ebc34c282c4b
  • first stream closed at elapsed 600.942s with 1011
  • request trace id: 7888bf9c87f33d5b
  • second stream opened at elapsed 601.108s
  • second stream used SessionResumptionConfig(handle=<cached>, transparent=True)
  • resumed stream returned the remembered codeword RUBY-RESUME-5E9F34
  • resumed stream reached turn_complete
  • no second exception occurred

Matching resource logs showed:

  • first stream goAway
  • ADK inner reconnect attempt
  • outer Bidi handler exit
  • immediate second Bidi request
  • new handle updates
  • Stream finished successfully for class method bidi_stream_query

Smallest mitigation shape

This is the core pattern we tested:

def build_run_config(handle: str | None = None) -> dict:
    return RunConfig(
        streaming_mode=StreamingMode.BIDI,
        response_modalities=["AUDIO"],
        session_resumption=types.SessionResumptionConfig(
            handle=handle,
            transparent=True if handle else None,
        ),
    ).model_dump(exclude_none=True, by_alias=False, mode="json")


async def open_bidi(handle: str | None = None):
    conn = await client.aio.live.agent_engines.connect(
        agent_engine=agent_engine_resource,
        config={"class_method": "bidi_stream_query"},
    ).__aenter__()

    await conn.send({
        "user_id": user_id,
        "session_id": session_id,
        "state": {},
        "run_config": build_run_config(handle),
    })
    return conn


cached_handle = None
conn = await open_bidi()

try:
    while True:
        response = await conn.receive()
        handle = extract_live_session_resumption_handle(response)
        if handle:
            cached_handle = handle
except ConnectionClosedError:
    if not cached_handle:
        raise

    # Recreate the Agent Engine outer Bidi stream and resume Live state.
    conn = await open_bidi(cached_handle)
    await conn.send({"content": {"role": "user", "parts": [{"text": "continue"}]}})

For an application proxy, the browser WebSocket can stay open while the upstream Agent Engine Bidi stream is treated as a 10-minute transport that is recreated with the cached handle.

Questions

Can you confirm the intended/supported contract here?

  1. Is recreating the Agent Engine Bidi stream with SessionResumptionConfig(handle=<cached>, transparent=True) the intended way to handle the documented 10-minute Bidi query limit?
  2. Should clients proactively rotate the Bidi stream before 10 minutes, or wait for the platform close and reconnect?
  3. Should the documented timeout surface as a graceful close / terminal event rather than 1011 / Reasoning Engine Execution failed?
  4. Is the Live resumption handle guaranteed to be valid across a new Agent Engine outer Bidi stream?
  5. Should ADK session resumption inside Agent Engine be expected to keep the original outer Bidi stream alive, or is external stream recreation the intended design?

One possible SDK/ADK-level improvement would be an official reconnect wrapper or handler around client.aio.live.agent_engines.connect(...) that caches Live resumption handles, recreates the outer Agent Engine Bidi stream at the documented boundary, and surfaces a graceful lifecycle event instead of a generic 1011.

smwitkowski avatar May 08 '26 03:05 smwitkowski

Questions

Can you confirm the intended/supported contract here?

  1. Is recreating the Agent Engine Bidi stream with SessionResumptionConfig(handle=<cached>, transparent=True) the intended way to handle the documented 10-minute Bidi query limit?

Yes, this is my understanding of the live API's session resumption as documented here: https://ai.google.dev/gemini-api/docs/live-api/session-management#session-resumption

  1. Should clients proactively rotate the Bidi stream before 10 minutes, or wait for the platform close and reconnect?

Based on https://ai.google.dev/gemini-api/docs/live-api/session-management#goaway-message, clients should generally not need to proactively rotate, but if long-running/expensive operations are being launched on the client they would likely want to monitor the messages about time left and then consider proactive rotation if needed.

  1. Should the documented timeout surface as a graceful close / terminal event rather than 1011 / Reasoning Engine Execution failed?

Yes, this is likely a gap in the AE SDK's exposure of error messages. I'll file a bug to track improving the ease of use here.

  1. Is the Live resumption handle guaranteed to be valid across a new Agent Engine outer Bidi stream?

AFAIK, that is entirely the responsibility of the Live API - so I would say yes. From AE's perspective we are just a proxy to the agent so whatever guarantees the underlying API has will continue to hold (i.e., it should not change anything if you are connecting to a locally running agent or via AE).

  1. Should ADK session resumption inside Agent Engine be expected to keep the original outer Bidi stream alive, or is external stream recreation the intended design?> > One possible SDK/ADK-level improvement would be an official reconnect wrapper or handler around client.aio.live.agent_engines.connect(...) that caches Live resumption handles, recreates the outer Agent Engine Bidi stream at the documented boundary, and surfaces a graceful lifecycle event instead of a generic 1011.

My reading of https://ai.google.dev/gemini-api/docs/live-api/session-management#session-resumption is that Live API explicitly makes this the responsibility of the client.

From Agent Engine's point of view, we want to be able to support bidi websocket communication between clients and arbitrary deployed agents. Our new bidi API (coming this week) will make it easier to establish bidi websocket streams with custom agents and agent's exposing the run_live ADK API, but right now that will require clients to connect via a websocket library rather than using the SDK client.aio.live.agent_engines.connect methods.

Incorporating support for the new more-flexible bidi API into the SDK will give us the opportunity to consider improving the usability of session resumption as well. We don't want to be in the position of wrapping the ADK Live API too tightly (the API would ideally be the same if calling through AE or locally), but the feedback here is very helpful in terms of thinking about where we can make things easier to use as the new API is refined.

I'll also add that my perspective here is from the Agent Engine networking side of things, rather than specific expertise with the session resumption API, so if there's aspects of this question or gaps in our API that I'm overlooking please let me know. Thank you.

ericgribkoff avatar May 11 '26 17:05 ericgribkoff

Thanks, Eric. This is very helpful and largely confirms the contract we were trying to pin down.

Based on your reply, we’ll treat the Agent Engine Bidi connection as a renewable outer transport and handle Live resumption at the client/proxy layer:

  1. cache the latest live_session_resumption_update.new_handle
  2. keep the browser/client connection open
  3. recreate the outer Agent Engine Bidi stream when it closes
  4. replay init with SessionResumptionConfig(handle=<cached>, transparent=True)
  5. continue forwarding traffic on the resumed stream

That matches the mitigation we validated in the repro: after the first Agent Engine Bidi stream closed at the ~600s boundary with 1011, opening a second stream with the cached handle resumed the Live session and preserved context.

One remaining practical question: in our repro, goAway was visible in the Agent Engine resource logs, but the current client.aio.live.agent_engines.connect(...) client path still surfaced the outer close as 1011 / Reasoning Engine Execution failed. Is goAway expected to be observable by the client on the current Agent Engine Bidi SDK path, or should clients primarily rely on reactive reconnect after the close until the newer Bidi API / SDK support lands?

We’ll also watch for the new more-flexible Bidi API you mentioned. It would be useful to know whether that API is expected to expose cleaner lifecycle events for:

  • goAway / time-left
  • graceful terminal close reason
  • resumption handle updates
  • reconnect/resume success or failure

Thanks again. This gives us enough confidence to implement the proxy-owned reconnect path while tracking the SDK/API usability improvements separately.

smwitkowski avatar May 11 '26 21:05 smwitkowski

One remaining practical question: in our repro, goAway was visible in the Agent Engine resource logs, but the current client.aio.live.agent_engines.connect(...) client path still surfaced the outer close as 1011 / Reasoning Engine Execution failed. Is goAway expected to be observable by the client on the current Agent Engine Bidi SDK path, or should clients primarily rely on reactive reconnect after the close until the newer Bidi API / SDK support lands?

I believe that the goAway is not currently observable by the client with the current AE SDK path. Assuming your client will be reconnecting regardless, I would expect the pattern to just be try to reconnect with the session resumption token on any failure. I've filed a separate bug for myself to track better surfacing of this - and potentially other - error messages via the AE SDK.

We’ll also watch for the new more-flexible Bidi API you mentioned. It would be useful to know whether that API is expected to expose cleaner lifecycle events for:

  • goAway / time-left
  • graceful terminal close reason
  • resumption handle updates
  • reconnect/resume success or failure

Thanks again. This gives us enough confidence to implement the proxy-owned reconnect path while tracking the SDK/API usability improvements separately.

Right now, the new bidi API is exactly just the websocket API exposed on the Agent, so for this use case, it would be the same /run_live websocket endpoint that you would see if running a Live agent locally with uvicorn or the like. There's not yet any integration with the client SDK for consuming this API, so no immediate help with automatic reconnection or resumption - on the other hand, there is no SDK to "swallow" any errors, so your websocket client would directly see the goAway or other errors emanating from the server side. The existing bidi API was aimed more or less directly at the run_live API and equivalent, whereas the new API is a bit more general (can support any websocket-based server). But unifying those as much as possible will also give me the chance to see how well we can clean up things like this usability issue with session resumption.

Thanks again!

ericgribkoff avatar May 12 '26 17:05 ericgribkoff