From 845e1377aba42394ef37d9ea9d5088d8c4c2716f Mon Sep 17 00:00:00 2001 From: Nikhil Pareek Date: Thu, 15 Oct 2026 19:51:18 -0700 Subject: [PATCH 1/2] feat(google-genai-live): Gemini Live session instrumentor (TH-8243) New package traceAI-google-genai-live: wraps google-genai AsyncLive.connect and the AsyncSession send_client_content / send_realtime_input / send_tool_response / receive methods (Gemini Developer API, google-genai >=2.28,<3) so one Live session is one trace: setup, user turns, model turns with model and tokens, tool calls with cancellation, interruptions, goAway, and a resume with the stored handle as a reconnect span in the same trace. Audio sessions get a parentless CONVERSATION root with call.* and conversation.transcript* so Future AGI lists them as calls; text sessions are CHAIN. Audio bytes, API keys and resumption handles never land on a span. Text content is on by default; metadata_only / TraceConfig hide_inputs / hide_outputs drop it. Vertex Live, ephemeral tokens, Live.send and Live.start_stream are not wrapped. Tests run the real SDK against a scripted socket, including frames recorded from real gemini-3.8-live sessions on 2026-10-15. Co-Authored-By: Claude Opus 5.5 --- .../frameworks/google-genai-live/CHANGELOG.md | 19 + python/frameworks/google-genai-live/README.md | 147 ++++ .../google-genai-live/examples/README.md | 26 + .../google-genai-live/examples/run_e2e.sh | 24 + .../google-genai-live/examples/voice_tool.py | 131 +++ .../google-genai-live/pyproject.toml | 48 ++ .../google-genai-live/tests/__init__.py | 0 .../google-genai-live/tests/_voice_tab.py | 57 ++ .../google-genai-live/tests/conftest.py | 174 ++++ .../fixtures/flag_generation_complete.json | 28 + .../tests/fixtures/flag_interrupted.json | 33 + .../tests/fixtures/flag_turn_complete.json | 28 + .../tests/fixtures/j1_realtime_audio.json | 119 +++ .../tests/fixtures/j2_tool.json | 119 +++ .../tests/fixtures/j3_interrupt_cancel.json | 53 ++ .../tests/fixtures/j4_first_connection.json | 65 ++ .../tests/fixtures/j4_second_connection.json | 55 ++ .../tests/fixtures/j6_socket_death.json | 28 + .../tests/fixtures/j8_setup_rejected.json | 7 + .../fixtures/real_20261015_audio_tool.json | 279 +++++++ .../fixtures/real_20261015_text_tool.json | 289 +++++++ .../tests/fixtures/text_session.json | 43 + .../google-genai-live/tests/test_fence.py | 172 ++++ .../google-genai-live/tests/test_privacy.py | 128 +++ .../google-genai-live/tests/test_resume.py | 132 +++ .../google-genai-live/tests/test_session.py | 347 ++++++++ .../traceai_google_genai_live/__init__.py | 138 ++++ .../traceai_google_genai_live/_connect.py | 338 ++++++++ .../traceai_google_genai_live/_redact.py | 169 ++++ .../traceai_google_genai_live/_session.py | 769 ++++++++++++++++++ .../traceai_google_genai_live/package.py | 2 + .../traceai_google_genai_live/py.typed | 0 .../traceai_google_genai_live/version.py | 1 + 33 files changed, 3968 insertions(+) create mode 100644 python/frameworks/google-genai-live/CHANGELOG.md create mode 100644 python/frameworks/google-genai-live/README.md create mode 100644 python/frameworks/google-genai-live/examples/README.md create mode 100755 python/frameworks/google-genai-live/examples/run_e2e.sh create mode 100644 python/frameworks/google-genai-live/examples/voice_tool.py create mode 100644 python/frameworks/google-genai-live/pyproject.toml create mode 100644 python/frameworks/google-genai-live/tests/__init__.py create mode 100644 python/frameworks/google-genai-live/tests/_voice_tab.py create mode 100644 python/frameworks/google-genai-live/tests/conftest.py create mode 100644 python/frameworks/google-genai-live/tests/fixtures/flag_generation_complete.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/flag_interrupted.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/flag_turn_complete.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j1_realtime_audio.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j2_tool.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j3_interrupt_cancel.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j4_first_connection.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j4_second_connection.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j6_socket_death.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/j8_setup_rejected.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/real_20261015_audio_tool.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/real_20261015_text_tool.json create mode 100644 python/frameworks/google-genai-live/tests/fixtures/text_session.json create mode 100644 python/frameworks/google-genai-live/tests/test_fence.py create mode 100644 python/frameworks/google-genai-live/tests/test_privacy.py create mode 100644 python/frameworks/google-genai-live/tests/test_resume.py create mode 100644 python/frameworks/google-genai-live/tests/test_session.py create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/_connect.py create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/_session.py create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/package.py create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/py.typed create mode 100644 python/frameworks/google-genai-live/traceai_google_genai_live/version.py diff --git a/python/frameworks/google-genai-live/CHANGELOG.md b/python/frameworks/google-genai-live/CHANGELOG.md new file mode 100644 index 000000000..8ed19aa35 --- /dev/null +++ b/python/frameworks/google-genai-live/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +## 0.1.0 (alpha) + +- New package: tracing for Gemini Live sessions opened with `client.aio.live.connect` on the + Gemini Developer API (google-genai `>=2.28,<3`; tested on 2.31.0 and 2.28.0 with + `gemini-3.8-live`). Gemini Live is a preview API: a field rename upstream is a floor and + fixture update here, not a silent fallback. google-genai 3.0 removes `Live.send` / + `Live.start_stream`, which this package never wrapped; pin below 3. +- One trace per session: setup, user turns, model turns (model, tokens, transcription), tool + calls with cancellation, interruptions, `goAway`, and a resume with the stored handle as a + `gemini.live.reconnect` span in the same trace. +- Audio sessions carry `fi.span.kind=CONVERSATION` on a parentless root plus `call.*` and + `conversation.transcript*`, so Future AGI lists them as calls. Text-only sessions are `CHAIN`. +- Privacy: audio bytes, API keys and resumption handles never land on a span. Text content is + on by default; `metadata_only=True` (or `TraceConfig` / `FI_HIDE_INPUTS` / `FI_HIDE_OUTPUTS`) + drops it. Raw error text is content; stack traces are never exported. +- Not supported: Vertex AI Live, ephemeral tokens, the deprecated `send` / `start_stream`, + LiveKit / Pipecat sessions, Node. diff --git a/python/frameworks/google-genai-live/README.md b/python/frameworks/google-genai-live/README.md new file mode 100644 index 000000000..37d7c99f5 --- /dev/null +++ b/python/frameworks/google-genai-live/README.md @@ -0,0 +1,147 @@ +# traceAI-google-genai-live + +Future AGI tracing for **Gemini Live** sessions that your Python process opens itself with +the official SDK: `client.aio.live.connect(...)` on the Gemini Developer API. + +One Live session is one trace. Inside it you get the setup, each user turn, each model turn +(model, tokens, transcription), each tool call, interruptions, cancellations, `goAway`, and +a reconnect that resumes the session with the handle this process stored. Audio sessions are +listed as calls in Future AGI's voice views. + +Status: **alpha**. Gemini Live is a preview API. + +| Tested | Version | +|---|---| +| google-genai | **2.31.0** (latest, 2026-10-14) and 2.28.0 (floor) | +| Live model | `gemini-3.8-live` (Gemini Developer API, real sessions on 2026-10-15) | +| Python | 3.10 - 3.13 (`fi-instrumentation-otel` declares `<3.14`) | + +## Install + +```bash +pip install traceAI-google-genai-live "google-genai>=2.28,<3" +``` + +## Use + +```python +import os + +from fi_instrumentation import register +from fi_instrumentation.fi_types import ProjectType +from google import genai +from traceai_google_genai_live import instrument + +trace_provider = register(project_type=ProjectType.OBSERVE, project_name="my-voice-agent") +instrument(tracer_provider=trace_provider) + +client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) +async with client.aio.live.connect(model="gemini-3.8-live", + config={"response_modalities": ["AUDIO"], + "input_audio_transcription": {}, + "output_audio_transcription": {}}) as session: + ... # your send_realtime_input / send_client_content / receive / send_tool_response loop +``` + +`GoogleGenAILiveInstrumentor().instrument(tracer_provider=...)` is the same thing. +A complete runnable example with a tool call is in [`examples/voice_tool.py`](examples/voice_tool.py). + +Options: + +| Option | Default | Effect | +|---|---|---| +| `metadata_only` | `False` | Drop all content: system instruction, user text, transcriptions, tool arguments and results, model text, raw error text. Model, voice name, timings, tool names, flags, close codes and token counts stay. | +| `config=TraceConfig(hide_inputs=..., hide_outputs=...)` or `FI_HIDE_INPUTS` / `FI_HIDE_OUTPUTS` | off | Drop one direction. The conversation transcript needs both. | +| `session_id`, `user_id` | none | Defaults for every session. `fi_instrumentation.using_session(...)` / `using_user(...)` around `connect` override them per call. | +| `resume_window_seconds` | `30` | How long a session whose connection ended after `goAway`, or died, waits for a reconnect with its stored handle before the root span closes. | + +## What you get + +``` +gemini.live.session CONVERSATION (audio) / CHAIN (text-only); parentless when CONVERSATION +├─ gemini.live.setup url.path (no host, no query), system instruction (content) +├─ gemini.live.user_turn transcription or text (content); gemini.turn_start / gemini.turn_boundary +├─ gemini.live.model_turn LLM: model, llm.token_count.*, gemini.usage.*, transcription (content) +│ └─ gemini.live.tool tool name, call id, args and result (content); cancelled / unanswered flags +├─ gemini.live.reconnect gap between the dropped connection and the resumed setup +└─ gemini.live.tool_response a response for a cancelled (ignored) or unknown (unmatched, ERROR) call id +``` + +Root attributes: `gen_ai.system=google_genai`, `gen_ai.request.model`, `session.id` +(+ `gemini.session_id_source` = `customer` | `server` | `local`), `user.id` only if you set +one, `gemini.response_modalities`, `gemini.voice_name`, `gemini.tool_names`, +`gemini.realtime_input_count`, `gemini.go_away`, `gemini.time_left`, `gemini.reconnect_count`. + +For audio sessions the root also carries the voice-call fields: `call.duration`, +`call.total_turns` (omitted, not 0, when no turn closed), `call.talk_ratio` (model speech +seconds / user speech seconds; omitted unless both were measured: user speech from the +server's `voiceActivity` offsets, model speech from raw PCM length at the declared rate), and +`conversation.transcript` / `fi.conversation.transcript` plus flattened +`conversation.transcript.{i}.message.role|content` rows when content capture is on. + +How server events map: + +- `generationComplete` sets `gemini.generation_complete` and does not close the model turn. + `turnComplete` closes it. `interrupted` closes it at once (`gemini.interrupted=true`). +- `send_client_content` during an open model turn closes that turn with + `gemini.close_reason=client_content`. +- A user turn opens on the server's `voiceActivity` `ACTIVITY_START` (or your + `activity_start`, a realtime `text=` input, or the first input transcription) and closes on + `ACTIVITY_END` (`gemini.turn_boundary=activity_end`), falling back to `turnComplete` + (`gemini.turn_boundary=turn_complete`). `send_client_content` is its own user turn. +- `toolCall` opens a model turn if none is open (real sessions send the call with no model + output before it, then `turnComplete` + usage for that generation), and one tool span per + function call. The span closes on your `send_tool_response` with the same id. + `toolCallCancellation` closes it with `gemini.cancelled=true` and no result. +- `usageMetadata` is copied onto the open model turn (latest message wins on that span, and + every message is also a span event). Nothing is summed. In real `gemini-3.8-live` sessions + each message covered one generation (its prompt count includes the context so far). +- Audio frames sent with `send_realtime_input` are counted, never spanned. + +## Failures + +| Case | Trace | +|---|---| +| Setup rejected (bad model, bad key) | One ERROR root (`setup_rejected`) with the setup span; the SDK's `APIError` reaches your code unchanged. No turns, no `call.total_turns`. | +| Socket dies (no `goAway`) | Open spans close ERROR `socket_closed`, `gemini.close_code` on the root, one flush. No reconnect by this package. | +| `goAway` | `gemini.go_away` / `gemini.time_left` on the root; the root stays open. | +| Reconnect with the stored handle | Same trace, one `gemini.live.reconnect` span. Failure: root ERROR `resume_failed`. | +| Reconnect with a handle this process did not store | New trace (`gemini.resumed_unknown_handle=true`). | +| Task cancelled | Open spans and the root close ERROR `task_cancelled`. | +| Exception from `receive()` | Recorded on the root, re-raised unchanged. | +| Export queue full | Spans are dropped by the batch processor; the socket reader is never blocked. | + +## Privacy + +- Default: text on, audio off. Audio bytes (`realtimeInput` and `modelTurn` inline data) never + land on a span. +- The API key is never copied. The connection URL is recorded as its path only. +- The session resumption handle stays in memory and is never a span attribute or a log line. +- Raw error text is content: it is recorded only when both content directions are on. Stack + traces are never exported. + +## Not supported + +- Vertex AI Live (`vertexai=True`): one warning, not traced. +- Ephemeral tokens (`auth_tokens/...`, `BidiGenerateContentConstrained`): one warning, not traced. +- The deprecated `session.send` and `session.start_stream` are not wrapped. Use + `send_client_content`, `send_realtime_input`, `send_tool_response` and `receive`. +- `generate_content` / `generate_content_stream`: use `traceAI-google-genai`. +- Gemini Live inside LiveKit or Pipecat: use `traceAI-livekit` / `traceAI-pipecat`. +- Node `@google/genai`. +- google-genai 3.x (the version check refuses it). +- No audio recording attachment, no cost in currency (the platform estimates cost from model + and tokens where it has a price). + +## Tests + +```bash +pip install -e . pytest pytest-asyncio +pytest +``` + +The tests run the real google-genai SDK against a scripted socket (`tests/conftest.py`). +`tests/fixtures/real_20261015_*.json` are server frames recorded from real +`gemini-3.8-live` sessions (audio replaced by silence, resumption handles replaced). +`tests/_voice_tab.py` is a local stand-in for the shared TH-8339 voice-tab assertion helper, +which had not merged; replace it with the shared helper when it lands. diff --git a/python/frameworks/google-genai-live/examples/README.md b/python/frameworks/google-genai-live/examples/README.md new file mode 100644 index 000000000..086309196 --- /dev/null +++ b/python/frameworks/google-genai-live/examples/README.md @@ -0,0 +1,26 @@ +# Example: a traced Gemini Live voice session + +`voice_tool.py` opens one Live session on `gemini-3.8-live`, asks for the weather (typed, or +spoken from a 16 kHz 16-bit mono PCM file), answers the model's `get_weather` tool call, and +lets the model answer out loud. With `instrument()` the whole session is one Future AGI trace. + +```bash +pip install traceAI-google-genai-live "google-genai>=2.28,<3" +export GEMINI_API_KEY=... FI_API_KEY=... FI_SECRET_KEY=... +# export FI_BASE_URL=... # only for a self-hosted Future AGI collector +python voice_tool.py --project gemini-live-demo +python voice_tool.py --pcm question.pcm # spoken question +python voice_tool.py --metadata-only --marker X # no content leaves the process +python voice_tool.py --resume # drop the socket, resume with the handle: still one trace +python voice_tool.py --model not-a-model # setup rejected: one ERROR root +``` + +A spoken question on macOS: + +```bash +say -o q.aiff "What is the weather in Paris right now?" +afconvert -f WAVE -d LEI16@16000 -c 1 q.aiff q.wav +python -c "import wave; w=wave.open('q.wav'); open('question.pcm','wb').write(w.readframes(w.getnframes()))" +``` + +`run_e2e.sh` runs the scenarios above in a fresh virtualenv built from this checkout. diff --git a/python/frameworks/google-genai-live/examples/run_e2e.sh b/python/frameworks/google-genai-live/examples/run_e2e.sh new file mode 100755 index 000000000..4e61097d0 --- /dev/null +++ b/python/frameworks/google-genai-live/examples/run_e2e.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# End-to-end run of traceAI-google-genai-live against a real Gemini Live model and a Future AGI +# collector. Needs GEMINI_API_KEY, FI_API_KEY, FI_SECRET_KEY (and FI_BASE_URL for a local +# collector). Each scenario is one short real Live session. +# usage: [PYTHON=python3.13] examples/run_e2e.sh [project-name] [google-genai version] +# Python 3.10-3.13 (fi-instrumentation-otel declares <3.14). +set -euo pipefail +HERE="$(cd "$(dirname "$0")" && pwd)" +PKG="$(dirname "$HERE")" +PROJECT="${1:-gemini-live-e2e}" +GENAI="${2:-2.31.0}" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT +"${PYTHON:-python3}" -m venv "$WORK/venv" +"$WORK/venv/bin/pip" -q install "$PKG" "google-genai==$GENAI" +export FI_PROJECT_NAME="$PROJECT" +PY="$WORK/venv/bin/python" +EX="$HERE/voice_tool.py" +MARK="FI-MARKER-$RANDOM$RANDOM" +echo "1. typed question + tool"; "$PY" "$EX" +echo "2. drop + resume (one trace)"; "$PY" "$EX" --resume +echo "3. metadata only ($MARK)"; "$PY" "$EX" --metadata-only --marker "$MARK" +echo "4. setup rejected (expect error)"; "$PY" "$EX" --model gemini-not-a-model-live || true +echo "Done. In Future AGI open project $PROJECT: 4 traces; search for $MARK must return nothing." diff --git a/python/frameworks/google-genai-live/examples/voice_tool.py b/python/frameworks/google-genai-live/examples/voice_tool.py new file mode 100644 index 000000000..75a6286a2 --- /dev/null +++ b/python/frameworks/google-genai-live/examples/voice_tool.py @@ -0,0 +1,131 @@ +"""Trace one Gemini Live voice session with a tool call in Future AGI. + + pip install traceAI-google-genai-live "google-genai>=2.28,<3" + export GEMINI_API_KEY=... FI_API_KEY=... FI_SECRET_KEY=... + python voice_tool.py # typed question, spoken answer + python voice_tool.py --pcm question.pcm # spoken question (16 kHz, 16-bit mono PCM) + +Options used by the package's end-to-end run: + --metadata-only keep model, timings, tool names and token counts; drop all content + --marker TEXT put TEXT in the system instruction, the question and the tool result + --resume drop the socket after the first answer and resume with the stored handle + --model NAME Live model (default gemini-3.8-live; the list is preview, re-read it) + --modality TEXT ask for TEXT responses (native-audio models refuse this) + --user-id ID attach your own user id to the call (the wire carries none) +""" + +import argparse +import asyncio +import os + +from fi_instrumentation import register, using_user +from fi_instrumentation.fi_types import ProjectType +from google import genai +from google.genai import errors, types + +from traceai_google_genai_live import instrument + +parser = argparse.ArgumentParser() +parser.add_argument("--project", default=os.getenv("FI_PROJECT_NAME", "gemini-live-demo")) +parser.add_argument("--model", default="gemini-3.8-live") +parser.add_argument("--pcm") +parser.add_argument("--metadata-only", action="store_true") +parser.add_argument("--marker", default="") +parser.add_argument("--resume", action="store_true") +parser.add_argument("--modality", default="AUDIO") +parser.add_argument("--user-id") +args = parser.parse_args() + +# 1. Future AGI first, then the instrumentor. +trace_provider = register(project_type=ProjectType.OBSERVE, project_name=args.project) +instrument(tracer_provider=trace_provider, metadata_only=args.metadata_only) + +WEATHER = { + "name": "get_weather", + "description": "Current weather for a city.", + "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}, +} + + +def get_weather(city: str) -> dict: + return {"city": city, "forecast": "18C and sunny", "note": args.marker} + + +def live_config(handle=None) -> dict: + return { + "response_modalities": [args.modality], + "input_audio_transcription": {}, + "output_audio_transcription": {}, + "system_instruction": "You are a terse voice assistant. Always use get_weather for weather. " + + args.marker, + "tools": [{"function_declarations": [WEATHER]}], + "session_resumption": {"handle": handle} if handle else {}, + } + + +async def stream_pcm(session, path: str) -> None: + pcm = open(path, "rb").read() + chunk = 3200 # 100 ms of 16 kHz 16-bit mono + for i in range(0, len(pcm), chunk): + await session.send_realtime_input(audio=types.Blob(data=pcm[i:i + chunk], mime_type="audio/pcm;rate=16000")) + await asyncio.sleep(0.1) + for _ in range(15): # trailing silence so server VAD ends the user turn + await session.send_realtime_input(audio=types.Blob(data=b"\x00" * chunk, mime_type="audio/pcm;rate=16000")) + await asyncio.sleep(0.1) + await session.send_realtime_input(audio_stream_end=True) + + +async def answer_turns(session, state: dict) -> None: + """Receive until the model has spoken an answer (a turn with audio), answering tool calls.""" + while True: + spoke = False + async for msg in session.receive(): + if msg.session_resumption_update and msg.session_resumption_update.new_handle: + state["handle"] = msg.session_resumption_update.new_handle + if msg.data: + spoke = True + if msg.server_content and msg.server_content.output_transcription: + print("model:", msg.server_content.output_transcription.text, flush=True) + if msg.tool_call: + await session.send_tool_response(function_responses=[ + types.FunctionResponse(id=fc.id, name=fc.name, response=get_weather(**(fc.args or {}))) + for fc in msg.tool_call.function_calls]) + if spoke: + return + + +async def main() -> None: + client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) + question = "What is the weather in Paris right now? " + args.marker + state: dict = {} + async with client.aio.live.connect(model=args.model, config=live_config()) as session: + if args.pcm: + sender = asyncio.create_task(stream_pcm(session, args.pcm)) + else: + await session.send_client_content(turns={"role": "user", "parts": [{"text": question}]}) + sender = None + await asyncio.wait_for(answer_turns(session, state), 60) + if sender: + await sender + if args.resume: + # Simulate a dropped connection: the next receive fails, as it would on a network drop. + await session.close() + try: + async for _ in session.receive(): + pass + except errors.APIError as exc: + print("connection dropped:", exc.code, flush=True) + if args.resume and state.get("handle"): + async with client.aio.live.connect(model=args.model, config=live_config(state["handle"])) as session: + await session.send_client_content(turns={"role": "user", "parts": [{"text": "Which city did I ask about?"}]}) + await asyncio.wait_for(answer_turns(session, state), 60) + + +try: + if args.user_id: + with using_user(args.user_id): + asyncio.run(main()) + else: + asyncio.run(main()) +finally: + trace_provider.force_flush() diff --git a/python/frameworks/google-genai-live/pyproject.toml b/python/frameworks/google-genai-live/pyproject.toml new file mode 100644 index 000000000..4850e0ee2 --- /dev/null +++ b/python/frameworks/google-genai-live/pyproject.toml @@ -0,0 +1,48 @@ +[tool.poetry] +name = "traceAI-google-genai-live" +version = "0.1.0" +description = "Future AGI tracing for Gemini Live sessions (google-genai client.aio.live.connect): one trace per session with turns, tool calls, interruptions and usage" +authors = ["Future AGI "] +license = "Apache-2.0" +readme = "README.md" +packages = [ + { include = "traceai_google_genai_live" }, +] +keywords = ["opentelemetry", "gemini", "gemini-live", "google-genai", "voice", "realtime", "tracing", "observability"] +classifiers = [ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: System :: Monitoring", +] + +[tool.poetry.dependencies] +# fi-instrumentation-otel declares python <3.14. +python = ">=3.10,<3.14" +# Gemini Live is preview. AsyncLive.connect and the send_client_content / +# send_realtime_input / send_tool_response / receive methods were confirmed at 2.28.0 +# and 2.31.0. The vendor removes Live.send and Live.start_stream before 3.0: pin below 3. +google-genai = ">=2.28,<3" +fi-instrumentation-otel = ">=0.1.11" +opentelemetry-api = ">=1.29.0" +opentelemetry-sdk = ">=1.29.0" +opentelemetry-instrumentation = ">=0.50b0" +wrapt = ">=1.15.0" + +[tool.poetry.group.dev.dependencies] +pytest = ">=7.0.0" +pytest-asyncio = ">=0.23.0" + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/python/frameworks/google-genai-live/tests/__init__.py b/python/frameworks/google-genai-live/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/python/frameworks/google-genai-live/tests/_voice_tab.py b/python/frameworks/google-genai-live/tests/_voice_tab.py new file mode 100644 index 000000000..bb7e7933e --- /dev/null +++ b/python/frameworks/google-genai-live/tests/_voice_tab.py @@ -0,0 +1,57 @@ +"""Local stand-in for the TH-8339 Voice-tab assertion helper. + +The shared helper (``assert_voice_tab(spans, *, omissions=(), forbidden_substrings=())``, +voice family architecture, D-V06) was not merged when this package was written. This file +implements the same contract for these tests only and should be replaced by an import of +the shared helper when it lands. It does not call ClickHouse or the collector: the +collector's ``resolveObservationType`` lowercases ``fi.span.kind`` and stores +``conversation`` only for that token, and the Voice tab lists parentless roots with +``observation_type = 'conversation'``. +""" + +from __future__ import annotations + +import json +from typing import Any, Iterable, Sequence + +CREDENTIAL_SHAPES = ("sk-", "Bearer ", "api_key=", "openai-insecure-api-key.") + + +def _root(spans: Sequence[Any]) -> Any: + roots = [s for s in spans if s.parent is None] + assert len(roots) == 1, f"expected exactly one parentless root, got {[s.name for s in roots]}" + return roots[0] + + +def _values(spans: Iterable[Any]) -> str: + out = [] + for s in spans: + out.append(json.dumps(dict(s.attributes or {}), default=str)) + for ev in s.events: + out.append(json.dumps(dict(ev.attributes or {}), default=str)) + out.append(str(s.status.description)) + return "\n".join(out) + + +def assert_voice_tab(spans: Sequence[Any], *, omissions: Iterable[str] = (), + forbidden_substrings: Iterable[str] = ()) -> Any: + root = _root(spans) + attrs = dict(root.attributes or {}) + assert str(attrs.get("fi.span.kind", "")).lower() == "conversation", attrs.get("fi.span.kind") + assert attrs.get("gen_ai.system"), "gen_ai.system missing: the server would parse the call as Vapi" + assert attrs.get("session.id"), "session.id missing" + for key in omissions: + assert key not in attrs, f"{key} must be absent, found {attrs.get(key)!r}" + text = _values(spans) + for shape in CREDENTIAL_SHAPES: + assert shape not in text, f"credential-shaped token {shape!r} on a span" + for needle in forbidden_substrings: + assert needle not in text, f"forbidden value {needle!r} on a span" + return root + + +def assert_not_voice_tab(spans: Sequence[Any]) -> Any: + root = _root(spans) + kind = str((root.attributes or {}).get("fi.span.kind", "")).lower() + assert kind != "conversation", "a text session must not be listed as a call" + return root diff --git a/python/frameworks/google-genai-live/tests/conftest.py b/python/frameworks/google-genai-live/tests/conftest.py new file mode 100644 index 000000000..68936a7d8 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/conftest.py @@ -0,0 +1,174 @@ +"""A scripted BidiGenerateContent socket under the real google-genai SDK. + +``google.genai.live.ws_connect`` is replaced by a fake that replays a fixture of server +frames (wire JSON, camelCase), so the real ``AsyncLive.connect``, converters and +``AsyncSession`` methods run unchanged. No network, no Gemini call. +""" + +from __future__ import annotations + +import json +import logging +import pathlib +from typing import Any, Dict, List + +import pytest +from google import genai +from google.genai import live as live_mod +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import SimpleSpanProcessor +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter +from websockets.exceptions import ConnectionClosedError +from websockets.frames import Close + +from traceai_google_genai_live import GoogleGenAILiveInstrumentor + +FIXTURES = pathlib.Path(__file__).parent / "fixtures" +API_KEY = "AIzaFixtureKey-0000-not-real-7f2e" +AUDIO_MARKER = "FI-AUDIO-BYTES-MARKER-9c41" +HANDLE_1 = "fixture-resumption-handle-1-d93e" +HANDLE_2 = "fixture-resumption-handle-2-a17b" + + +def load(name: str) -> List[Any]: + return json.loads((FIXTURES / f"{name}.json").read_text()) + + +class FakeSocket: + def __init__(self, frames: List[Any]) -> None: + self.frames = list(frames) + self.sent: List[Dict[str, Any]] = [] + self.closed = False + + async def send(self, message: str) -> None: + if self.closed: + raise ConnectionClosedError(Close(1000, ""), None) + self.sent.append(json.loads(message)) + + async def recv(self, decode: Any = None) -> bytes: + if not self.frames: + raise AssertionError("fixture exhausted: the test read past the script") + frame = self.frames.pop(0) + if isinstance(frame, list) and frame and frame[0] == "close": + self.closed = True + raise ConnectionClosedError(Close(frame[1], frame[2]) if frame[1] != 1006 else None, None) + return json.dumps(frame).encode() + + async def close(self) -> None: + self.closed = True + + +class FakeServer: + """Each ``ws_connect`` call takes the next script from ``scripts``.""" + + def __init__(self) -> None: + self.scripts: List[List[Any]] = [] + self.sockets: List[FakeSocket] = [] + self.connects = 0 + self.uris: List[str] = [] + + def __call__(self, uri: str, **kwargs: Any): + server = self + + class _CM: + async def __aenter__(self_inner): + server.connects += 1 + server.uris.append(uri) + sock = FakeSocket(server.scripts.pop(0)) + server.sockets.append(sock) + return sock + + async def __aexit__(self_inner, *exc): + return False + + return _CM() + + +@pytest.fixture +def server(monkeypatch: pytest.MonkeyPatch) -> FakeServer: + srv = FakeServer() + monkeypatch.setattr(live_mod, "ws_connect", srv) + return srv + + +@pytest.fixture +def exporter() -> InMemorySpanExporter: + return InMemorySpanExporter() + + +@pytest.fixture +def provider(exporter: InMemorySpanExporter) -> TracerProvider: + tp = TracerProvider() + tp.add_span_processor(SimpleSpanProcessor(exporter)) + return tp + + +@pytest.fixture +def instrument(provider: TracerProvider, monkeypatch: pytest.MonkeyPatch): + for name in ("FI_HIDE_INPUTS", "FI_HIDE_OUTPUTS"): + monkeypatch.delenv(name, raising=False) + created = [] + + def _instrument(**kwargs: Any) -> GoogleGenAILiveInstrumentor: + inst = GoogleGenAILiveInstrumentor() + inst.instrument(tracer_provider=provider, **kwargs) + created.append(inst) + return inst + + yield _instrument + for inst in created: + inst.uninstrument() + + +@pytest.fixture +def client() -> genai.Client: + return genai.Client(api_key=API_KEY) + + +AUDIO_CONFIG = {"response_modalities": ["AUDIO"], "output_audio_transcription": {}, + "input_audio_transcription": {}} + + +async def run_turn(session: Any, *, tool_result: Any = None) -> List[Any]: + """One ``receive()`` pass. Answers each tool call with ``tool_result`` when given.""" + from google.genai import types + + seen = [] + async for msg in session.receive(): + seen.append(msg) + if msg.tool_call and tool_result is not None: + await session.send_tool_response(function_responses=[ + types.FunctionResponse(id=fc.id, name=fc.name, response=tool_result) + for fc in msg.tool_call.function_calls]) + return seen + + +def spans_by_name(exporter: InMemorySpanExporter) -> Dict[str, List[Any]]: + out: Dict[str, List[Any]] = {} + for span in exporter.get_finished_spans(): + out.setdefault(span.name, []).append(span) + return out + + +def all_text(exporter: InMemorySpanExporter) -> str: + """Every attribute value, event attribute, status text and resource attribute exported.""" + chunks = [] + for span in exporter.get_finished_spans(): + chunks.append(span.name) + chunks.append(json.dumps(dict(span.attributes or {}), default=str)) + chunks.append(json.dumps(dict(span.resource.attributes or {}), default=str)) + chunks.append(str(span.status.description)) + for ev in span.events: + chunks.append(ev.name) + chunks.append(json.dumps(dict(ev.attributes or {}), default=str)) + return "\n".join(chunks) + + +@pytest.fixture +def log_text(caplog: pytest.LogCaptureFixture): + caplog.set_level(logging.DEBUG) + + def _text() -> str: + return "\n".join(r.getMessage() + " " + str(r.args) for r in caplog.records) + + return _text diff --git a/python/frameworks/google-genai-live/tests/fixtures/flag_generation_complete.json b/python/frameworks/google-genai-live/tests/fixtures/flag_generation_complete.json new file mode 100644 index 000000000..eadac4b04 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/flag_generation_complete.json @@ -0,0 +1,28 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "Hello " + } + } + }, + { + "serverContent": { + "generationComplete": true + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/flag_interrupted.json b/python/frameworks/google-genai-live/tests/fixtures/flag_interrupted.json new file mode 100644 index 000000000..85d8402e0 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/flag_interrupted.json @@ -0,0 +1,33 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "Hello " + } + } + }, + { + "serverContent": { + "interrupted": true + } + }, + { + "serverContent": { + "turnComplete": true + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/flag_turn_complete.json b/python/frameworks/google-genai-live/tests/fixtures/flag_turn_complete.json new file mode 100644 index 000000000..6e05b748e --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/flag_turn_complete.json @@ -0,0 +1,28 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "Hello " + } + } + }, + { + "serverContent": { + "turnComplete": true + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j1_realtime_audio.json b/python/frameworks/google-genai-live/tests/fixtures/j1_realtime_audio.json new file mode 100644 index 000000000..163e7e48d --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j1_realtime_audio.json @@ -0,0 +1,119 @@ +[ + { + "setupComplete": {} + }, + { + "sessionResumptionUpdate": { + "newHandle": "fixture-resumption-handle-1-d93e", + "resumable": true + } + }, + { + "voiceActivity": { + "type": "ACTIVITY_START", + "audioOffset": "0.160s" + } + }, + { + "serverContent": { + "inputTranscription": { + "text": "What is the weather " + } + } + }, + { + "serverContent": { + "inputTranscription": { + "text": "in Paris?" + } + } + }, + { + "voiceActivity": { + "type": "ACTIVITY_END", + "audioOffset": "2.160s" + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "It is " + } + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "sunny." + } + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + } + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 700, + "responseTokenCount": 40, + "totalTokenCount": 740, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 690 + }, + { + "modality": "AUDIO", + "tokenCount": 10 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 40 + } + ] + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j2_tool.json b/python/frameworks/google-genai-live/tests/fixtures/j2_tool.json new file mode 100644 index 000000000..d16a74c50 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j2_tool.json @@ -0,0 +1,119 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": {} + }, + { + "toolCall": { + "functionCalls": [ + { + "name": "get_weather", + "args": { + "city": "Paris" + }, + "id": "call_1" + } + ] + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 616, + "responseTokenCount": 12, + "totalTokenCount": 628, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 606 + }, + { + "modality": "AUDIO", + "tokenCount": 10 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 12 + } + ] + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "Paris is 18C " + } + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "and sunny." + } + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 648, + "responseTokenCount": 81, + "totalTokenCount": 729, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 638 + }, + { + "modality": "AUDIO", + "tokenCount": 10 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 81 + } + ] + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j3_interrupt_cancel.json b/python/frameworks/google-genai-live/tests/fixtures/j3_interrupt_cancel.json new file mode 100644 index 000000000..38cf76438 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j3_interrupt_cancel.json @@ -0,0 +1,53 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMt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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "Let me check " + } + } + }, + { + "toolCall": { + "functionCalls": [ + { + "name": "get_weather", + "args": { + "city": "Rome" + }, + "id": "call_9" + } + ] + } + }, + { + "serverContent": { + "interrupted": true + } + }, + { + "toolCallCancellation": { + "ids": [ + "call_9" + ] + } + }, + { + "serverContent": { + "turnComplete": true + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j4_first_connection.json b/python/frameworks/google-genai-live/tests/fixtures/j4_first_connection.json new file mode 100644 index 000000000..e8f139c51 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j4_first_connection.json @@ -0,0 +1,65 @@ +[ + { + "setupComplete": {} + }, + { + "sessionResumptionUpdate": { + "newHandle": "fixture-resumption-handle-1-d93e", + "resumable": true + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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, + "usageMetadata": { + "promptTokenCount": 100, + "responseTokenCount": 10, + "totalTokenCount": 110, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 90 + }, + { + "modality": "AUDIO", + "tokenCount": 10 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 10 + } + ] + } + }, + { + "goAway": { + "timeLeft": "10s" + } + }, + { + "serverContent": { + "turnComplete": true + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j4_second_connection.json b/python/frameworks/google-genai-live/tests/fixtures/j4_second_connection.json new file mode 100644 index 000000000..d50e3e902 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j4_second_connection.json @@ -0,0 +1,55 @@ +[ + { + "setupComplete": {} + }, + { + "sessionResumptionUpdate": { + "newHandle": "fixture-resumption-handle-2-a17b", + "resumable": true + } + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "Second answer." + } + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 130, + "responseTokenCount": 12, + "totalTokenCount": 142, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 120 + }, + { + "modality": "AUDIO", + "tokenCount": 10 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 12 + } + ] + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j6_socket_death.json b/python/frameworks/google-genai-live/tests/fixtures/j6_socket_death.json new file mode 100644 index 000000000..841dc1021 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j6_socket_death.json @@ -0,0 +1,28 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ] + }, + "outputTranscription": { + "text": "I was say" + } + } + }, + [ + "close", + 1006, + "" + ] +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/j8_setup_rejected.json b/python/frameworks/google-genai-live/tests/fixtures/j8_setup_rejected.json new file mode 100644 index 000000000..2dc8803f5 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/j8_setup_rejected.json @@ -0,0 +1,7 @@ +[ + [ + "close", + 1008, + "models/x is not found for API version v1beta" + ] +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/real_20261015_audio_tool.json b/python/frameworks/google-genai-live/tests/fixtures/real_20261015_audio_tool.json new file mode 100644 index 000000000..97ef6877a --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/real_20261015_audio_tool.json @@ -0,0 +1,279 @@ +[ + { + "setupComplete": {} + }, + { + "sessionResumptionUpdate": { + "newHandle": "fixture-resumption-handle-1-d93e", + "resumable": true + } + }, + { + "voiceActivity": { + "type": "ACTIVITY_START", + "audioOffset": "0.160s" + } + }, + {}, + {}, + { + "serverContent": { + "inputTranscription": { + "text": "What is the weather in Paris right now? Please check with the tool." + } + } + }, + { + "voiceActivity": { + "type": "ACTIVITY_END", + "audioOffset": "4.720s" + } + }, + { + "serverContent": {} + }, + { + "toolCall": { + "functionCalls": [ + { + "name": "get_weather", + "args": { + "city": "Paris" + }, + "id": "call_1059563" + } + ] + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 716, + "responseTokenCount": 12, + "totalTokenCount": 728, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 348 + }, + { + "modality": "AUDIO", + "tokenCount": 317 + } + ], + "thoughtsTokenCount": 34 + } + }, + {}, + { + "serverContent": {} + }, + {}, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + }, + "outputTranscription": { + "text": "The weather in " + } + } + }, + {}, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAA" + } + } + ], + "role": "model" + }, + "outputTranscription": { + "text": "Paris is 18C" + } + } + }, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + }, + "outputTranscription": { + "text": " and sunny." + } + } + }, + {}, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + } + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 748, + "responseTokenCount": 79, + "totalTokenCount": 827, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 362 + }, + { + "modality": "AUDIO", + "tokenCount": 317 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 79 + } + ], + "thoughtsTokenCount": 83 + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/real_20261015_text_tool.json b/python/frameworks/google-genai-live/tests/fixtures/real_20261015_text_tool.json new file mode 100644 index 000000000..359079f82 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/real_20261015_text_tool.json @@ -0,0 +1,289 @@ +[ + { + "setupComplete": {} + }, + { + "sessionResumptionUpdate": { + "newHandle": "fixture-resumption-handle-1-d93e", + "resumable": true + } + }, + {}, + { + "serverContent": {} + }, + {}, + { + "toolCall": { + "functionCalls": [ + { + "name": "get_weather", + "args": { + "city": "Paris" + }, + "id": "call_177134" + } + ] + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 616, + "responseTokenCount": 12, + "totalTokenCount": 628, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 345 + }, + { + "modality": "AUDIO", + "tokenCount": 222 + } + ], + "thoughtsTokenCount": 35 + } + }, + {}, + { + "serverContent": {} + }, + {}, + {}, + {}, + {}, + {}, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + }, + "outputTranscription": { + "text": "The weather in Paris " + } + } + }, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + }, + "outputTranscription": { + "text": "is 18C and sunny." + } + } + }, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + } + } + }, + {}, + {}, + {}, + {}, + {}, + { + "serverContent": { + "modelTurn": { + "parts": [ + { + "inlineData": { + "mimeType": "audio/pcm;rate=24000", + "data": "RkktQVVESU8tQllURVMtTUFSS0VSLTljNDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + } + } + ], + "role": "model" + } + } + }, + { + "serverContent": { + "generationComplete": true + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 648, + "responseTokenCount": 81, + "totalTokenCount": 729, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 359 + }, + { + "modality": "AUDIO", + "tokenCount": 222 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 81 + } + ], + "thoughtsTokenCount": 50 + } + } +] diff --git a/python/frameworks/google-genai-live/tests/fixtures/text_session.json b/python/frameworks/google-genai-live/tests/fixtures/text_session.json new file mode 100644 index 000000000..342fa3545 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/fixtures/text_session.json @@ -0,0 +1,43 @@ +[ + { + "setupComplete": {} + }, + { + "serverContent": { + "modelTurn": { + "role": "model", + "parts": [ + { + "text": "Hi there." + } + ] + } + } + }, + { + "serverContent": { + "turnComplete": true + }, + "usageMetadata": { + "promptTokenCount": 20, + "responseTokenCount": 3, + "totalTokenCount": 23, + "promptTokensDetails": [ + { + "modality": "TEXT", + "tokenCount": 10 + }, + { + "modality": "AUDIO", + "tokenCount": 10 + } + ], + "responseTokensDetails": [ + { + "modality": "AUDIO", + "tokenCount": 3 + } + ] + } + } +] diff --git a/python/frameworks/google-genai-live/tests/test_fence.py b/python/frameworks/google-genai-live/tests/test_fence.py new file mode 100644 index 000000000..160948ed3 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/test_fence.py @@ -0,0 +1,172 @@ +"""What this package patches and what it leaves alone (AC-01, AC-02, AC-22, AC-24, V-B, D-V11, AC-17).""" + +from __future__ import annotations + +import inspect +import logging +import pathlib +import sys +import time + +import pytest +import wrapt +from google import genai +from google.genai import live, models +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor, SpanExporter, SpanExportResult + +from traceai_google_genai_live import PATCHED, GoogleGenAILiveInstrumentor + +from .conftest import AUDIO_CONFIG, load, run_turn + +MODEL = "gemini-3.8-live" + + +def test_vb_sdk_surface_exists(): + """V-B: the hook points exist in the installed google-genai.""" + assert inspect.isfunction(live.AsyncLive.connect) or callable(live.AsyncLive.connect) + for name in ("send_client_content", "send_realtime_input", "send_tool_response", "receive"): + assert hasattr(live.AsyncSession, name), name + params = inspect.signature(live.AsyncLive.connect).parameters + assert "model" in params and "config" in params + + +def test_ac01_ac02_patch_list(instrument): + instrument() + assert set(PATCHED) == {("AsyncLive", "connect"), ("AsyncSession", "send_client_content"), + ("AsyncSession", "send_realtime_input"), ("AsyncSession", "send_tool_response"), + ("AsyncSession", "receive")} + for cls, name in PATCHED: + assert isinstance(getattr(live, cls).__dict__[name], wrapt.FunctionWrapper) + # Deprecated upstream: not wrapped. + assert not isinstance(live.AsyncSession.__dict__["send"], wrapt.FunctionWrapper) + assert not isinstance(live.AsyncSession.__dict__["start_stream"], wrapt.FunctionWrapper) + # The chat path belongs to traceAI-google-genai. + for name in ("generate_content", "generate_content_stream"): + assert not isinstance(models.Models.__dict__[name], wrapt.FunctionWrapper) + assert not isinstance(models.AsyncModels.__dict__[name], wrapt.FunctionWrapper) + + +def test_uninstrument_restores_originals(instrument): + originals = {(c, n): getattr(live, c).__dict__[n] for c, n in PATCHED} + inst = instrument() + inst.uninstrument() + for (c, n), fn in originals.items(): + assert getattr(live, c).__dict__[n] is fn + + +def test_ac22_livekit_and_pipecat_untouched(instrument): + before = {m for m in sys.modules if m.split(".")[0] in ("livekit", "pipecat", "traceai_livekit", "traceai_pipecat")} + instrument() + after = {m for m in sys.modules if m.split(".")[0] in ("livekit", "pipecat", "traceai_livekit", "traceai_pipecat")} + assert after == before + assert all(cls in ("AsyncLive", "AsyncSession") for cls, _ in PATCHED) + + +def test_ac24_chat_package_floor_untouched(): + pyproject = pathlib.Path(__file__).resolve().parents[2] / "google-genai" / "pyproject.toml" + if not pyproject.exists(): + pytest.skip("not run from the traceAI monorepo") + assert 'google-genai = ">=1.8.0"' in pyproject.read_text() + + +async def test_vertex_client_warns_once_and_is_not_traced(server, instrument, exporter, caplog): + from traceai_google_genai_live import _connect + + _connect._warned.discard("vertex") + instrument() + client = genai.Client(api_key="AIzaVertexExpress", vertexai=True) + server.scripts.append([{"setupComplete": {}}]) + caplog.set_level(logging.WARNING) + async with client.aio.live.connect(model="gemini-live", config=AUDIO_CONFIG): + pass + assert exporter.get_finished_spans() == () + assert sum("Vertex AI Live" in r.getMessage() for r in caplog.records) == 1 + + +async def test_ephemeral_token_is_not_traced(server, instrument, exporter, caplog): + from traceai_google_genai_live import _connect + + _connect._warned.discard("ephemeral") + instrument() + client = genai.Client(api_key="auth_tokens/fixture", http_options={"api_version": "v1alpha"}) + server.scripts.append([{"setupComplete": {}}]) + caplog.set_level(logging.WARNING) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG): + pass + assert exporter.get_finished_spans() == () + assert any("ephemeral-token" in r.getMessage() for r in caplog.records) + + +class _SlowExporter(SpanExporter): + def __init__(self): + self.exported = 0 + + def export(self, spans): + time.sleep(0.5) + self.exported += len(spans) + return SpanExportResult.SUCCESS + + def shutdown(self): + pass + + +async def test_ac17_full_queue_drops_and_never_blocks_the_reader(server, client): + slow = _SlowExporter() + tp = TracerProvider() + tp.add_span_processor(BatchSpanProcessor(slow, max_queue_size=1, max_export_batch_size=1, + schedule_delay_millis=1)) + inst = GoogleGenAILiveInstrumentor() + inst.instrument(tracer_provider=tp) + try: + frames = [{"setupComplete": {}}] + for i in range(30): + frames.append({"toolCall": {"functionCalls": [{"name": "f", "args": {}, "id": f"c{i}"}]}}) + frames.append({"serverContent": {"turnComplete": True}}) + server.scripts.append(frames) + worst = 0.0 + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + it = session.receive() + while True: + t0 = time.perf_counter() + try: + msg = await it.__anext__() + except StopAsyncIteration: + break + worst = max(worst, time.perf_counter() - t0) + if msg.tool_call: + from google.genai import types + + await session.send_tool_response(function_responses=[ + types.FunctionResponse(id=fc.id, name=fc.name, response={}) + for fc in msg.tool_call.function_calls]) + assert worst < 0.05, f"receive blocked for {worst:.3f}s" + finally: + inst.uninstrument() + tp.shutdown() + assert slow.exported < 33 # some spans were dropped instead of blocking + + +def test_ac01_generate_content_keeps_its_own_span_and_gets_no_live_span(instrument, provider, exporter, monkeypatch): + """AC-01: with both packages instrumented, generate_content is traced by traceAI-google-genai only.""" + chat = pytest.importorskip("traceai_google_genai") + import json as _json + + from google.genai import _api_client + + payload = {"candidates": [{"content": {"role": "model", "parts": [{"text": "pong"}]}, "finishReason": "STOP"}], + "usageMetadata": {"promptTokenCount": 3, "candidatesTokenCount": 1, "totalTokenCount": 4}} + monkeypatch.setattr(_api_client.BaseApiClient, "_request", + lambda self, *a, **k: _api_client.HttpResponse(headers={}, response_stream=[_json.dumps(payload)])) + instrument() + chat_inst = chat.GoogleGenAIInstrumentor() + chat_inst.instrument(tracer_provider=provider) + try: + client = genai.Client(api_key="AIzaFixtureKey-0000-not-real-7f2e") + out = client.models.generate_content(model="gemini-3.7-flash", contents="ping") + assert out.text == "pong" + finally: + chat_inst.uninstrument() + names = [s.name for s in exporter.get_finished_spans()] + assert names, "traceAI-google-genai produced no span" + assert not any(n.startswith("gemini.live.") for n in names), names diff --git a/python/frameworks/google-genai-live/tests/test_privacy.py b/python/frameworks/google-genai-live/tests/test_privacy.py new file mode 100644 index 000000000..7bb4de385 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/test_privacy.py @@ -0,0 +1,128 @@ +"""Privacy (J7; AC-14, AC-18, AC-19, AC-20).""" + +from __future__ import annotations + +import pytest +from google.genai import errors, types +from fi_instrumentation import TraceConfig + +from ._voice_tab import assert_voice_tab +from .conftest import API_KEY, AUDIO_CONFIG, AUDIO_MARKER, HANDLE_1, all_text, load, run_turn, spans_by_name + +MODEL = "gemini-3.8-live" +MARK = "FI-PRIVACY-MARKER-31d7" +SYSTEM = "You are a weather bot. " + MARK + + +def _config(): + return dict(AUDIO_CONFIG, system_instruction=SYSTEM, session_resumption={}, + tools=[{"function_declarations": [{"name": "get_weather", "description": "weather " + MARK}]}]) + + +async def _run(client, server): + server.scripts.append(load("j2_tool")) + async with client.aio.live.connect(model=MODEL, config=_config()) as session: + await session.send_client_content(turns={"role": "user", "parts": [{"text": "Weather in " + MARK}]}) + await session.send_realtime_input(text="realtime " + MARK) + await run_turn(session, tool_result={"result": "sunny " + MARK}) + await run_turn(session) + + +async def test_default_captures_text_but_never_audio_key_or_handle(server, instrument, client, exporter, log_text): + instrument() + server.scripts.append(load("j1_realtime_audio")) + async with client.aio.live.connect(model=MODEL, config=dict(_config())) as session: + await session.send_realtime_input(audio=types.Blob(data=AUDIO_MARKER.encode() * 20, + mime_type="audio/pcm;rate=16000")) + await run_turn(session) + text = all_text(exporter) + assert MARK in text # the system instruction is content and is on by default + assert AUDIO_MARKER not in text # AC-19: inbound and outbound audio bytes + assert API_KEY not in text and API_KEY not in log_text() # AC-20 + assert HANDLE_1 not in text and HANDLE_1 not in log_text() # AC-14 + assert "generativelanguage.googleapis.com" not in text # host is not recorded, path only + assert_voice_tab(exporter.get_finished_spans(), forbidden_substrings=(API_KEY, HANDLE_1, AUDIO_MARKER)) + + +async def test_ac18_metadata_only_omits_all_content(server, instrument, client, exporter): + instrument(metadata_only=True) + await _run(client, server) + text = all_text(exporter) + assert MARK not in text + by = spans_by_name(exporter) + for span in exporter.get_finished_spans(): + for key in ("input.value", "output.value", "conversation.transcript", "fi.conversation.transcript"): + assert key not in (span.attributes or {}), (span.name, key) + # Metadata stays. + (tool,) = by["gemini.live.tool"] + assert tool.attributes["tool.name"] == "get_weather" + root = by["gemini.live.session"][0] + assert root.attributes["gemini.tool_names"] == ("get_weather",) + model = by["gemini.live.model_turn"][0] + assert model.attributes["llm.token_count.prompt"] == 616 + assert model.attributes["llm.model_name"] == MODEL + assert root.attributes["call.total_turns"] >= 2 + + +async def test_full_mode_captures_each_content_kind(server, instrument, client, exporter): + instrument() + await _run(client, server) + by = spans_by_name(exporter) + assert MARK in by["gemini.live.setup"][0].attributes["input.value"] + assert MARK in by["gemini.live.tool"][0].attributes["output.value"] + assert any(MARK in (u.attributes.get("input.value") or "") for u in by["gemini.live.user_turn"]) + assert MARK in by["gemini.live.session"][0].attributes["conversation.transcript"] + + +@pytest.mark.parametrize("hide", ["inputs", "outputs"]) +async def test_trace_config_hides_one_direction(server, instrument, client, exporter, hide): + cfg = TraceConfig(hide_inputs=hide == "inputs", hide_outputs=hide == "outputs") + instrument(config=cfg) + await _run(client, server) + by = spans_by_name(exporter) + tool = by["gemini.live.tool"][0] + if hide == "inputs": + assert "input.value" not in tool.attributes and "output.value" in tool.attributes + assert "input.value" not in by["gemini.live.setup"][0].attributes + else: + assert "output.value" not in tool.attributes and "input.value" in tool.attributes + # The transcript needs both directions. + assert "conversation.transcript" not in by["gemini.live.session"][0].attributes + + +async def test_env_hide_inputs_is_honoured(server, instrument, client, exporter, monkeypatch): + instrument() + monkeypatch.setenv("FI_HIDE_INPUTS", "true") + # TraceConfig reads the environment when instrument() builds it, so re-instrument. + from traceai_google_genai_live import GoogleGenAILiveInstrumentor + + GoogleGenAILiveInstrumentor().uninstrument() + instrument() + await _run(client, server) + assert "input.value" not in spans_by_name(exporter)["gemini.live.setup"][0].attributes + + +async def test_error_text_is_content(server, instrument, client, exporter): + instrument(metadata_only=True) + server.scripts.append([["close", 1008, "models/" + MARK + " is not found"]]) + with pytest.raises(errors.APIError): + async with client.aio.live.connect(model=MARK, config=AUDIO_CONFIG): + pass + text = all_text(exporter) + # The model string is metadata and stays; the server's error text does not. + assert "is not found" not in text + setup = spans_by_name(exporter)["gemini.live.setup"][0] + assert setup.attributes["gemini.close_code"] == 1008 + assert setup.attributes["exception.type"] == "APIError" + assert "exception.stacktrace" not in text + + +async def test_error_text_kept_in_full_mode_without_stacktrace(server, instrument, client, exporter): + instrument() + server.scripts.append([["close", 1008, "models/x is not found"]]) + with pytest.raises(errors.APIError): + async with client.aio.live.connect(model="x", config=AUDIO_CONFIG): + pass + text = all_text(exporter) + assert "is not found" in text + assert "Traceback" not in text and "stacktrace" not in text diff --git a/python/frameworks/google-genai-live/tests/test_resume.py b/python/frameworks/google-genai-live/tests/test_resume.py new file mode 100644 index 000000000..56a9d4c01 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/test_resume.py @@ -0,0 +1,132 @@ +"""goAway, resume with the stored handle, resume failure, unknown handle (J4; AC-10..AC-14).""" + +from __future__ import annotations + +import asyncio + +import pytest +from google.genai import errors +from opentelemetry.trace import StatusCode + +from .conftest import AUDIO_CONFIG, HANDLE_1, HANDLE_2, load, run_turn, spans_by_name + +MODEL = "gemini-3.8-live" + + +def resume_config(handle=None): + return dict(AUDIO_CONFIG, session_resumption={"handle": handle} if handle else {}) + + +async def _first_connection(client, *, stop_after_go_away: bool = True): + handle = None + async with client.aio.live.connect(model=MODEL, config=resume_config()) as session: + for msg in await run_turn(session): + if msg.session_resumption_update and msg.session_resumption_update.new_handle: + handle = msg.session_resumption_update.new_handle + async for msg in session.receive(): + if msg.go_away is not None and stop_after_go_away: + break + return handle + + +async def test_ac10_go_away_does_not_close_root(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j4_first_connection")) + async with client.aio.live.connect(model=MODEL, config=resume_config()) as session: + await run_turn(session) + it = session.receive() + msg = await it.__anext__() + assert msg.go_away is not None + names = [s.name for s in exporter.get_finished_spans()] + assert "gemini.live.session" not in names + await it.aclose() + + +async def test_ac11_resume_with_stored_handle_is_one_trace(server, instrument, client, exporter, log_text): + instrument() + server.scripts += [load("j4_first_connection"), load("j4_second_connection")] + handle = await _first_connection(client) + assert handle == HANDLE_1 + # The connection ended after goAway: the root waits for a resume. + assert "gemini.live.session" not in [s.name for s in exporter.get_finished_spans()] + async with client.aio.live.connect(model=MODEL, config=resume_config(handle)) as session: + await run_turn(session) + spans = exporter.get_finished_spans() + roots = [s for s in spans if s.parent is None] + assert len(roots) == 1 + assert len({s.context.trace_id for s in spans}) == 1 + by = spans_by_name(exporter) + (reconnect,) = by["gemini.live.reconnect"] + assert reconnect.attributes["gemini.reconnect.after_go_away"] is True + assert reconnect.attributes["gemini.reconnect.gap_ms"] >= 0 + assert reconnect.status.status_code == StatusCode.OK + root = roots[0] + assert root.attributes["gemini.go_away"] is True + assert root.attributes["gemini.time_left"] == "10s" + assert root.attributes["gemini.reconnect_count"] == 1 + assert len(by["gemini.live.setup"]) == 2 + assert len(by["gemini.live.model_turn"]) == 2 + # AC-14: neither handle is on any span, event or log line. + from .conftest import all_text + + exported = all_text(exporter) + for h in (HANDLE_1, HANDLE_2): + assert h not in exported + assert h not in log_text() + assert server.connects == 2 # both connects were the customer's + + +async def test_ac12_resume_failure_closes_error_and_does_not_reconnect(server, instrument, client, exporter): + instrument() + server.scripts += [load("j4_first_connection"), [["close", 1008, "bad handle"]]] + handle = await _first_connection(client) + with pytest.raises(errors.APIError): + async with client.aio.live.connect(model=MODEL, config=resume_config(handle)): + pass + by = spans_by_name(exporter) + root = by["gemini.live.session"][0] + assert root.status.status_code == StatusCode.ERROR + assert root.status.description == "resume_failed" + (reconnect,) = by["gemini.live.reconnect"] + assert reconnect.status.status_code == StatusCode.ERROR + assert server.connects == 2 + + +async def test_ac13_unknown_handle_is_a_new_trace(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j4_second_connection")) + async with client.aio.live.connect(model=MODEL, config=resume_config("handle-from-another-process")) as session: + await run_turn(session) + by = spans_by_name(exporter) + root = by["gemini.live.session"][0] + assert root.parent is None + assert root.attributes["gemini.resumed_unknown_handle"] is True + assert "gemini.live.reconnect" not in by + assert "handle-from-another-process" not in str(dict(root.attributes)) + + +async def test_detached_root_closes_after_resume_window(server, instrument, client, exporter): + instrument(resume_window_seconds=0.05) + server.scripts.append(load("j4_first_connection")) + await _first_connection(client) + assert "gemini.live.session" not in [s.name for s in exporter.get_finished_spans()] + await asyncio.sleep(0.15) + root = spans_by_name(exporter)["gemini.live.session"][0] + assert root.status.status_code == StatusCode.OK # goAway then a clean exit is not an error + assert root.attributes["call.total_turns"] == 1 + + +async def test_clean_exit_without_go_away_closes_immediately(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j4_second_connection")) + async with client.aio.live.connect(model=MODEL, config=resume_config()) as session: + await run_turn(session) + assert "gemini.live.session" in [s.name for s in exporter.get_finished_spans()] + + +async def test_uninstrument_closes_detached_sessions(server, instrument, client, exporter): + inst = instrument() + server.scripts.append(load("j4_first_connection")) + await _first_connection(client) + inst.uninstrument() + assert "gemini.live.session" in [s.name for s in exporter.get_finished_spans()] diff --git a/python/frameworks/google-genai-live/tests/test_session.py b/python/frameworks/google-genai-live/tests/test_session.py new file mode 100644 index 000000000..e5409ef54 --- /dev/null +++ b/python/frameworks/google-genai-live/tests/test_session.py @@ -0,0 +1,347 @@ +"""Session, turn, flag and tool mapping (J1-J3, J5, J6, J8; AC-03..AC-10, AC-15, AC-16, AC-21, AC-23).""" + +from __future__ import annotations + +import asyncio +import json + +import pytest +from google.genai import errors, types +from opentelemetry.trace import StatusCode + +from ._voice_tab import assert_not_voice_tab, assert_voice_tab +from .conftest import AUDIO_CONFIG, load, run_turn, spans_by_name + +MODEL = "gemini-3.8-live" + + +async def test_j1_realtime_audio_is_one_conversation(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j1_realtime_audio")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + for _ in range(5): + await session.send_realtime_input(audio=types.Blob(data=b"\x01\x02" * 1600, + mime_type="audio/pcm;rate=16000")) + await run_turn(session) + spans = exporter.get_finished_spans() + by = spans_by_name(exporter) + root = assert_voice_tab(spans, forbidden_substrings=("fixture-resumption-handle",)) + attrs = dict(root.attributes) + assert root.parent is None + assert attrs["fi.span.kind"] == "CONVERSATION" + assert attrs["gen_ai.system"] == "google_genai" + assert attrs["gen_ai.request.model"] == MODEL + assert attrs["gemini.session_id_source"] == "local" + assert attrs["gemini.realtime_input_count"] == 5 + assert attrs["call.total_turns"] == 2 + assert isinstance(attrs["call.duration"], float) + # 2.0 s of user speech (voiceActivity offsets) against 3 x 960 bytes of 24 kHz PCM. + assert attrs["call.talk_ratio"] == pytest.approx((3 * 960 / 48000) / 2.0, rel=1e-3) + rows = json.loads(attrs["conversation.transcript"]) + assert rows == [{"role": "user", "content": "What is the weather in Paris?"}, + {"role": "assistant", "content": "It is sunny."}] + assert attrs["fi.conversation.transcript"] == attrs["conversation.transcript"] + assert attrs["conversation.transcript.1.message.role"] == "assistant" + + (user,) = by["gemini.live.user_turn"] + assert user.attributes["gemini.turn_start"] == "server_activity" + assert user.attributes["gemini.turn_boundary"] == "activity_end" + assert user.attributes["input.value"] == "What is the weather in Paris?" + (model,) = by["gemini.live.model_turn"] + assert model.parent.span_id == root.context.span_id + assert model.attributes["fi.span.kind"] == "LLM" + assert model.attributes["llm.model_name"] == MODEL + assert model.attributes["output.value"] == "It is sunny." + assert model.attributes["input.value"] == "What is the weather in Paris?" + assert model.attributes["llm.token_count.prompt"] == 700 + assert model.attributes["llm.token_count.completion"] == 40 + assert model.attributes["gemini.generation_complete"] is True + assert model.attributes["gemini.turn_complete"] is True + assert model.attributes["gemini.output_audio_bytes"] == 3 * 960 + (setup,) = by["gemini.live.setup"] + assert setup.attributes["url.path"].startswith("/ws/google.ai.generativelanguage.") + assert "?" not in setup.attributes["url.path"] + assert user.end_time <= model.start_time + + +async def test_j2_tool_span_closes_on_matching_response(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j2_tool")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await session.send_client_content(turns={"role": "user", "parts": [{"text": "Weather in Paris?"}]}) + await run_turn(session, tool_result={"result": "18C and sunny"}) + await run_turn(session) + by = spans_by_name(exporter) + (tool,) = by["gemini.live.tool"] + first, second = sorted(by["gemini.live.model_turn"], key=lambda s: s.start_time) + root = by["gemini.live.session"][0] + assert tool.parent.span_id == first.context.span_id + assert tool.attributes["tool.name"] == "get_weather" + assert tool.attributes["gen_ai.tool.call.id"] == "call_1" + assert json.loads(tool.attributes["input.value"]) == {"city": "Paris"} + assert json.loads(tool.attributes["output.value"]) == {"result": "18C and sunny"} + assert tool.status.status_code == StatusCode.OK + # The following model turn is a sibling of the first, not a child of the tool. + assert second.parent.span_id == root.context.span_id + assert first.attributes["gemini.turn_start"] == "tool_call" + assert first.attributes["llm.token_count.prompt"] == 616 + assert second.attributes["llm.token_count.prompt"] == 648 + assert json.loads(first.attributes["output.value"]) == [{"name": "get_weather", "args": {"city": "Paris"}}] + assert "18C and sunny" in second.attributes["input.value"] + assert second.attributes["output.value"] == "Paris is 18C and sunny." + (user,) = by["gemini.live.user_turn"] + assert user.attributes["gemini.input_mode"] == "client_content" + assert user.attributes["gemini.turn_boundary"] == "client_turn_complete" + + +async def test_j3_interrupt_then_cancel_and_late_response_ignored(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j3_interrupt_cancel")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + msgs = await run_turn(session) + assert any(m.tool_call_cancellation for m in msgs) + # The customer answers anyway: not attached to the cancelled span (AC-09). + await session.send_tool_response(function_responses=[ + types.FunctionResponse(id="call_9", name="get_weather", response={"result": "LATE-RESULT"})]) + by = spans_by_name(exporter) + (tool,) = by["gemini.live.tool"] + assert tool.attributes["gemini.cancelled"] is True + assert tool.status.status_code == StatusCode.OK + assert "output.value" not in tool.attributes + (model,) = by["gemini.live.model_turn"] + assert model.attributes["gemini.interrupted"] is True + assert model.attributes["gemini.close_reason"] == "interrupted" + (late,) = by["gemini.live.tool_response"] + assert late.attributes["gemini.ignored"] is True + assert late.parent.span_id == by["gemini.live.session"][0].context.span_id + assert "LATE-RESULT" not in json.dumps(dict(late.attributes)) + + +@pytest.mark.parametrize("fixture,open_after,flag", [ + ("flag_generation_complete", True, "gemini.generation_complete"), + ("flag_turn_complete", False, "gemini.turn_complete"), + ("flag_interrupted", False, "gemini.interrupted"), +]) +async def test_ac05_three_flags(server, instrument, client, exporter, fixture, open_after, flag): + instrument() + server.scripts.append(load(fixture)) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + it = session.receive() + await it.__anext__() # model audio + await it.__anext__() # the flag + await it.aclose() + names = [s.name for s in exporter.get_finished_spans()] + assert ("gemini.live.model_turn" not in names) is open_after + model = spans_by_name(exporter)["gemini.live.model_turn"][0] + assert model.attributes.get(flag) is True + if fixture == "flag_generation_complete": + assert model.attributes["gemini.close_reason"] == "connection_end" + + +async def test_interrupted_wins_over_later_turn_complete(server, instrument, client, exporter): + instrument() + server.scripts.append(load("flag_interrupted")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await run_turn(session) + (model,) = spans_by_name(exporter)["gemini.live.model_turn"] + assert model.attributes["gemini.interrupted"] is True + assert "gemini.turn_complete" not in model.attributes + + +async def test_ac06_client_content_interrupts_open_model_turn(server, instrument, client, exporter): + instrument() + server.scripts.append(load("flag_generation_complete")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + it = session.receive() + await it.__anext__() + await it.aclose() + await session.send_client_content(turns={"role": "user", "parts": [{"text": "Stop, new question"}]}) + by = spans_by_name(exporter) + (model,) = by["gemini.live.model_turn"] + assert model.attributes["gemini.interrupted"] is True + assert model.attributes["gemini.close_reason"] == "client_content" + (user,) = by["gemini.live.user_turn"] + assert user.start_time >= model.start_time + + +async def test_ac07_audio_frames_are_counted_not_spanned(server, instrument, client, exporter): + instrument() + server.scripts.append([{"setupComplete": {}}]) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + for _ in range(100): + await session.send_realtime_input(audio=types.Blob(data=b"\x00" * 320, mime_type="audio/pcm;rate=16000")) + names = [s.name for s in exporter.get_finished_spans()] + assert names.count("gemini.live.user_turn") == 0 + root = spans_by_name(exporter)["gemini.live.session"][0] + assert root.attributes["gemini.realtime_input_count"] == 100 + assert root.attributes["gemini.realtime_audio_bytes"] == 32000 + assert len(names) == 2 # root + setup + + +async def test_ac08_unknown_tool_response_is_error_on_root(server, instrument, client, exporter): + instrument() + server.scripts.append([{"setupComplete": {}}]) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await session.send_tool_response(function_responses=[ + types.FunctionResponse(id="nope", name="get_weather", response={"x": 1})]) + by = spans_by_name(exporter) + (late,) = by["gemini.live.tool_response"] + assert late.attributes["gemini.unmatched"] is True + assert late.status.status_code == StatusCode.ERROR + assert late.parent.span_id == by["gemini.live.session"][0].context.span_id + + +async def test_duplicate_tool_call_id_opens_one_span(server, instrument, client, exporter): + instrument() + call = {"toolCall": {"functionCalls": [{"name": "f", "args": {}, "id": "dup"}]}} + server.scripts.append([{"setupComplete": {}}, call, call, {"serverContent": {"turnComplete": True}}]) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await run_turn(session, tool_result={"ok": True}) + assert len(spans_by_name(exporter)["gemini.live.tool"]) == 1 + + +async def test_ac03_text_session_is_chain_and_not_listed(server, instrument, client, exporter): + instrument() + server.scripts.append(load("text_session")) + async with client.aio.live.connect(model=MODEL, config={"response_modalities": ["TEXT"]}) as session: + await session.send_client_content(turns={"role": "user", "parts": [{"text": "hello"}]}) + await run_turn(session) + spans = exporter.get_finished_spans() + root = assert_not_voice_tab(spans) + assert root.attributes["fi.span.kind"] == "CHAIN" + assert "call.duration" not in root.attributes + assert "conversation.transcript" not in root.attributes + (model,) = spans_by_name(exporter)["gemini.live.model_turn"] + assert model.attributes["output.value"] == "Hi there." + + +async def test_default_modality_is_audio(server, instrument, client, exporter): + instrument() + server.scripts.append([{"setupComplete": {}}]) + async with client.aio.live.connect(model=MODEL): + pass + assert_voice_tab(exporter.get_finished_spans()) + + +async def test_ac04_ac23_setup_rejected_is_one_error_root(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j8_setup_rejected")) + with pytest.raises(errors.APIError): + async with client.aio.live.connect(model="models/x", config=AUDIO_CONFIG): + pass + spans = exporter.get_finished_spans() + by = spans_by_name(exporter) + assert set(by) == {"gemini.live.session", "gemini.live.setup"} + root = assert_voice_tab(spans, omissions=("call.total_turns", "call.talk_ratio", "conversation.transcript", + "fi.conversation.transcript", "user.id")) + assert root.status.status_code == StatusCode.ERROR + assert root.status.description == "setup_rejected" + assert by["gemini.live.setup"][0].status.status_code == StatusCode.ERROR + assert by["gemini.live.setup"][0].attributes["gemini.close_code"] == 1008 + + +async def test_ac15_ac16_socket_death_reraises_and_flushes_once(server, instrument, client, exporter, provider): + flushes = [] + provider.force_flush = lambda *a, **k: flushes.append(1) or True # type: ignore[method-assign] + inst = instrument() + server.scripts.append(load("j6_socket_death")) + with pytest.raises(errors.APIError) as raised: + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await run_turn(session) + assert raised.value.code == 1006 # the exception the caller sees is the SDK's own + by = spans_by_name(exporter) + root = by["gemini.live.session"][0] + assert root.status.status_code == StatusCode.ERROR + assert root.status.description == "socket_closed" + (model,) = by["gemini.live.model_turn"] + assert model.status.status_code == StatusCode.ERROR + assert model.attributes["gemini.close_reason"] == "socket_closed" + events = [e.name for e in root.events] + assert "gemini.receive_error" in events + assert server.connects == 1 # no reconnect from the instrumentor + await asyncio.sleep(0.05) + assert inst._inst.flush_count == 1 + assert len(flushes) == 1 + + +async def test_task_cancellation_closes_error_task_cancelled(server, instrument, client, exporter): + instrument() + gate = asyncio.Event() + server.scripts.append([{"setupComplete": {}}]) + + async def body(): + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG): + await gate.wait() + + task = asyncio.create_task(body()) + await asyncio.sleep(0.01) + task.cancel() + with pytest.raises(asyncio.CancelledError): + await task + root = spans_by_name(exporter)["gemini.live.session"][0] + assert root.status.description == "task_cancelled" + + +async def test_ac21_usage_is_copied_not_summed(server, instrument, client, exporter): + instrument() + server.scripts.append(load("j2_tool")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await run_turn(session, tool_result={"r": 1}) + await run_turn(session) + values = [] + for span in exporter.get_finished_spans(): + for key, value in (span.attributes or {}).items(): + if "token" in key: + values.append(value) + for ev in span.events: + values.extend(v for k, v in ev.attributes.items() if "token" in k) + assert 628 in values and 729 in values + assert 628 + 729 not in values + assert 616 + 648 not in values + + +async def test_customer_session_and_user_ids(server, instrument, client, exporter): + from fi_instrumentation import using_session, using_user + + instrument() + server.scripts.append([{"setupComplete": {}}]) + with using_session("call-42"), using_user("user-7"): + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG): + pass + root = spans_by_name(exporter)["gemini.live.session"][0] + assert root.attributes["session.id"] == "call-42" + assert root.attributes["gemini.session_id_source"] == "customer" + assert root.attributes["user.id"] == "user-7" + + +async def test_real_recorded_audio_session(server, instrument, client, exporter): + """Frames recorded from gemini-3.8-live on 2026-10-15 (audio replaced by silence).""" + instrument() + server.scripts.append(load("real_20261015_audio_tool")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await run_turn(session, tool_result={"result": "18C and sunny"}) + await run_turn(session) + by = spans_by_name(exporter) + root = assert_voice_tab(exporter.get_finished_spans(), forbidden_substrings=("fixture-resumption-handle",)) + (user,) = by["gemini.live.user_turn"] + assert user.attributes["gemini.turn_boundary"] == "activity_end" + assert "Paris" in user.attributes["input.value"] + assert len(by["gemini.live.model_turn"]) == 2 + (tool,) = by["gemini.live.tool"] + assert tool.attributes["tool.name"] == "get_weather" + assert root.attributes["call.total_turns"] == 3 + assert "call.talk_ratio" in root.attributes + + +async def test_real_recorded_text_input_session(server, instrument, client, exporter): + instrument() + server.scripts.append(load("real_20261015_text_tool")) + async with client.aio.live.connect(model=MODEL, config=AUDIO_CONFIG) as session: + await session.send_client_content(turns={"role": "user", "parts": [{"text": "What's the weather in Paris?"}]}) + await run_turn(session, tool_result={"result": "18C and sunny"}) + await run_turn(session) + by = spans_by_name(exporter) + assert len(by["gemini.live.model_turn"]) == 2 + assert by["gemini.live.tool"][0].status.status_code == StatusCode.OK + root = by["gemini.live.session"][0] + assert "call.talk_ratio" not in root.attributes # no measured user speech diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py b/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py new file mode 100644 index 000000000..3084eb9c5 --- /dev/null +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py @@ -0,0 +1,138 @@ +"""Future AGI tracing for Gemini Live sessions opened with ``client.aio.live.connect``. + +Gemini Developer API only. One Live session is one trace, including a reconnect that +passes the resumption handle this process stored. Audio bytes, API keys and resumption +handles never land on a span. +""" + +from __future__ import annotations + +import atexit +import logging +from typing import Any, Collection, Optional + +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor # type: ignore +from opentelemetry.trace import get_tracer, get_tracer_provider +from wrapt import wrap_function_wrapper + +from fi_instrumentation import FITracer, TraceConfig +from traceai_google_genai_live._connect import ( + ClientContentWrapper, + ConnectWrapper, + RealtimeInputWrapper, + ReceiveWrapper, + ToolResponseWrapper, + _Instrumentation, +) +from traceai_google_genai_live._redact import policy_from +from traceai_google_genai_live.package import _instruments +from traceai_google_genai_live.version import __version__ + +__all__ = ["GoogleGenAILiveInstrumentor", "instrument", "__version__", "PATCHED"] + +logger = logging.getLogger(__name__) +logger.addHandler(logging.NullHandler()) + +_MODULE = "google.genai.live" +# Everything this package patches. `Live.send`, `Live.start_stream`, `Models.generate_content` +# and every LiveKit / Pipecat class are deliberately absent. +PATCHED = ( + ("AsyncLive", "connect"), + ("AsyncSession", "send_client_content"), + ("AsyncSession", "send_realtime_input"), + ("AsyncSession", "send_tool_response"), + ("AsyncSession", "receive"), +) + + +class GoogleGenAILiveInstrumentor(BaseInstrumentor): # type: ignore[misc] + """Instrument ``google.genai.live.AsyncLive.connect`` and the session methods that remain. + + Keyword arguments to ``instrument()``: + + - ``tracer_provider``: the provider from ``fi_instrumentation.register()``. + - ``config``: a ``TraceConfig``. ``hide_inputs`` / ``FI_HIDE_INPUTS`` and + ``hide_outputs`` / ``FI_HIDE_OUTPUTS`` drop the matching content. + - ``metadata_only``: drop all content (system instruction, transcriptions, user text, + tool arguments and results, model text, error text). Model, voice name, timings, + tool names, flags and token counts stay. + - ``session_id`` / ``user_id``: defaults for every session. ``fi_instrumentation``'s + ``using_session`` / ``using_user`` context managers override them per connect. + - ``resume_window_seconds``: how long a session whose connection ended after + ``goAway`` (or died) waits for a reconnect with its stored handle. Default 30. + """ + + def instrumentation_dependencies(self) -> Collection[str]: + return _instruments + + def _instrument(self, **kwargs: Any) -> None: + tracer_provider = kwargs.get("tracer_provider") or get_tracer_provider() + config = kwargs.get("config") + if config is None: + config = TraceConfig() + elif not isinstance(config, TraceConfig): + raise TypeError("config must be a fi_instrumentation.TraceConfig") + tracer = FITracer(get_tracer(__name__, __version__, tracer_provider), config=config) + self._inst = _Instrumentation( + tracer=tracer, + tracer_provider=tracer_provider, + policy=policy_from(config, bool(kwargs.get("metadata_only", False))), + session_id=kwargs.get("session_id"), + user_id=kwargs.get("user_id"), + resume_window_seconds=float(kwargs.get("resume_window_seconds", 30.0)), + flush_timeout_millis=int(kwargs.get("flush_timeout_millis", 5000)), + ) + + import google.genai.live as live # noqa: F401 (fail loudly if the SDK moved) + + self._originals = {} + wrappers = { + ("AsyncLive", "connect"): ConnectWrapper(self._inst), + ("AsyncSession", "send_client_content"): ClientContentWrapper(), + ("AsyncSession", "send_realtime_input"): RealtimeInputWrapper(), + ("AsyncSession", "send_tool_response"): ToolResponseWrapper(), + ("AsyncSession", "receive"): ReceiveWrapper(), + } + for (cls_name, method), wrapper in wrappers.items(): + cls = getattr(live, cls_name) + self._originals[(cls_name, method)] = cls.__dict__[method] + wrap_function_wrapper(_MODULE, f"{cls_name}.{method}", wrapper) + # Sessions still waiting for a resume are closed before the provider shuts down + # (atexit runs in reverse registration order and register() came first). + self._atexit = self._inst.close_all + atexit.register(self._atexit) + + def _uninstrument(self, **kwargs: Any) -> None: + import google.genai.live as live + + inst = getattr(self, "_inst", None) + if inst is not None: + inst.close_all() + for (cls_name, method), original in getattr(self, "_originals", {}).items(): + setattr(getattr(live, cls_name), method, original) + self._originals = {} + if getattr(self, "_atexit", None) is not None: + atexit.unregister(self._atexit) + self._atexit = None + + +def instrument( + tracer_provider: Any = None, + *, + config: Optional[TraceConfig] = None, + metadata_only: bool = False, + session_id: Optional[str] = None, + user_id: Optional[str] = None, + resume_window_seconds: float = 30.0, +) -> GoogleGenAILiveInstrumentor: + """Shorthand for ``GoogleGenAILiveInstrumentor().instrument(...)``. Returns the instrumentor.""" + instrumentor = GoogleGenAILiveInstrumentor() + instrumentor.instrument( + tracer_provider=tracer_provider, + config=config, + metadata_only=metadata_only, + session_id=session_id, + user_id=user_id, + resume_window_seconds=resume_window_seconds, + ) + return instrumentor diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/_connect.py b/python/frameworks/google-genai-live/traceai_google_genai_live/_connect.py new file mode 100644 index 000000000..9a3b877fa --- /dev/null +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/_connect.py @@ -0,0 +1,338 @@ +"""Wrappers for ``AsyncLive.connect`` and the four ``AsyncSession`` methods that remain. + +``Live.send`` and ``Live.start_stream`` are deprecated upstream and are not wrapped. +""" + +from __future__ import annotations + +import logging +import threading +import time +from typing import Any, Dict, Optional + +from opentelemetry import context as context_api +from opentelemetry.context import _SUPPRESS_INSTRUMENTATION_KEY + +from ._redact import ContentPolicy, _get, as_list, text_of_turns, url_path +from ._session import SessionTrace + +logger = logging.getLogger(__name__) +logger.addHandler(logging.NullHandler()) + +_STATE_ATTR = "_fi_gemini_live_trace" +_warned: set = set() + + +def _warn_once(key: str, message: str) -> None: + if key not in _warned: + _warned.add(key) + logger.warning(message) + + +class _Instrumentation: + """Shared state for one ``instrument()`` call.""" + + def __init__(self, *, tracer: Any, tracer_provider: Any, policy: ContentPolicy, + session_id: Optional[str], user_id: Optional[str], + resume_window_seconds: float, flush_timeout_millis: int) -> None: + self.tracer = tracer + self.tracer_provider = tracer_provider + self.policy = policy + self.session_id = session_id + self.user_id = user_id + self.resume_window_seconds = resume_window_seconds + self.flush_timeout_millis = flush_timeout_millis + # Resumption handle -> open or detached session. Memory only. + self._by_handle: Dict[str, SessionTrace] = {} + self._live: set = set() + self.flush_count = 0 + + # handles ------------------------------------------------------------- + def remember_handle(self, trace: SessionTrace, handle: str) -> None: + if trace.handle is not None: + self._by_handle.pop(trace.handle, None) + trace.handle = handle + trace.handle_resumable = True + if not trace.closed: + self._by_handle[handle] = trace + + def lookup(self, handle: Optional[str]) -> Optional[SessionTrace]: + if not handle: + return None + trace = self._by_handle.get(handle) + if trace is None or trace.closed: + return None + return trace + + def track(self, trace: SessionTrace) -> None: + self._live.add(trace) + + def forget(self, trace: SessionTrace) -> None: + self._live.discard(trace) + if trace.handle is not None: + self._by_handle.pop(trace.handle, None) + trace.handle = None + + def close_all(self) -> None: + """End every session still open or waiting for a resume (process exit, uninstrument).""" + for trace in list(self._live): + try: + if trace.detached_ns is not None: + trace.expire() + else: + trace.close(error=None) + except Exception: + pass + + def flush(self) -> None: + """One flush per closed session, off the event loop, never blocking the socket reader.""" + provider = self.tracer_provider + force_flush = getattr(provider, "force_flush", None) + if force_flush is None: + return + self.flush_count += 1 + timeout = self.flush_timeout_millis + + def run() -> None: + try: + force_flush(timeout) + except Exception: + pass + + threading.Thread(target=run, name="fi-gemini-live-flush", daemon=False).start() + + +# ---------------------------------------------------------------------- config parsing +def _modalities_include_audio(config: Any) -> bool: + modalities = _get(config, "response_modalities") + if modalities is None: + modalities = _get(_get(config, "generation_config"), "response_modalities") + if not modalities: + # The SDK documents AUDIO as the default when nothing is requested. + return True + for m in as_list(modalities): + name = getattr(m, "value", None) or getattr(m, "name", None) or str(m) + if str(name).upper().endswith("AUDIO"): + return True + return False + + +def _setup_info(config: Any) -> Dict[str, Any]: + info: Dict[str, Any] = {} + modalities = _get(config, "response_modalities") or _get(_get(config, "generation_config"), + "response_modalities") + if modalities: + info["gemini.response_modalities"] = [ + str(getattr(m, "value", None) or getattr(m, "name", None) or m) for m in as_list(modalities)] + voice = _get(_get(_get(_get(config, "speech_config"), "voice_config"), "prebuilt_voice_config"), + "voice_name") + if voice: + info["gemini.voice_name"] = str(voice) + if _get(config, "context_window_compression") is not None: + info["gemini.context_window_compression"] = True + if _get(config, "session_resumption") is not None: + info["gemini.session_resumption"] = True + if _get(config, "input_audio_transcription") is not None: + info["gemini.input_audio_transcription"] = True + if _get(config, "output_audio_transcription") is not None: + info["gemini.output_audio_transcription"] = True + names = [] + for tool in as_list(_get(config, "tools")): + for decl in as_list(_get(tool, "function_declarations")): + name = _get(decl, "name") + if name: + names.append(str(name)) + if names: + info["gemini.tool_names"] = names + return info + + +def _system_instruction(config: Any) -> Optional[str]: + value = _get(config, "system_instruction") + if value is None: + return None + if isinstance(value, str): + return value + return text_of_turns(value) or None + + +def _resume_handle(config: Any) -> Optional[str]: + handle = _get(_get(config, "session_resumption"), "handle") + return str(handle) if handle else None + + +# ---------------------------------------------------------------------- connect +class _TracedConnect: + """Async context manager returned in place of the SDK's ``connect`` context manager.""" + + def __init__(self, inst: _Instrumentation, wrapped: Any, instance: Any, args: Any, + kwargs: Dict[str, Any]) -> None: + self._inst = inst + self._cm = wrapped(*args, **kwargs) + self._instance = instance + self._kwargs = kwargs + self._trace: Optional[SessionTrace] = None + self._session: Any = None + + async def __aenter__(self) -> Any: + trace = None + setup_span = None + reconnect_span = None + try: + trace, setup_span, reconnect_span = self._begin() + except Exception: + logger.debug("gemini live: session not traced", exc_info=False) + trace = None + try: + session = await self._cm.__aenter__() + except BaseException as exc: + if trace is not None: + try: + if reconnect_span is not None: + trace.end_reconnect(reconnect_span, ok=False, exc=exc) + trace.fail_setup(setup_span, exc, resumed=reconnect_span is not None) + except Exception: + logger.debug("gemini live: setup failure not recorded", exc_info=False) + raise + if trace is not None: + try: + trace.end_setup(setup_span, session) + if reconnect_span is not None: + trace.end_reconnect(reconnect_span, ok=True) + setattr(session, _STATE_ATTR, trace) + except Exception: + logger.debug("gemini live: setup not recorded", exc_info=False) + self._trace = trace + self._session = session + return session + + def _begin(self): + if context_api.get_value(_SUPPRESS_INSTRUMENTATION_KEY): + return None, None, None + api_client = getattr(self._instance, "_api_client", None) + if getattr(api_client, "vertexai", False): + _warn_once("vertex", "traceai-google-genai-live: Vertex AI Live sessions are not " + "supported; this session is not traced.") + return None, None, None + api_key = getattr(api_client, "api_key", None) + if isinstance(api_key, str) and api_key.startswith("auth_tokens/"): + _warn_once("ephemeral", "traceai-google-genai-live: ephemeral-token sessions " + "(BidiGenerateContentConstrained) are not supported; " + "this session is not traced.") + return None, None, None + model = self._kwargs.get("model") + config = self._kwargs.get("config") + handle = _resume_handle(config) + version = getattr(getattr(api_client, "_http_options", None), "api_version", None) or "v1beta" + path = url_path(f"/ws/google.ai.generativelanguage.{version}.GenerativeService.BidiGenerateContent") + inst = self._inst + trace = inst.lookup(handle) + reconnect_span = None + if trace is not None: + reconnect_span = trace.start_reconnect() + else: + from fi_instrumentation.fi_types import SpanAttributes + + session_id = context_api.get_value(SpanAttributes.SESSION_ID) or inst.session_id + user_id = context_api.get_value(SpanAttributes.USER_ID) or inst.user_id + info = _setup_info(config) + if handle: + info["gemini.resumed_unknown_handle"] = True + trace = SessionTrace(inst, model=str(model) if model else None, + audio=_modalities_include_audio(config), setup_info=info, + session_id=str(session_id) if session_id else None, + user_id=str(user_id) if user_id else None) + inst.track(trace) + setup_span = trace.start_setup(system_instruction=_system_instruction(config), path=path, + resumed=reconnect_span is not None) + return trace, setup_span, reconnect_span + + async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> Any: + try: + result = await self._cm.__aexit__(exc_type, exc, tb) + except BaseException as inner: + self._finish(inner) + raise + self._finish(None if result else exc) + return result + + def _finish(self, exc: Optional[BaseException]) -> None: + trace = self._trace + if trace is None: + return + self._trace = None + try: + if self._session is not None and getattr(self._session, _STATE_ATTR, None) is trace: + setattr(self._session, _STATE_ATTR, None) + except Exception: + pass + trace.on_connection_exit(exc) + + +def _trace_of(instance: Any) -> Optional[SessionTrace]: + trace = getattr(instance, _STATE_ATTR, None) + if trace is None or trace.closed: + return None + return trace + + +class ConnectWrapper: + def __init__(self, inst: _Instrumentation) -> None: + self._inst = inst + + def __call__(self, wrapped: Any, instance: Any, args: Any, kwargs: Any) -> Any: + return _TracedConnect(self._inst, wrapped, instance, args, kwargs) + + +class ClientContentWrapper: + async def __call__(self, wrapped: Any, instance: Any, args: Any, kwargs: Any) -> Any: + trace = _trace_of(instance) + start = time.time_ns() + result = await wrapped(*args, **kwargs) + if trace is not None: + trace.on_client_content(kwargs.get("turns"), kwargs.get("turn_complete", True), start) + return result + + +class RealtimeInputWrapper: + async def __call__(self, wrapped: Any, instance: Any, args: Any, kwargs: Any) -> Any: + trace = _trace_of(instance) + start = time.time_ns() + result = await wrapped(*args, **kwargs) + if trace is not None: + trace.on_realtime_input(kwargs, start) + return result + + +class ToolResponseWrapper: + async def __call__(self, wrapped: Any, instance: Any, args: Any, kwargs: Any) -> Any: + trace = _trace_of(instance) + result = await wrapped(*args, **kwargs) + if trace is not None: + trace.on_tool_response(kwargs.get("function_responses")) + return result + + +class ReceiveWrapper: + def __call__(self, wrapped: Any, instance: Any, args: Any, kwargs: Any) -> Any: + return _traced_receive(wrapped(*args, **kwargs), instance) + + +async def _traced_receive(agen: Any, instance: Any): + trace = _trace_of(instance) + try: + async for message in agen: + if trace is not None: + trace.on_server_message(message) + yield message + except Exception as exc: + if trace is not None: + trace.on_receive_error(exc) + raise + finally: + aclose = getattr(agen, "aclose", None) + if aclose is not None: + try: + await aclose() + except Exception: + pass diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py b/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py new file mode 100644 index 000000000..e4817895c --- /dev/null +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py @@ -0,0 +1,169 @@ +"""What may land on a span. Audio bytes, API keys and resumption handles never do.""" + +from __future__ import annotations + +import json +from dataclasses import dataclass +from typing import Any, Iterable, List, Optional +from urllib.parse import urlsplit + +from fi_instrumentation import TraceConfig + + +@dataclass(frozen=True) +class ContentPolicy: + """Which content directions are captured. + + ``capture_inputs`` covers the system instruction, user text and input transcription, + tool arguments and the prompt shown on a model turn. ``capture_outputs`` covers model + text and output transcription and tool results. The conversation transcript and raw + error text need both. Metadata (model, voice name, timings, tool names, flags, token + counts, close codes) is always kept. + """ + + capture_inputs: bool = True + capture_outputs: bool = True + + @property + def full(self) -> bool: + return self.capture_inputs and self.capture_outputs + + +def policy_from(config: Optional[TraceConfig], metadata_only: bool) -> ContentPolicy: + if metadata_only: + return ContentPolicy(False, False) + config = config or TraceConfig() + return ContentPolicy( + capture_inputs=not bool(getattr(config, "hide_inputs", False)), + capture_outputs=not bool(getattr(config, "hide_outputs", False)), + ) + + +def url_path(url: Any) -> Optional[str]: + """Path only. Scheme, host, port, user info and query string are dropped.""" + if not url: + return None + try: + return urlsplit(str(url)).path or None + except ValueError: + return None + + +def _get(obj: Any, name: str, default: Any = None) -> Any: + if obj is None: + return default + if isinstance(obj, dict): + if name in obj: + return obj[name] + camel = "".join(p.capitalize() if i else p for i, p in enumerate(name.split("_"))) + return obj.get(camel, default) + return getattr(obj, name, default) + + +def as_list(value: Any) -> List[Any]: + if value is None: + return [] + if isinstance(value, (list, tuple)): + return list(value) + return [value] + + +def text_of_parts(parts: Iterable[Any], *, skip_thoughts: bool = True) -> str: + """Concatenated text parts. Inline data (audio, images) is never read.""" + out = [] + for part in parts or []: + if isinstance(part, str): + out.append(part) + continue + if skip_thoughts and _get(part, "thought"): + continue + text = _get(part, "text") + if isinstance(text, str) and text: + out.append(text) + return "".join(out) + + +def text_of_turns(turns: Any) -> str: + """Text of the Content objects (or dicts, or strings) passed to send_client_content.""" + chunks = [] + for item in as_list(turns): + if isinstance(item, str): + chunks.append(item) + continue + text = text_of_parts(as_list(_get(item, "parts"))) + if text: + chunks.append(text) + return "\n".join(chunks) + + +def roles_of_turns(turns: Any) -> List[str]: + roles = [] + for item in as_list(turns): + role = "user" if isinstance(item, str) else _get(item, "role") + if role: + roles.append(str(role)) + return roles + + +def inline_audio(parts: Iterable[Any]) -> List[tuple]: + """(byte_count, mime_type) for each inline-data part. The bytes themselves are not kept.""" + found = [] + for part in parts or []: + blob = _get(part, "inline_data") + if blob is None: + continue + data = _get(blob, "data") + size = len(data) if isinstance(data, (bytes, bytearray)) else 0 + found.append((size, _get(blob, "mime_type") or "")) + return found + + +def pcm_rate(mime_type: str) -> Optional[int]: + """Sample rate from ``audio/pcm;rate=24000``. None when the type is not raw PCM with a rate.""" + if not mime_type or not str(mime_type).lower().startswith("audio/pcm"): + return None + for piece in str(mime_type).split(";")[1:]: + key, _, value = piece.strip().partition("=") + if key.strip().lower() == "rate": + try: + rate = int(value.strip()) + except ValueError: + return None + return rate if rate > 0 else None + return None + + +def to_json(value: Any) -> str: + """JSON for tool args and results. Bytes are replaced by their length, never encoded.""" + + def scrub(obj: Any) -> Any: + if isinstance(obj, (bytes, bytearray)): + return f"<{len(obj)} bytes>" + if hasattr(obj, "model_dump"): + try: + obj = obj.model_dump(exclude_none=True) + except Exception: + return str(type(obj).__name__) + if isinstance(obj, dict): + return {str(k): scrub(v) for k, v in obj.items()} + if isinstance(obj, (list, tuple)): + return [scrub(v) for v in obj] + return obj + + try: + return json.dumps(scrub(value), ensure_ascii=False, default=str) + except Exception: + return "{}" + + +def seconds(offset: Any) -> Optional[float]: + """Parse a protobuf Duration string such as ``4.720s``.""" + if offset is None: + return None + text = str(offset).strip() + if text.endswith("s"): + text = text[:-1] + try: + return float(text) + except ValueError: + return None diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/_session.py b/python/frameworks/google-genai-live/traceai_google_genai_live/_session.py new file mode 100644 index 000000000..2ad91eca6 --- /dev/null +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/_session.py @@ -0,0 +1,769 @@ +"""One Gemini Live session as one trace. + +The root opens when ``connect`` is entered. Turns, tool calls and the reconnect gap are +children of the root. A reconnect that passes the resumption handle this process stored +attaches to the same root. Nothing here opens a socket, retries or reconnects. + +Every public ``on_*`` method swallows its own errors: instrumentation must never change +what the customer's code sees. +""" + +from __future__ import annotations + +import asyncio +import json +import logging +import time +import uuid +from typing import TYPE_CHECKING, Any, Dict, List, Optional + +from opentelemetry import context as context_api +from opentelemetry import trace as trace_api +from opentelemetry.trace import Status, StatusCode + +from ._redact import ( + ContentPolicy, + _get, + as_list, + inline_audio, + pcm_rate, + roles_of_turns, + seconds, + text_of_parts, + text_of_turns, + to_json, +) + +if TYPE_CHECKING: # pragma: no cover + from ._connect import _Instrumentation + +logger = logging.getLogger(__name__) +logger.addHandler(logging.NullHandler()) + +FI_SPAN_KIND = "fi.span.kind" +SYSTEM = "google_genai" + +ROOT_NAME = "gemini.live.session" +SETUP_NAME = "gemini.live.setup" +USER_TURN_NAME = "gemini.live.user_turn" +MODEL_TURN_NAME = "gemini.live.model_turn" +TOOL_NAME = "gemini.live.tool" +TOOL_RESPONSE_NAME = "gemini.live.tool_response" +RECONNECT_NAME = "gemini.live.reconnect" + +# usageMetadata fields copied as the server sent them (never summed). +_USAGE_FIELDS = ( + "prompt_token_count", + "cached_content_token_count", + "response_token_count", + "tool_use_prompt_token_count", + "thoughts_token_count", + "total_token_count", +) + + +def _now() -> int: + return time.time_ns() + + +def _enum_name(value: Any) -> str: + name = getattr(value, "name", None) or getattr(value, "value", None) or value + return str(name) + + +class _Turn: + __slots__ = ("span", "start_ns", "text", "deferred_end_ns", "boundary", "audio_bytes", + "audio_seconds", "audio_rate_known", "calls", "flags", "va_start") + + def __init__(self, span: Any, start_ns: int) -> None: + self.span = span + self.start_ns = start_ns + self.text: List[str] = [] + self.deferred_end_ns: Optional[int] = None + self.boundary: Optional[str] = None + self.audio_bytes = 0 + self.audio_seconds = 0.0 + self.audio_rate_known = True + self.calls: List[Dict[str, Any]] = [] + self.flags: Dict[str, Any] = {} + self.va_start: Optional[float] = None + + +class SessionTrace: + """State for one root span. Lives across reconnects that resume the same handle.""" + + def __init__( + self, + inst: "_Instrumentation", + *, + model: Optional[str], + audio: bool, + setup_info: Dict[str, Any], + session_id: Optional[str], + user_id: Optional[str], + ) -> None: + self.inst = inst + self.policy: ContentPolicy = inst.policy + self.model = model + self.audio = audio + self.start_ns = _now() + self.closed = False + self.detached_ns: Optional[int] = None + self._expiry: Optional[asyncio.TimerHandle] = None + self._pending_status: Optional[str] = None + self.go_away = False + self.connections = 0 + self.handle: Optional[str] = None # memory only, never on a span or in a log + self.handle_resumable = False + + self.user_turn: Optional[_Turn] = None + self.model_turn: Optional[_Turn] = None + self.tools: Dict[str, Any] = {} + self.closed_tool_ids: set = set() + self.cancelled_ids: set = set() + self.unconsumed_input: List[str] = [] + self._unconsumed_owner: Optional[_Turn] = None + self.transcript: List[Dict[str, str]] = [] + self.closed_user_turns = 0 + self.closed_model_turns = 0 + self.realtime_count = 0 + self.realtime_audio_bytes = 0 + self.user_speech_seconds = 0.0 + self.model_audio_seconds = 0.0 + self.model_audio_unmeasured = False + self._last_receive_error: Optional[str] = None + + attrs: Dict[str, Any] = { + FI_SPAN_KIND: "CONVERSATION" if audio else "CHAIN", + "gen_ai.system": SYSTEM, + } + if model: + attrs["gen_ai.request.model"] = model + attrs.update({k: v for k, v in setup_info.items() if v is not None}) + if session_id: + attrs["session.id"] = session_id + attrs["gemini.session_id_source"] = "customer" + else: + attrs["session.id"] = str(uuid.uuid4()) + attrs["gemini.session_id_source"] = "local" + if user_id: + attrs["user.id"] = user_id + # A voice call must be parentless for the Voice tab to list it. A text session + # keeps the caller's active span as its parent. + parent_ctx = context_api.Context() if audio else None + self.root = inst.tracer.start_span(ROOT_NAME, context=parent_ctx, start_time=self.start_ns, + attributes=attrs) + self.root_ctx = trace_api.set_span_in_context(self.root) + self._session_id_from_customer = bool(session_id) + + # ------------------------------------------------------------------ helpers + def _child(self, name: str, kind: str, start_ns: Optional[int] = None, + parent: Any = None, attributes: Optional[Dict[str, Any]] = None) -> Any: + ctx = trace_api.set_span_in_context(parent) if parent is not None else self.root_ctx + attrs = {FI_SPAN_KIND: kind} + if attributes: + attrs.update({k: v for k, v in attributes.items() if v is not None}) + return self.inst.tracer.start_span(name, context=ctx, start_time=start_ns or _now(), + attributes=attrs) + + def _root_event(self, name: str, attributes: Dict[str, Any]) -> None: + try: + self.root.add_event(name, {k: v for k, v in attributes.items() if v is not None}) + # Some stores keep event names only; the latest close code also lives on the root. + if isinstance(attributes.get("gemini.close_code"), int): + self.root.set_attribute("gemini.close_code", attributes["gemini.close_code"]) + except Exception: + pass + + def _error_attrs(self, exc: BaseException) -> Dict[str, Any]: + attrs: Dict[str, Any] = {"exception.type": type(exc).__name__} + code = getattr(exc, "code", None) + if isinstance(code, int): + attrs["gemini.close_code"] = code + if self.policy.full: + # Raw error text can echo prompts or local paths, so it is content. Stack + # traces are never exported. + attrs["exception.message"] = str(exc)[:2000] + return attrs + + # ------------------------------------------------------------------ setup + def start_setup(self, *, system_instruction: Optional[str], path: Optional[str], + resumed: bool) -> Any: + attrs: Dict[str, Any] = {"url.path": path, "gemini.resumed": resumed or None} + if self.model: + attrs["gen_ai.request.model"] = self.model + if system_instruction and self.policy.capture_inputs: + attrs["input.value"] = system_instruction + attrs["input.mime_type"] = "text/plain" + return self._child(SETUP_NAME, "CHAIN", attributes=attrs) + + def end_setup(self, span: Any, session: Any) -> None: + setup_complete = getattr(session, "setup_complete", None) + if setup_complete is not None: + span.set_attribute("gemini.setup_complete", True) + server_id = getattr(setup_complete, "session_id", None) + if server_id and not self._session_id_from_customer and self.connections == 0: + self.root.set_attribute("session.id", str(server_id)) + self.root.set_attribute("gemini.session_id_source", "server") + span.set_status(Status(StatusCode.OK)) + else: + span.set_attribute("gemini.setup_complete", False) + span.set_status(Status(StatusCode.ERROR, "no_setup_complete")) + span.end() + self.connections += 1 + + def fail_setup(self, span: Any, exc: BaseException, *, resumed: bool) -> None: + reason = "resume_failed" if resumed else "setup_rejected" + if isinstance(exc, asyncio.CancelledError): + reason = "task_cancelled" + span.set_attributes(self._error_attrs(exc)) + span.set_status(Status(StatusCode.ERROR, reason)) + span.end() + self._root_event("gemini.setup_failed", self._error_attrs(exc)) + self.close(error=reason) + + # ------------------------------------------------------------------ client side + def on_client_content(self, turns: Any, turn_complete: Any, start_ns: int) -> None: + try: + now = _now() + self._finish_deferred_user_turn() + if self.model_turn is not None: + # clientContent interrupts generation (E-GEM-2). + self._close_model_turn(now, interrupted=True, reason="client_content") + if self.user_turn is not None: + self._close_user_turn(now, boundary="client_content") + turn = _Turn(self._child(USER_TURN_NAME, "CHAIN", start_ns, attributes={ + "gemini.input_mode": "client_content", + "gemini.client_turn_complete": bool(turn_complete) if turn_complete is not None else True, + "gemini.turn_roles": roles_of_turns(turns) or None, + }), start_ns) + text = text_of_turns(turns) + if text: + self._add_user_text(turn, text) + self.user_turn = turn + if turn_complete is None or bool(turn_complete): + self._close_user_turn(now, boundary="client_turn_complete") + except Exception: + logger.debug("gemini live: client content not recorded", exc_info=False) + + def on_realtime_input(self, kwargs: Dict[str, Any], start_ns: int) -> None: + try: + self.realtime_count += 1 + audio = kwargs.get("audio") or kwargs.get("media") + data = _get(audio, "data") if audio is not None else None + if isinstance(data, (bytes, bytearray)): + self.realtime_audio_bytes += len(data) + text = kwargs.get("text") + if kwargs.get("activity_start") is not None: + self._open_user_turn(start_ns, "client_activity") + if isinstance(text, str) and text: + turn = self._open_user_turn(start_ns, "realtime_text") + self._add_user_text(turn, text) + if kwargs.get("activity_end") is not None and self.user_turn is not None: + self.user_turn.deferred_end_ns = _now() + self.user_turn.boundary = "activity_end" + except Exception: + logger.debug("gemini live: realtime input not recorded", exc_info=False) + + def on_tool_response(self, function_responses: Any) -> None: + try: + now = _now() + for resp in as_list(function_responses): + call_id = _get(resp, "id") + name = _get(resp, "name") + result = _get(resp, "response") + if call_id is not None: + call_id = str(call_id) + span = self.tools.get(call_id) if call_id is not None else None + if span is not None: + if self.policy.capture_outputs and result is not None: + span.set_attribute("output.value", to_json(result)) + span.set_attribute("output.mime_type", "application/json") + if _get(resp, "will_continue"): + span.set_attribute("gemini.will_continue", True) + continue + span.set_status(Status(StatusCode.OK)) + span.end(end_time=now) + del self.tools[call_id] + self.closed_tool_ids.add(call_id) + if self.policy.capture_inputs and result is not None: + self.unconsumed_input.append(to_json({"tool": name, "result": result})) + self._unconsumed_owner = None + continue + attrs: Dict[str, Any] = {"gen_ai.tool.call.id": call_id, "tool.name": name, + "gen_ai.tool.name": name} + if call_id is not None and (call_id in self.cancelled_ids or call_id in self.closed_tool_ids): + # The server cancelled (or already got) this call: the response is not + # attached to the tool span (J3). + attrs["gemini.ignored"] = True + attrs["gemini.ignored_reason"] = ( + "cancelled" if call_id in self.cancelled_ids else "duplicate") + late = self._child(TOOL_RESPONSE_NAME, "TOOL", now, attributes=attrs) + late.set_status(Status(StatusCode.OK)) + late.end(end_time=now) + else: + attrs["gemini.unmatched"] = True + late = self._child(TOOL_RESPONSE_NAME, "TOOL", now, attributes=attrs) + late.set_status(Status(StatusCode.ERROR, "unmatched_tool_response")) + late.end(end_time=now) + except Exception: + logger.debug("gemini live: tool response not recorded", exc_info=False) + + # ------------------------------------------------------------------ server side + def on_server_message(self, msg: Any) -> None: + try: + self._on_server_message(msg) + except Exception: + logger.debug("gemini live: server message not recorded", exc_info=False) + + def _on_server_message(self, msg: Any) -> None: + now = _now() + update = getattr(msg, "session_resumption_update", None) + if update is not None: + new_handle = getattr(update, "new_handle", None) + resumable = getattr(update, "resumable", None) + if new_handle and resumable is not False: + self.inst.remember_handle(self, str(new_handle)) + elif resumable is False: + self.handle_resumable = False + + go_away = getattr(msg, "go_away", None) + if go_away is not None: + self.go_away = True + self.root.set_attribute("gemini.go_away", True) + time_left = getattr(go_away, "time_left", None) + if time_left is not None: + self.root.set_attribute("gemini.time_left", str(time_left)) + + activity = getattr(msg, "voice_activity", None) + if activity is not None: + kind = _enum_name(getattr(activity, "voice_activity_type", None)) + offset = seconds(getattr(activity, "audio_offset", None)) + if kind == "ACTIVITY_START": + turn = self._open_user_turn(now, "server_activity", force_new=True) + turn.va_start = offset + elif kind == "ACTIVITY_END" and self.user_turn is not None: + turn = self.user_turn + if turn.va_start is not None and offset is not None and offset >= turn.va_start: + self.user_speech_seconds += offset - turn.va_start + turn.deferred_end_ns = now + turn.boundary = "activity_end" + + vad = getattr(msg, "voice_activity_detection_signal", None) + if vad is not None: + kind = _enum_name(getattr(vad, "vad_signal_type", None)) + if kind.endswith("_SOS"): + self._open_user_turn(now, "server_vad", force_new=True) + elif kind.endswith("_EOS") and self.user_turn is not None: + self.user_turn.deferred_end_ns = now + self.user_turn.boundary = "activity_end" + + content = getattr(msg, "server_content", None) + if content is not None: + transcription = getattr(content, "input_transcription", None) + text = getattr(transcription, "text", None) if transcription is not None else None + if text: + if self.user_turn is not None: + self._add_user_text(self.user_turn, text) + else: + turn = self._open_user_turn(now, "input_transcription") + self._add_user_text(turn, text) + + model_turn = getattr(content, "model_turn", None) + parts = as_list(getattr(model_turn, "parts", None)) if model_turn is not None else [] + out_tr = getattr(content, "output_transcription", None) + out_text = getattr(out_tr, "text", None) if out_tr is not None else None + if parts or out_text: + turn = self._open_model_turn(now) + for size, mime in inline_audio(parts): + turn.audio_bytes += size + rate = pcm_rate(mime) + if rate: + turn.audio_seconds += size / (rate * 2.0) + elif size: + turn.audio_rate_known = False + thoughts = sum(1 for p in parts if _get(p, "thought")) + if thoughts: + turn.flags["gemini.thought_parts"] = turn.flags.get("gemini.thought_parts", 0) + thoughts + if out_text: + turn.flags["_has_transcription"] = True + turn.text.append(out_text) + elif not turn.flags.get("_has_transcription"): + part_text = text_of_parts(parts) + if part_text: + turn.text.append(part_text) + + if getattr(content, "generation_complete", None) and self.model_turn is not None: + self.model_turn.flags["gemini.generation_complete"] = True + reason = getattr(content, "turn_complete_reason", None) + if reason is not None and self.model_turn is not None: + self.model_turn.flags["gemini.turn_complete_reason"] = _enum_name(reason) + if getattr(content, "interrupted", None): + if self.model_turn is not None: + self._close_model_turn(now, interrupted=True, reason="interrupted") + + tool_call = getattr(msg, "tool_call", None) + if tool_call is not None: + self._finish_deferred_user_turn() + calls = as_list(getattr(tool_call, "function_calls", None)) + if calls: + turn = self._open_model_turn(now, opened_by="tool_call") + for call in calls: + self._open_tool(turn, call, now) + + cancellation = getattr(msg, "tool_call_cancellation", None) + if cancellation is not None: + for call_id in as_list(getattr(cancellation, "ids", None)): + call_id = str(call_id) + self.cancelled_ids.add(call_id) + span = self.tools.pop(call_id, None) + if span is not None: + span.set_attribute("gemini.cancelled", True) + span.set_status(Status(StatusCode.OK)) + span.end(end_time=now) + self.closed_tool_ids.add(call_id) + + usage = getattr(msg, "usage_metadata", None) + if usage is not None: + self._record_usage(usage) + + if content is not None and getattr(content, "turn_complete", None): + if self.user_turn is not None: + self._close_user_turn(now, boundary=self.user_turn.boundary or "turn_complete") + if self.model_turn is not None: + self.model_turn.flags["gemini.turn_complete"] = True + self._close_model_turn(now) + + def _record_usage(self, usage: Any) -> None: + raw = {} + for field in _USAGE_FIELDS: + value = getattr(usage, field, None) + if isinstance(value, int): + raw["gemini.usage." + field] = value + for detail_field in ("prompt_tokens_details", "response_tokens_details"): + for detail in as_list(getattr(usage, detail_field, None)): + modality = _enum_name(getattr(detail, "modality", None)).lower() + count = getattr(detail, "token_count", None) + if isinstance(count, int): + raw[f"gemini.usage.{detail_field}.{modality}"] = count + if not raw: + return + target = self.model_turn + if target is None: + # No open model turn: keep the server's numbers on the root as an event, so + # two messages stay two values and nothing is summed. + self._root_event("gemini.usage_metadata", raw) + return + target.span.add_event("gemini.usage_metadata", raw) + # The latest message wins on the turn. Copied, never added. + target.span.set_attributes(raw) + mapping = { + "gemini.usage.prompt_token_count": "llm.token_count.prompt", + "gemini.usage.response_token_count": "llm.token_count.completion", + "gemini.usage.total_token_count": "llm.token_count.total", + } + for src, dst in mapping.items(): + if src in raw: + target.span.set_attribute(dst, raw[src]) + + # ------------------------------------------------------------------ turns + def _open_user_turn(self, start_ns: int, how: str, force_new: bool = False) -> _Turn: + if self.user_turn is not None: + if not force_new and self.user_turn.deferred_end_ns is None: + return self.user_turn + self._close_user_turn(start_ns, boundary=self.user_turn.boundary or "new_activity") + span = self._child(USER_TURN_NAME, "CHAIN", start_ns, attributes={ + "gemini.input_mode": "realtime_input" if how != "client_content" else how, + "gemini.turn_start": how, + }) + self.user_turn = _Turn(span, start_ns) + return self.user_turn + + def _add_user_text(self, turn: _Turn, text: str) -> None: + turn.text.append(text) + # Transcription arrives in chunks: keep one prompt entry per user turn. + if self.unconsumed_input and self._unconsumed_owner is turn: + self.unconsumed_input[-1] += text + else: + self.unconsumed_input.append(text) + self._unconsumed_owner = turn + + def _finish_deferred_user_turn(self) -> None: + turn = self.user_turn + if turn is not None and turn.deferred_end_ns is not None: + self._close_user_turn(turn.deferred_end_ns, boundary=turn.boundary or "activity_end") + + def _close_user_turn(self, end_ns: int, *, boundary: str, error: Optional[str] = None) -> None: + turn = self.user_turn + if turn is None: + return + self.user_turn = None + span = turn.span + span.set_attribute("gemini.turn_boundary", boundary) + text = "".join(turn.text) + if text and self.policy.capture_inputs: + span.set_attribute("input.value", text) + span.set_attribute("input.mime_type", "text/plain") + if error: + span.set_attribute("gemini.close_reason", error) + span.set_status(Status(StatusCode.ERROR, error)) + else: + span.set_status(Status(StatusCode.OK)) + self.closed_user_turns += 1 + span.end(end_time=max(end_ns, turn.start_ns)) + if text: + row = {"role": "user", "content": text} + self.transcript.append(row) + + def _open_model_turn(self, start_ns: int, opened_by: str = "server_content") -> _Turn: + if self.model_turn is not None: + return self.model_turn + self._finish_deferred_user_turn() + attrs: Dict[str, Any] = { + "gen_ai.system": SYSTEM, + "gemini.turn_start": opened_by, + } + if self.model: + attrs["llm.model_name"] = self.model + attrs["gen_ai.request.model"] = self.model + if self.unconsumed_input and self.policy.capture_inputs: + attrs["input.value"] = "\n".join(self.unconsumed_input) + attrs["input.mime_type"] = "text/plain" + self.unconsumed_input = [] + self._unconsumed_owner = None + self.model_turn = _Turn(self._child(MODEL_TURN_NAME, "LLM", start_ns, attributes=attrs), start_ns) + return self.model_turn + + def _close_model_turn(self, end_ns: int, *, interrupted: bool = False, + reason: Optional[str] = None, error: Optional[str] = None) -> None: + turn = self.model_turn + if turn is None: + return + self.model_turn = None + span = turn.span + for key, value in turn.flags.items(): + if not key.startswith("_"): + span.set_attribute(key, value) + if turn.audio_bytes: + span.set_attribute("gemini.output_audio_bytes", turn.audio_bytes) + if turn.audio_rate_known: + span.set_attribute("gemini.output_audio_seconds", round(turn.audio_seconds, 3)) + self.model_audio_seconds += turn.audio_seconds + else: + self.model_audio_unmeasured = True + text = "".join(turn.text) + if self.policy.capture_outputs: + if text: + span.set_attribute("output.value", text) + span.set_attribute("output.mime_type", "text/plain") + elif turn.calls: + span.set_attribute("output.value", json.dumps(turn.calls, ensure_ascii=False, default=str)) + span.set_attribute("output.mime_type", "application/json") + if interrupted: + span.set_attribute("gemini.interrupted", True) + if reason: + span.set_attribute("gemini.close_reason", reason) + if error: + span.set_status(Status(StatusCode.ERROR, error)) + else: + span.set_status(Status(StatusCode.OK)) + self.closed_model_turns += 1 + # Tool spans opened by this turn keep running: the turn is the model's request, + # the tool runs on the customer's side and closes on its own toolResponse. + span.end(end_time=max(end_ns, turn.start_ns)) + if text: + self.transcript.append({"role": "assistant", "content": text}) + + def _open_tool(self, turn: _Turn, call: Any, now: int) -> None: + call_id = _get(call, "id") + call_id = str(call_id) if call_id is not None else None + name = _get(call, "name") + if call_id is not None and (call_id in self.tools or call_id in self.closed_tool_ids): + return # server-supplied id seen before: no second span + args = _get(call, "args") + attrs: Dict[str, Any] = { + "tool.name": name, + "gen_ai.tool.name": name, + "gen_ai.tool.call.id": call_id, + } + if self.policy.capture_inputs and args is not None: + attrs["input.value"] = to_json(args) + attrs["input.mime_type"] = "application/json" + span = self._child(TOOL_NAME, "TOOL", now, parent=turn.span, attributes=attrs) + key = call_id if call_id is not None else f"_noid_{len(self.tools)}" + self.tools[key] = span + entry: Dict[str, Any] = {"name": name} + if args is not None: + entry["args"] = json.loads(to_json(args)) + turn.calls.append(entry) + + # ------------------------------------------------------------------ errors and exit + def on_receive_error(self, exc: BaseException) -> None: + try: + reason = "socket_closed" if _is_socket_close(exc) else "receive_error" + self._root_event("gemini.receive_error", self._error_attrs(exc)) + self._close_open(_now(), reason) + self._last_receive_error = reason + except Exception: + logger.debug("gemini live: receive error not recorded", exc_info=False) + + def _close_open(self, now: int, reason: str) -> None: + if self.user_turn is not None: + if self.user_turn.deferred_end_ns is not None: + self._finish_deferred_user_turn() + else: + self._close_user_turn(now, boundary="connection_end", error=reason) + if self.model_turn is not None: + self._close_model_turn(now, error=reason, reason=reason) + for call_id, span in list(self.tools.items()): + span.set_attribute("gemini.close_reason", reason) + span.set_status(Status(StatusCode.ERROR, reason)) + span.end(end_time=now) + self.closed_tool_ids.add(call_id) + self.tools.clear() + + def _end_open_clean(self, now: int) -> None: + """Clean exit: whatever is still open simply ends here, without an error status.""" + if self.user_turn is not None: + self._finish_deferred_user_turn() + if self.user_turn is not None: + self._close_user_turn(now, boundary="connection_end") + if self.model_turn is not None: + self._close_model_turn(now, reason="connection_end") + for call_id, span in list(self.tools.items()): + span.set_attribute("gemini.unanswered", True) + span.end(end_time=now) + self.closed_tool_ids.add(call_id) + self.tools.clear() + + def on_connection_exit(self, exc: Optional[BaseException]) -> None: + """The customer's ``async with connect(...)`` block ended.""" + try: + now = _now() + if exc is None: + reason = getattr(self, "_last_receive_error", None) + elif isinstance(exc, asyncio.CancelledError): + reason = "task_cancelled" + elif _is_socket_close(exc): + reason = "socket_closed" + else: + reason = getattr(self, "_last_receive_error", None) or "exception" + self._last_receive_error = None + if exc is not None and reason != "task_cancelled": + self._root_event("gemini.connection_error", self._error_attrs(exc)) + if reason: + self._close_open(now, reason) + else: + self._end_open_clean(now) + resumable = self.handle is not None and self.handle_resumable + if reason != "task_cancelled" and resumable and (self.go_away or reason): + # The customer may reconnect with the stored handle; keep the root open. + self.detach(now, reason) + return + self.close(error=reason, end_ns=now) + except Exception: + logger.debug("gemini live: connection exit not recorded", exc_info=False) + + def detach(self, now: int, reason: Optional[str]) -> None: + self.detached_ns = now + self._pending_status = reason + try: + loop = asyncio.get_running_loop() + self._expiry = loop.call_later(self.inst.resume_window_seconds, self.expire) + except RuntimeError: + self._expiry = None + + def expire(self) -> None: + if self.closed or self.detached_ns is None: + return + self.close(error=self._pending_status, end_ns=self.detached_ns) + + def start_reconnect(self) -> Any: + if self._expiry is not None: + self._expiry.cancel() + self._expiry = None + gap_start = self.detached_ns or _now() + span = self._child(RECONNECT_NAME, "CHAIN", gap_start, attributes={ + "gemini.reconnect.after_go_away": self.go_away or None, + "gemini.reconnect.previous_close_reason": self._pending_status, + }) + self.detached_ns = None + self._pending_status = None + self.go_away = False + self.handle_resumable = False + return span + + def end_reconnect(self, span: Any, ok: bool, exc: Optional[BaseException] = None) -> None: + now = _now() + start = getattr(span, "start_time", None) + if isinstance(start, int): + span.set_attribute("gemini.reconnect.gap_ms", round((now - start) / 1e6, 3)) + if ok: + span.set_status(Status(StatusCode.OK)) + self.root.set_attribute("gemini.reconnect_count", max(self.connections - 1, 1)) + else: + if exc is not None: + span.set_attributes(self._error_attrs(exc)) + span.set_status(Status(StatusCode.ERROR, "resume_failed")) + span.end(end_time=now) + + def close(self, *, error: Optional[str] = None, end_ns: Optional[int] = None) -> None: + if self.closed: + return + self.closed = True + if self._expiry is not None: + self._expiry.cancel() + self._expiry = None + end_ns = end_ns or _now() + try: + if error: + self._close_open(end_ns, error) + else: + self._end_open_clean(end_ns) + except Exception: + pass + root = self.root + try: + root.set_attribute("gemini.realtime_input_count", self.realtime_count) + if self.realtime_audio_bytes: + root.set_attribute("gemini.realtime_audio_bytes", self.realtime_audio_bytes) + if self.audio: + root.set_attribute("call.duration", round((end_ns - self.start_ns) / 1e9, 3)) + turns = self.closed_user_turns + self.closed_model_turns + if turns: + root.set_attribute("call.total_turns", turns) + if (self.user_speech_seconds > 0 and self.model_audio_seconds > 0 + and not self.model_audio_unmeasured): + # Agent talk time over user talk time, as the platform's Retell + # normaliser defines it. Omitted unless both sides were measured. + root.set_attribute("call.talk_ratio", + round(self.model_audio_seconds / self.user_speech_seconds, 4)) + if self.policy.full: + rows = [r for r in self.transcript if r.get("role") and r.get("content")] + if rows: + rendered = json.dumps(rows, ensure_ascii=False) + root.set_attribute("conversation.transcript", rendered) + root.set_attribute("fi.conversation.transcript", rendered) + for i, row in enumerate(rows): + root.set_attribute(f"conversation.transcript.{i}.message.role", row["role"]) + root.set_attribute(f"conversation.transcript.{i}.message.content", row["content"]) + if error: + root.set_attribute("gemini.close_reason", error) + root.set_status(Status(StatusCode.ERROR, error)) + else: + root.set_status(Status(StatusCode.OK)) + finally: + root.end(end_time=max(end_ns, self.start_ns)) + self.inst.forget(self) + self.inst.flush() + + +def _is_socket_close(exc: BaseException) -> bool: + code = getattr(exc, "code", None) + if isinstance(code, int) and 1000 <= code <= 4999: + return True + try: + from websockets.exceptions import ConnectionClosed + except Exception: # pragma: no cover + return False + return isinstance(exc, ConnectionClosed) diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/package.py b/python/frameworks/google-genai-live/traceai_google_genai_live/package.py new file mode 100644 index 000000000..fc610aecd --- /dev/null +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/package.py @@ -0,0 +1,2 @@ +_instruments = ("google-genai >= 2.28, < 3",) +_supports_metrics = False diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/py.typed b/python/frameworks/google-genai-live/traceai_google_genai_live/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/version.py b/python/frameworks/google-genai-live/traceai_google_genai_live/version.py new file mode 100644 index 000000000..3dc1f76bc --- /dev/null +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/version.py @@ -0,0 +1 @@ +__version__ = "0.1.0" From a9f0df8ef2a8fea827c8b11ca46271cc331fa0e5 Mon Sep 17 00:00:00 2001 From: Nikhil Pareek Date: Thu, 15 Oct 2026 21:58:45 -0700 Subject: [PATCH 2/2] fix(google-genai-live): standard FI_HIDE_* switches only, de-flake timing tests Drop the package-specific metadata_only option. Content capture now follows the standard traceAI TraceConfig convention only: text on by default; FI_HIDE_INPUTS / FI_HIDE_OUTPUTS (or TraceConfig(hide_inputs/hide_outputs)) drop a direction, and the *_MESSAGES / *_TEXT variants do the same because all Live content is message text. The example, run_e2e.sh, README and changelog use the env vars. Tests: privacy tests run through the env vars and TraceConfig; new cases for the message/text switches. The non-blocking-reader check allows half the 0.5 s export time instead of 50 ms, and the flush-once check waits for the flush thread to start, so both hold on a loaded machine. Unused imports removed from test_fence. Co-Authored-By: Claude Opus 5.5 --- .../frameworks/google-genai-live/CHANGELOG.md | 5 +- python/frameworks/google-genai-live/README.md | 9 +-- .../google-genai-live/examples/README.md | 2 +- .../google-genai-live/examples/run_e2e.sh | 2 +- .../google-genai-live/examples/voice_tool.py | 8 +-- .../google-genai-live/tests/conftest.py | 3 +- .../google-genai-live/tests/test_fence.py | 6 +- .../google-genai-live/tests/test_privacy.py | 55 +++++++++++++++++-- .../google-genai-live/tests/test_session.py | 7 ++- .../traceai_google_genai_live/__init__.py | 16 +++--- .../traceai_google_genai_live/_redact.py | 15 +++-- 11 files changed, 95 insertions(+), 33 deletions(-) diff --git a/python/frameworks/google-genai-live/CHANGELOG.md b/python/frameworks/google-genai-live/CHANGELOG.md index 8ed19aa35..9fb239b35 100644 --- a/python/frameworks/google-genai-live/CHANGELOG.md +++ b/python/frameworks/google-genai-live/CHANGELOG.md @@ -13,7 +13,8 @@ - Audio sessions carry `fi.span.kind=CONVERSATION` on a parentless root plus `call.*` and `conversation.transcript*`, so Future AGI lists them as calls. Text-only sessions are `CHAIN`. - Privacy: audio bytes, API keys and resumption handles never land on a span. Text content is - on by default; `metadata_only=True` (or `TraceConfig` / `FI_HIDE_INPUTS` / `FI_HIDE_OUTPUTS`) - drops it. Raw error text is content; stack traces are never exported. + on by default, as in every traceAI instrumentor; the standard `FI_HIDE_INPUTS` / + `FI_HIDE_OUTPUTS` (and `*_MESSAGES` / `*_TEXT`) variables or `TraceConfig(...)` drop it. + No package-specific privacy switch. Raw error text is content; stack traces are never exported. - Not supported: Vertex AI Live, ephemeral tokens, the deprecated `send` / `start_stream`, LiveKit / Pipecat sessions, Node. diff --git a/python/frameworks/google-genai-live/README.md b/python/frameworks/google-genai-live/README.md index 37d7c99f5..92a8b21f3 100644 --- a/python/frameworks/google-genai-live/README.md +++ b/python/frameworks/google-genai-live/README.md @@ -50,8 +50,7 @@ Options: | Option | Default | Effect | |---|---|---| -| `metadata_only` | `False` | Drop all content: system instruction, user text, transcriptions, tool arguments and results, model text, raw error text. Model, voice name, timings, tool names, flags, close codes and token counts stay. | -| `config=TraceConfig(hide_inputs=..., hide_outputs=...)` or `FI_HIDE_INPUTS` / `FI_HIDE_OUTPUTS` | off | Drop one direction. The conversation transcript needs both. | +| `config=TraceConfig(...)` or the standard `FI_HIDE_*` environment variables | content on | `hide_inputs` / `FI_HIDE_INPUTS` drops the input side (system instruction, user text and transcription, tool arguments); `hide_outputs` / `FI_HIDE_OUTPUTS` drops the output side (model text and transcription, tool results). `*_input_messages` / `*_input_text` (and the output pair) do the same, because all Live content is message text. Both sides off leaves metadata only: model, voice name, timings, tool names, flags, close codes and token counts. The conversation transcript and raw error text need both sides on. | | `session_id`, `user_id` | none | Defaults for every session. `fi_instrumentation.using_session(...)` / `using_user(...)` around `connect` override them per call. | | `resume_window_seconds` | `30` | How long a session whose connection ended after `goAway`, or died, waits for a reconnect with its stored handle before the root span closes. | @@ -113,8 +112,10 @@ How server events map: ## Privacy -- Default: text on, audio off. Audio bytes (`realtimeInput` and `modelTurn` inline data) never - land on a span. +- Default: text on, audio off, as in every traceAI instrumentor. Audio bytes (`realtimeInput` + and `modelTurn` inline data) never land on a span. +- Metadata only: `FI_HIDE_INPUTS=true FI_HIDE_OUTPUTS=true` (or + `TraceConfig(hide_inputs=True, hide_outputs=True)`). There is no package-specific switch. - The API key is never copied. The connection URL is recorded as its path only. - The session resumption handle stays in memory and is never a span attribute or a log line. - Raw error text is content: it is recorded only when both content directions are on. Stack diff --git a/python/frameworks/google-genai-live/examples/README.md b/python/frameworks/google-genai-live/examples/README.md index 086309196..ea8876756 100644 --- a/python/frameworks/google-genai-live/examples/README.md +++ b/python/frameworks/google-genai-live/examples/README.md @@ -10,7 +10,7 @@ export GEMINI_API_KEY=... FI_API_KEY=... FI_SECRET_KEY=... # export FI_BASE_URL=... # only for a self-hosted Future AGI collector python voice_tool.py --project gemini-live-demo python voice_tool.py --pcm question.pcm # spoken question -python voice_tool.py --metadata-only --marker X # no content leaves the process +FI_HIDE_INPUTS=true FI_HIDE_OUTPUTS=true python voice_tool.py --marker X # no content leaves the process python voice_tool.py --resume # drop the socket, resume with the handle: still one trace python voice_tool.py --model not-a-model # setup rejected: one ERROR root ``` diff --git a/python/frameworks/google-genai-live/examples/run_e2e.sh b/python/frameworks/google-genai-live/examples/run_e2e.sh index 4e61097d0..9d384ea0d 100755 --- a/python/frameworks/google-genai-live/examples/run_e2e.sh +++ b/python/frameworks/google-genai-live/examples/run_e2e.sh @@ -19,6 +19,6 @@ EX="$HERE/voice_tool.py" MARK="FI-MARKER-$RANDOM$RANDOM" echo "1. typed question + tool"; "$PY" "$EX" echo "2. drop + resume (one trace)"; "$PY" "$EX" --resume -echo "3. metadata only ($MARK)"; "$PY" "$EX" --metadata-only --marker "$MARK" +echo "3. content hidden ($MARK)"; FI_HIDE_INPUTS=true FI_HIDE_OUTPUTS=true "$PY" "$EX" --marker "$MARK" echo "4. setup rejected (expect error)"; "$PY" "$EX" --model gemini-not-a-model-live || true echo "Done. In Future AGI open project $PROJECT: 4 traces; search for $MARK must return nothing." diff --git a/python/frameworks/google-genai-live/examples/voice_tool.py b/python/frameworks/google-genai-live/examples/voice_tool.py index 75a6286a2..ba509edcd 100644 --- a/python/frameworks/google-genai-live/examples/voice_tool.py +++ b/python/frameworks/google-genai-live/examples/voice_tool.py @@ -4,9 +4,9 @@ export GEMINI_API_KEY=... FI_API_KEY=... FI_SECRET_KEY=... python voice_tool.py # typed question, spoken answer python voice_tool.py --pcm question.pcm # spoken question (16 kHz, 16-bit mono PCM) + FI_HIDE_INPUTS=true FI_HIDE_OUTPUTS=true python voice_tool.py # metadata only, no content Options used by the package's end-to-end run: - --metadata-only keep model, timings, tool names and token counts; drop all content --marker TEXT put TEXT in the system instruction, the question and the tool result --resume drop the socket after the first answer and resume with the stored handle --model NAME Live model (default gemini-3.8-live; the list is preview, re-read it) @@ -29,16 +29,16 @@ parser.add_argument("--project", default=os.getenv("FI_PROJECT_NAME", "gemini-live-demo")) parser.add_argument("--model", default="gemini-3.8-live") parser.add_argument("--pcm") -parser.add_argument("--metadata-only", action="store_true") parser.add_argument("--marker", default="") parser.add_argument("--resume", action="store_true") parser.add_argument("--modality", default="AUDIO") parser.add_argument("--user-id") args = parser.parse_args() -# 1. Future AGI first, then the instrumentor. +# 1. Future AGI first, then the instrumentor. Content capture follows the standard +# FI_HIDE_INPUTS / FI_HIDE_OUTPUTS environment variables (or pass config=TraceConfig(...)). trace_provider = register(project_type=ProjectType.OBSERVE, project_name=args.project) -instrument(tracer_provider=trace_provider, metadata_only=args.metadata_only) +instrument(tracer_provider=trace_provider) WEATHER = { "name": "get_weather", diff --git a/python/frameworks/google-genai-live/tests/conftest.py b/python/frameworks/google-genai-live/tests/conftest.py index 68936a7d8..16908f8b6 100644 --- a/python/frameworks/google-genai-live/tests/conftest.py +++ b/python/frameworks/google-genai-live/tests/conftest.py @@ -105,7 +105,8 @@ def provider(exporter: InMemorySpanExporter) -> TracerProvider: @pytest.fixture def instrument(provider: TracerProvider, monkeypatch: pytest.MonkeyPatch): - for name in ("FI_HIDE_INPUTS", "FI_HIDE_OUTPUTS"): + for name in ("FI_HIDE_INPUTS", "FI_HIDE_OUTPUTS", "FI_HIDE_INPUT_MESSAGES", + "FI_HIDE_OUTPUT_MESSAGES", "FI_HIDE_INPUT_TEXT", "FI_HIDE_OUTPUT_TEXT"): monkeypatch.delenv(name, raising=False) created = [] diff --git a/python/frameworks/google-genai-live/tests/test_fence.py b/python/frameworks/google-genai-live/tests/test_fence.py index 160948ed3..e6a44d1ba 100644 --- a/python/frameworks/google-genai-live/tests/test_fence.py +++ b/python/frameworks/google-genai-live/tests/test_fence.py @@ -17,7 +17,7 @@ from traceai_google_genai_live import PATCHED, GoogleGenAILiveInstrumentor -from .conftest import AUDIO_CONFIG, load, run_turn +from .conftest import AUDIO_CONFIG MODEL = "gemini-3.8-live" @@ -140,7 +140,9 @@ async def test_ac17_full_queue_drops_and_never_blocks_the_reader(server, client) await session.send_tool_response(function_responses=[ types.FunctionResponse(id=fc.id, name=fc.name, response={}) for fc in msg.tool_call.function_calls]) - assert worst < 0.05, f"receive blocked for {worst:.3f}s" + # One export takes 0.5 s, so a reader that waited on the exporter would stall that long. + # Half of it leaves room for a loaded CI machine without hiding a real block. + assert worst < 0.25, f"receive blocked for {worst:.3f}s" finally: inst.uninstrument() tp.shutdown() diff --git a/python/frameworks/google-genai-live/tests/test_privacy.py b/python/frameworks/google-genai-live/tests/test_privacy.py index 7bb4de385..ee0d5469c 100644 --- a/python/frameworks/google-genai-live/tests/test_privacy.py +++ b/python/frameworks/google-genai-live/tests/test_privacy.py @@ -44,8 +44,17 @@ async def test_default_captures_text_but_never_audio_key_or_handle(server, instr assert_voice_tab(exporter.get_finished_spans(), forbidden_substrings=(API_KEY, HANDLE_1, AUDIO_MARKER)) -async def test_ac18_metadata_only_omits_all_content(server, instrument, client, exporter): - instrument(metadata_only=True) +HIDE_BOTH = {"FI_HIDE_INPUTS": "true", "FI_HIDE_OUTPUTS": "true"} + + +def _hide(monkeypatch, env=HIDE_BOTH): + for name, value in env.items(): + monkeypatch.setenv(name, value) + + +async def test_ac18_hide_flags_omit_all_content(server, instrument, client, exporter, monkeypatch): + _hide(monkeypatch) + instrument() await _run(client, server) text = all_text(exporter) assert MARK not in text @@ -102,8 +111,46 @@ async def test_env_hide_inputs_is_honoured(server, instrument, client, exporter, assert "input.value" not in spans_by_name(exporter)["gemini.live.setup"][0].attributes -async def test_error_text_is_content(server, instrument, client, exporter): - instrument(metadata_only=True) +async def test_trace_config_both_sides_equals_env(server, instrument, client, exporter): + instrument(config=TraceConfig(hide_inputs=True, hide_outputs=True)) + await _run(client, server) + assert MARK not in all_text(exporter) + + +@pytest.mark.parametrize("env", [ + {"FI_HIDE_INPUT_MESSAGES": "true", "FI_HIDE_OUTPUT_MESSAGES": "true"}, + {"FI_HIDE_INPUT_TEXT": "true", "FI_HIDE_OUTPUT_TEXT": "true"}, +]) +async def test_message_and_text_switches_drop_live_content(server, instrument, client, exporter, + monkeypatch, env): + # Live content is all message text, so the narrower standard switches drop it too. + _hide(monkeypatch, env) + instrument() + await _run(client, server) + assert MARK not in all_text(exporter) + assert spans_by_name(exporter)["gemini.live.tool"][0].attributes["tool.name"] == "get_weather" + + +async def test_one_narrow_switch_drops_only_its_side(server, instrument, client, exporter, monkeypatch): + _hide(monkeypatch, {"FI_HIDE_OUTPUT_TEXT": "true"}) + instrument() + await _run(client, server) + tool = spans_by_name(exporter)["gemini.live.tool"][0] + assert "output.value" not in tool.attributes and "input.value" in tool.attributes + assert MARK in spans_by_name(exporter)["gemini.live.setup"][0].attributes["input.value"] + + +def test_no_package_specific_privacy_switch(): + import inspect + + from traceai_google_genai_live import instrument as shorthand + + assert "metadata_only" not in inspect.signature(shorthand).parameters + + +async def test_error_text_is_content(server, instrument, client, exporter, monkeypatch): + _hide(monkeypatch) + instrument() server.scripts.append([["close", 1008, "models/" + MARK + " is not found"]]) with pytest.raises(errors.APIError): async with client.aio.live.connect(model=MARK, config=AUDIO_CONFIG): diff --git a/python/frameworks/google-genai-live/tests/test_session.py b/python/frameworks/google-genai-live/tests/test_session.py index e5409ef54..15de09e2f 100644 --- a/python/frameworks/google-genai-live/tests/test_session.py +++ b/python/frameworks/google-genai-live/tests/test_session.py @@ -259,7 +259,12 @@ async def test_ac15_ac16_socket_death_reraises_and_flushes_once(server, instrume events = [e.name for e in root.events] assert "gemini.receive_error" in events assert server.connects == 1 # no reconnect from the instrumentor - await asyncio.sleep(0.05) + # The flush runs on its own thread; give a loaded machine time to start it. + for _ in range(200): + if flushes: + break + await asyncio.sleep(0.01) + await asyncio.sleep(0.05) # and room for a second flush to show up if there were one assert inst._inst.flush_count == 1 assert len(flushes) == 1 diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py b/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py index 3084eb9c5..1204d4a7e 100644 --- a/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/__init__.py @@ -51,11 +51,13 @@ class GoogleGenAILiveInstrumentor(BaseInstrumentor): # type: ignore[misc] Keyword arguments to ``instrument()``: - ``tracer_provider``: the provider from ``fi_instrumentation.register()``. - - ``config``: a ``TraceConfig``. ``hide_inputs`` / ``FI_HIDE_INPUTS`` and - ``hide_outputs`` / ``FI_HIDE_OUTPUTS`` drop the matching content. - - ``metadata_only``: drop all content (system instruction, transcriptions, user text, - tool arguments and results, model text, error text). Model, voice name, timings, - tool names, flags and token counts stay. + - ``config``: a ``TraceConfig``; without one the standard ``FI_HIDE_*`` environment + variables apply. ``hide_inputs`` / ``hide_input_messages`` / ``hide_input_text`` + drop the input side (system instruction, user text and transcription, tool + arguments); ``hide_outputs`` / ``hide_output_messages`` / ``hide_output_text`` drop + the output side (model text and transcription, tool results). Both sides off leaves + metadata only: model, voice name, timings, tool names, flags and token counts. Raw + error text and the conversation transcript need both sides on. - ``session_id`` / ``user_id``: defaults for every session. ``fi_instrumentation``'s ``using_session`` / ``using_user`` context managers override them per connect. - ``resume_window_seconds``: how long a session whose connection ended after @@ -76,7 +78,7 @@ def _instrument(self, **kwargs: Any) -> None: self._inst = _Instrumentation( tracer=tracer, tracer_provider=tracer_provider, - policy=policy_from(config, bool(kwargs.get("metadata_only", False))), + policy=policy_from(config), session_id=kwargs.get("session_id"), user_id=kwargs.get("user_id"), resume_window_seconds=float(kwargs.get("resume_window_seconds", 30.0)), @@ -120,7 +122,6 @@ def instrument( tracer_provider: Any = None, *, config: Optional[TraceConfig] = None, - metadata_only: bool = False, session_id: Optional[str] = None, user_id: Optional[str] = None, resume_window_seconds: float = 30.0, @@ -130,7 +131,6 @@ def instrument( instrumentor.instrument( tracer_provider=tracer_provider, config=config, - metadata_only=metadata_only, session_id=session_id, user_id=user_id, resume_window_seconds=resume_window_seconds, diff --git a/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py b/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py index e4817895c..60fd94c91 100644 --- a/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py +++ b/python/frameworks/google-genai-live/traceai_google_genai_live/_redact.py @@ -29,13 +29,18 @@ def full(self) -> bool: return self.capture_inputs and self.capture_outputs -def policy_from(config: Optional[TraceConfig], metadata_only: bool) -> ContentPolicy: - if metadata_only: - return ContentPolicy(False, False) +# The standard traceAI switches (TraceConfig fields, or the FI_HIDE_* environment variables). +# Everything a Live session carries in either direction is message text, so any one of them +# drops that whole direction here. +_INPUT_SWITCHES = ("hide_inputs", "hide_input_messages", "hide_input_text") +_OUTPUT_SWITCHES = ("hide_outputs", "hide_output_messages", "hide_output_text") + + +def policy_from(config: Optional[TraceConfig]) -> ContentPolicy: config = config or TraceConfig() return ContentPolicy( - capture_inputs=not bool(getattr(config, "hide_inputs", False)), - capture_outputs=not bool(getattr(config, "hide_outputs", False)), + capture_inputs=not any(bool(getattr(config, n, False)) for n in _INPUT_SWITCHES), + capture_outputs=not any(bool(getattr(config, n, False)) for n in _OUTPUT_SWITCHES), )