:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(replay-vision): route gemini via the gateway on its own project (#107183)
Brandonb5dad83c3693
diff --git a/posthog/llm/gateway_client.py b/posthog/llm/gateway_client.pyindex 0c7efb2f8f6f..fa2c4ac023b9 100644--- a/posthog/llm/gateway_client.py+++ b/posthog/llm/gateway_client.py@@ -12,6 +12,8 @@ from django.conf import settingsimport httpximport structlogfrom anthropic import Anthropic, AsyncAnthropic+from google import genai+from google.genai import types as genai_typesfrom openai import AsyncOpenAI, OpenAIfrom posthog.clickhouse.query_tagging import private_capture_context@@ -396,7 +398,8 @@ def trace_id_from_seed(seed: str) -> str:def anthropic_gateway_base_url(openai_base_url: str) -> str:"""Drop the OpenAI ``/v1`` suffix so the Anthropic SDK, which appends ``/v1/messages``itself, hits the same gateway root the OpenAI route uses. ``resolve_ai_gateway_config``- guarantees the ``/v1`` suffix, so this is the inverse of that validation.+ guarantees the ``/v1`` suffix, so this is the inverse of that validation. The Gemini SDK+ appends ``/v1beta/models/...`` and needs the same root."""trimmed = openai_base_url.rstrip("/")if trimmed.endswith("/v1"):@@ -593,3 +596,76 @@ def build_anthropic_client(use_bedrock_fallback=use_bedrock_fallback,default_headers=fallback_headers,)+++class AIGatewayHostError(RuntimeError):+ """A gateway client was about to send its key to a host other than the gateway."""+++def _origin(url: httpx.URL) -> str:+ # httpx lowercases the host and drops a scheme's default port, so equal origins compare equal.+ return f"{url.scheme}://{url.netloc.decode('ascii')}"+++def build_gemini_client(+ ai_product: str | None = None,+ trace_id: str | None = None,+ session_id: str | None = None,+ properties: Mapping[str, str] | None = None,+ distinct_id: str | None = None,+ privacy_mode: bool = False,+ timeout_ms: int | None = None,+) -> genai.Client | None:+ """Return a plain ``google.genai.Client`` routed through the Go ai-gateway, or None when the gateway is unset.++ It skips the posthoganalytics wrapper because the gateway captures the generation.+ """+ gateway = resolve_ai_gateway_config()+ if gateway is None:+ return None+ headers = (+ ai_gateway_headers(+ ai_product=ai_product,+ trace_id=trace_id,+ session_id=session_id,+ properties=properties,+ distinct_id=distinct_id,+ )+ or {}+ )+ if privacy_mode:+ headers["X-PostHog-Privacy-Mode"] = "true"+ base_url = anthropic_gateway_base_url(gateway.url)+ # `files.upload` sends its chunks with none of the client's headers, so the transports carry them and the key.+ transport_headers = {**headers, "x-goog-api-key": gateway.api_key}+ # Chunks go to whatever URL the upload start returned, so pin every request to the gateway host.+ expected_origin = _origin(httpx.URL(base_url))++ def pin_to_gateway(request: httpx.Request) -> None:+ if (origin := _origin(request.url)) != expected_origin:+ raise AIGatewayHostError(f"request origin must be the AI gateway {expected_origin}, got {origin}")++ async def apin_to_gateway(request: httpx.Request) -> None:+ pin_to_gateway(request)++ return genai.Client(+ api_key=gateway.api_key,+ http_options=genai_types.HttpOptions(+ base_url=base_url,+ headers=headers,+ timeout=timeout_ms,+ client_args={+ "trust_env": False,+ "headers": transport_headers,+ "event_hooks": {"request": [pin_to_gateway]},+ },+ # The SDK uses aiohttp for async whenever it is installed, and that session always trusts+ # proxy env vars. An explicit httpx transport switches async to httpx, which honours trust_env.+ async_client_args={+ "trust_env": False,+ "transport": httpx.AsyncHTTPTransport(),+ "headers": transport_headers,+ "event_hooks": {"request": [apin_to_gateway]},+ },+ ),+ )diff --git a/posthog/llm/test_gateway_client.py b/posthog/llm/test_gateway_client.pyindex 1b0c2ee24843..b04f80e669e0 100644--- a/posthog/llm/test_gateway_client.py+++ b/posthog/llm/test_gateway_client.py@@ -8,18 +8,21 @@ from unittest.mock import patchfrom django.test import override_settings+import httpxfrom asgiref.sync import async_to_sync, sync_to_asyncfrom posthog.clickhouse.query_tagging import get_query_tagsfrom posthog.llm import gateway_clientfrom posthog.llm.gateway_client import (AIGatewayConfig,+ AIGatewayHostError,GatewayNotConfiguredError,Product,build_ai_gateway_anthropic_client,build_anthropic_client,build_async_anthropic_client,build_async_openai_client,+ build_gemini_client,build_openai_client,get_anthropic_gateway_client,get_async_anthropic_gateway_client,@@ -564,6 +567,81 @@ class TestBuildAIGatewayAnthropicClient:mock_get_anthropic.assert_not_called()+class TestBuildGeminiClient:+ @override_settings(AI_GATEWAY_URL=AI_GATEWAY_URL, AI_GATEWAY_API_KEY=AI_GATEWAY_KEY)+ @patch("posthog.llm.gateway_client.genai.Client")+ def test_gateway_mode_omits_privacy_header_unless_asked(self, mock_genai):+ build_gemini_client(ai_product="replay_vision")++ assert "X-PostHog-Privacy-Mode" not in mock_genai.call_args.kwargs["http_options"].headers++ @override_settings(AI_GATEWAY_URL=AI_GATEWAY_URL, AI_GATEWAY_API_KEY=AI_GATEWAY_KEY)+ def test_real_client_sends_the_key_and_headers_to_the_native_route(self):+ seen: list[httpx.Request] = []++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request)+ return httpx.Response(200, json={"candidates": [{"content": {"role": "model", "parts": [{"text": "ok"}]}}]})++ client = build_gemini_client(ai_product="replay_vision", privacy_mode=True)+ assert client is not None+ sync_client, async_client = client._api_client._httpx_client, client._api_client._async_httpx_client+ assert sync_client is not None and sync_client.trust_env is False+ assert async_client is not None and async_client.trust_env is False+ client._api_client._httpx_client = httpx.Client(transport=httpx.MockTransport(handler))++ client.models.generate_content(model="models/gemini-test", contents="hi")++ assert str(seen[0].url) == "https://ai-gateway.example/v1beta/models/gemini-test:generateContent"+ assert seen[0].headers["x-goog-api-key"] == AI_GATEWAY_KEY+ assert seen[0].headers["x-posthog-product"] == "replay_vision"+ assert seen[0].headers["x-posthog-privacy-mode"] == "true"++ @override_settings(AI_GATEWAY_URL=AI_GATEWAY_URL, AI_GATEWAY_API_KEY=AI_GATEWAY_KEY)+ @pytest.mark.parametrize(+ "url",+ [+ "https://generativelanguage.googleapis.com/upload/v1beta/files",+ "http://ai-gateway.example/v1beta/files",+ "http://ai-gateway.example:443/v1beta/files",+ "https://ai-gateway.example:8443/v1beta/files",+ ],+ )+ def test_both_transports_refuse_any_origin_but_the_gateway(self, url):+ seen: list[httpx.Request] = []+ client = build_gemini_client(ai_product="replay_vision")+ assert client is not None+ sync_client = client._api_client._httpx_client+ async_client = client._api_client._async_httpx_client+ assert sync_client is not None and async_client is not None++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request)+ return httpx.Response(200)++ sync_client._transport = httpx.MockTransport(handler)+ async_client._transport = httpx.MockTransport(handler)++ with pytest.raises(AIGatewayHostError):+ sync_client.post(url)+ with pytest.raises(AIGatewayHostError):+ asyncio.run(async_client.post(url))+ sync_client.get("https://ai-gateway.example/v1beta/files/abc")+ asyncio.run(async_client.get("https://ai-gateway.example/v1beta/files/abc"))++ assert [str(r.url) for r in seen] == ["https://ai-gateway.example/v1beta/files/abc"] * 2++ @pytest.mark.parametrize(+ ("url", "api_key"),+ [("", ""), (AI_GATEWAY_URL, ""), ("", AI_GATEWAY_KEY), ("https://ai-gateway.example", AI_GATEWAY_KEY)],+ )+ @patch("posthog.llm.gateway_client.genai.Client")+ def test_unset_or_misconfigured_returns_none_so_the_caller_stays_direct(self, mock_genai, url, api_key):+ with override_settings(AI_GATEWAY_URL=url, AI_GATEWAY_API_KEY=api_key):+ assert build_gemini_client(ai_product="replay_vision") is None+ mock_genai.assert_not_called()++class TestPrivateScoutGateway:@pytest.mark.parametrize(("builder", "sdk", "suffix"),diff --git a/products/replay_vision/backend/gemini_client.py b/products/replay_vision/backend/gemini_client.pynew file mode 100644index 000000000000..5be9ad4eb3bd--- /dev/null+++ b/products/replay_vision/backend/gemini_client.py@@ -0,0 +1,171 @@+"""Replay Vision's Gemini client: the AI gateway when enabled for the team, else Google directly with our key."""++from collections.abc import Mapping+from typing import Any++from django.conf import settings++from google.genai import (+ Client as RawGenAIClient,+ errors,+ types,+)+from posthoganalytics.ai.gemini import genai++from posthog.llm.gateway_client import (+ ai_gateway_headers,+ build_gemini_client,+ resolve_ai_gateway_config,+ team_distinct_id,+)+from posthog.models import Team+from posthog.ph_client import feature_enabled_or_false++from products.replay_vision.backend.temporal.gemini import gemini_api_key++AI_PRODUCT = "replay_vision"+GATEWAY_FLAG = "replay-vision-ai-gateway"+++async def replay_gemini_client(team_id: int, *, properties: Mapping[str, Any] | None = None) -> "ReplayGemini":+ base_properties = dict(properties or {})+ if await _gateway_enabled(team_id) and (+ client := build_gemini_client(+ ai_product=AI_PRODUCT, properties=_gateway_labels(base_properties), privacy_mode=True+ )+ ):+ return ReplayGemini(client, _AsyncGatewayModels(client.aio.models, base_properties))+ # Privacy mode keeps recording content out of the internal project, where it can't be deleted with the recording.+ direct = genai.AsyncClient(api_key=gemini_api_key(), posthog_privacy_mode=True, posthog_properties=base_properties)+ return ReplayGemini(RawGenAIClient(api_key=gemini_api_key()), direct.models)+++async def _gateway_enabled(team_id: int) -> bool:+ # Cleanup and the sweep delete files with the direct key, so gateway mode also needs Replay Vision's own key.+ if not settings.REPLAY_VISION_GEMINI_API_KEY or resolve_ai_gateway_config() is None:+ return False+ organization_id = str(await Team.objects.values_list("organization_id", flat=True).aget(id=team_id))+ # Local evaluation only, so a flag outage leaves a scan's many calls direct and fast; conditions must use the+ # org or project id.+ return feature_enabled_or_false(+ GATEWAY_FLAG,+ team_distinct_id(team_id),+ groups={"organization": organization_id, "project": str(team_id)},+ group_properties={"organization": {"id": organization_id}, "project": {"id": str(team_id)}},+ only_evaluate_locally=True,+ send_feature_flag_events=False,+ )+++class ReplayGemini:+ """One client for both routes: `models` takes the analytics wrapper's `posthog_*` kwargs; `raw` serves files and+ caches."""++ def __init__(self, raw: RawGenAIClient, models: Any) -> None:+ self.raw = raw+ self.models = models++ async def aclose(self) -> None:+ """Close both SDK transports; the sync one carries `files`, the async one `aio`."""+ self.raw.close()+ await self.raw.aio.aclose()+++class _AsyncGatewayModels:+ """Takes the analytics wrapper's `posthog_*` kwargs as gateway headers; groups have none, so they drop. Streams+ every call: the gateway cuts a buffered call at 290s and long scans run past that."""++ def __init__(self, models: Any, base_properties: Mapping[str, Any]) -> None:+ self._models = models+ self._base_properties = base_properties++ def _config(+ self,+ config: types.GenerateContentConfig | None,+ posthog_distinct_id: str | None,+ posthog_trace_id: str | None,+ posthog_properties: Mapping[str, Any] | None,+ ) -> types.GenerateContentConfig | None:+ # The client's default headers carry the product, privacy mode and base labels; a call adds its own.+ labels = (+ _gateway_labels({**self._base_properties, **posthog_properties, "ai_product": AI_PRODUCT})+ if posthog_properties+ else None+ )+ headers = (+ ai_gateway_headers(trace_id=posthog_trace_id, properties=labels, distinct_id=posthog_distinct_id) or {}+ )+ # The gateway sets `$ai_span_name` only from this header; it strips `$` keys from the properties.+ if posthog_properties and (span_name := posthog_properties.get("$ai_span_name")):+ headers["X-PostHog-Span-Name"] = str(span_name)+ return _with_headers(config, headers)++ async def generate_content(+ self,+ *,+ model: str,+ contents: Any,+ config: types.GenerateContentConfig | None = None,+ posthog_distinct_id: str | None = None,+ posthog_trace_id: str | None = None,+ posthog_properties: Mapping[str, Any] | None = None,+ posthog_privacy_mode: bool | None = None,+ posthog_groups: Mapping[str, Any] | None = None,+ ) -> types.GenerateContentResponse:+ stream = await self._models.generate_content_stream(+ model=model,+ contents=contents,+ config=self._config(config, posthog_distinct_id, posthog_trace_id, posthog_properties),+ )+ chunks = [chunk async for chunk in stream]+ return assemble_stream(chunks)+++def assemble_stream(chunks: list[types.GenerateContentResponse]) -> types.GenerateContentResponse:+ """Fold streamed chunks into the response a buffered call returns, parts in arrival order."""+ parts: list[types.Part] = []+ last_candidate: types.Candidate | None = None+ finish_reason: types.FinishReason | None = None+ for chunk in chunks:+ for candidate in chunk.candidates or []:+ if candidate.index not in (None, 0):+ continue+ last_candidate = candidate+ if candidate.content and candidate.content.parts:+ parts.extend(candidate.content.parts)+ if candidate.finish_reason is not None:+ finish_reason = candidate.finish_reason+ usage = next((chunk.usage_metadata for chunk in reversed(chunks) if chunk.usage_metadata), None)+ prompt_feedback = next((chunk.prompt_feedback for chunk in chunks if chunk.prompt_feedback), None)+ if not chunks:+ raise errors.ServerError(503, {"error": {"code": 503, "message": "Gemini stream ended with no chunks"}})+ final = chunks[-1]+ if last_candidate is None:+ # A blocked prompt streams no candidate; the feedback says why.+ return final.model_copy(update={"usage_metadata": usage, "prompt_feedback": prompt_feedback})+ role = last_candidate.content.role if last_candidate.content and last_candidate.content.role else "model"+ candidate = last_candidate.model_copy(+ update={+ "content": types.Content(role=role, parts=parts),+ "finish_reason": finish_reason,+ }+ )+ return final.model_copy(+ update={"candidates": [candidate], "usage_metadata": usage, "prompt_feedback": prompt_feedback}+ )+++def _gateway_labels(properties: Mapping[str, Any]) -> dict[str, str]:+ # The gateway strips `$`-prefixed keys as reserved.+ return {key: str(value) for key, value in properties.items() if not key.startswith("$") and value is not None}+++def _with_headers(+ config: types.GenerateContentConfig | None, headers: dict[str, str] | None+) -> types.GenerateContentConfig | None:+ if not headers:+ return config+ config = config or types.GenerateContentConfig()+ options = config.http_options or types.HttpOptions()+ merged = options.model_copy(update={"headers": {**(options.headers or {}), **headers}})+ return config.model_copy(update={"http_options": merged})diff --git a/products/replay_vision/backend/temporal/activities/call_scanner_provider.py b/products/replay_vision/backend/temporal/activities/call_scanner_provider.pyindex 2b970f91c5a6..fd9e694e524b 100644--- a/products/replay_vision/backend/temporal/activities/call_scanner_provider.py+++ b/products/replay_vision/backend/temporal/activities/call_scanner_provider.py@@ -29,7 +29,6 @@ from google.genai import (Client as GoogleGenAIClient,types,)-from posthoganalytics.ai.gemini import genaifrom pydantic import BaseModel, ValidationErrorfrom temporalio import activity@@ -41,13 +40,18 @@ from posthog.temporal.session_replay.rasterize_recording.types import Navigationfrom products.exports.backend.models.exported_asset import ExportedAssetfrom products.replay_vision.backend.consent import is_ai_data_processing_approvedfrom products.replay_vision.backend.distinct_ids import replay_vision_distinct_id+from products.replay_vision.backend.gemini_client import AI_PRODUCT, replay_gemini_clientfrom products.replay_vision.backend.learned_rules import ScanRules, load_scan_rulesfrom products.replay_vision.backend.models.replay_observation import ObservationStatus, ReplayObservationfrom products.replay_vision.backend.tags import slugify_tagfrom products.replay_vision.backend.temporal.decorators import track_activityfrom products.replay_vision.backend.temporal.errors import ConsentWithdrawnError, FailureKind, ScannerFailureErrorfrom products.replay_vision.backend.temporal.events_tool import build_events_index-from products.replay_vision.backend.temporal.gemini import classify_gemini_error, describe_gemini_error, gemini_api_key+from products.replay_vision.backend.temporal.gemini import (+ classify_gemini_error,+ describe_gemini_error,+ is_gateway_refusal,+)from products.replay_vision.backend.temporal.lookups import (LookupPlan,render_lookup_results,@@ -589,20 +593,14 @@ async def _run_mission(Caching is best-effort: a video too short to cache (or any cache hiccup) falls back to sending it inline, and acached run that fails for a non-validation reason is retried inline once before giving up."""- api_key = gemini_api_key()# Attribute every scanner generation to Replay Vision in LLM analytics so costs and traces roll up to the product.- client = genai.AsyncClient(- api_key=api_key,- # Privacy mode keeps the recording's content out of the internal project, where it could not be deleted with the recording.- posthog_privacy_mode=True,- posthog_properties={- "ai_product": "replay_vision",- "feature": "scanner",- "scanner_type": snapshot.scanner_type.value,- "team_id": team_id,- },- )- cache_client = GoogleGenAIClient(api_key=api_key)+ properties = {+ "ai_product": AI_PRODUCT,+ "feature": "scanner",+ "scanner_type": snapshot.scanner_type.value,+ "team_id": team_id,+ }+model = f"models/{snapshot.model}"metric_labels = {"provider": snapshot.provider,@@ -627,7 +625,6 @@ async def _run_mission(record(scanner_type, snapshot.model)return run_lookups(plan, events_index=events_index, network_index=network_index)- cache = await _maybe_create_video_cache(cache_client, model, video_part, preamble_text)signal_duration_s = video_clock.citable_duration_s(llm_inputs.metadata.duration_seconds)network_available = network_index.has_requests()steps: list[MissionStep] = []@@ -647,9 +644,10 @@ async def _run_mission()steps.append(step)+ gemini = await replay_gemini_client(team_id, properties=properties)run = functools.partial(_run_steps,- client=client,+ client=gemini,model=model,steps=steps,video_part=video_part,@@ -659,11 +657,14 @@ async def _run_mission(trace_id=trace_id,look_up=look_up,)+ cache = Nonetry:+ cache = await _maybe_create_video_cache(gemini.raw, model, video_part, preamble_text)step_outputs = await _run_mission_attempts(run=run, cache=cache, model=snapshot.model)finally:if cache is not None:- await _delete_video_cache(cache_client, cache.name)+ await _delete_video_cache(gemini.raw, cache.name)+ await gemini.aclose()finalized, signals = scanner.assemble(step_outputs)return _MissionOutcome(@@ -724,10 +725,10 @@ async def _run_pass(*, run: Any, cache: Any | None, model: str) -> dict[str, Basexcept ScannerFailureError:raise # a required step genuinely couldn't be satisfied, so re-running won't help.except Exception as exc:- # Transient provider errors belong to the Temporal activity retry, which backs off across the quota- # window; an immediate inline re-run would just re-spend the video tokens against a provider that is- # still rate-limiting or down, and would multiply with the other retry layers.- if classify_gemini_error(exc) is FailureKind.PROVIDER_TRANSIENT:+ # Retryable errors go to the Temporal retry, which backs off; an inline re-run would re-spend the video+ # tokens. A gateway refusal would refuse the inline run too.+ kind = classify_gemini_error(exc)+ if (kind is not None and kind.is_retryable) or is_gateway_refusal(exc):raise# The cached request failed some other way (a stale cache reference, an unrecognized error), so retry# inline once. Capture the cause: this fallback fires often enough that we need to know whether it's@@ -780,7 +781,11 @@ async def _run_steps(convo.append(types.Part(text=instruction))result = await run_step(step=step, convo=convo)except Exception as exc:- if step.required:+ # A gateway refusal that repeats on every attempt (e.g. a 413) fails the pass even on an optional step.+ # One that can clear (rate limit, budget cap) skips the step instead: a Temporal retry would re-spend+ # the core step's video tokens, and a capped budget would refuse the retry too.+ kind = classify_gemini_error(exc)+ if step.required or (is_gateway_refusal(exc) and not (kind and kind.is_retryable)):raise# A provider error arrives here, not as an empty output, and must not sink a paid-for scan.logger.warning("replay_vision.call_scanner_provider.optional_step_failed", step=step.name, error=str(exc))diff --git a/products/replay_vision/backend/temporal/activities/upload_video_to_gemini.py b/products/replay_vision/backend/temporal/activities/upload_video_to_gemini.pyindex 5ac20332ddbc..9e4506b2ae66 100644--- a/products/replay_vision/backend/temporal/activities/upload_video_to_gemini.py+++ b/products/replay_vision/backend/temporal/activities/upload_video_to_gemini.py@@ -18,6 +18,7 @@ from posthog.temporal.common.heartbeat import Heartbeaterfrom products.exports.backend.models.exported_asset import ExportedAssetfrom products.replay_vision.backend.consent import is_ai_data_processing_approved+from products.replay_vision.backend.gemini_client import replay_gemini_clientfrom products.replay_vision.backend.temporal.decorators import track_activityfrom products.replay_vision.backend.temporal.errors import ConsentWithdrawnError, FailureKind, ScannerFailureErrorfrom products.replay_vision.backend.temporal.gemini import (@@ -25,7 +26,6 @@ from products.replay_vision.backend.temporal.gemini import (classify_gemini_file_error,describe_gemini_error,describe_gemini_file_error,- gemini_api_key,)from products.replay_vision.backend.temporal.gemini_cleanup_sweep.tracking import track_uploaded_filefrom products.replay_vision.backend.temporal.types import UploadedVideo, UploadVideoToGeminiInputs@@ -87,11 +87,18 @@ async def _upload_video(inputs: UploadVideoToGeminiInputs) -> UploadedVideo:f"ExportedAsset {inputs.asset_id} produced empty video bytes", kind=FailureKind.INTERNAL_ERROR)- raw_client = RawGenAIClient(api_key=gemini_api_key())+ gemini = await replay_gemini_client(asset.team_id)+ try:+ return await _store_video(gemini.raw, video_bytes, asset.export_format, workflow_id)+ finally:+ await gemini.aclose()+++async def _store_video(+ raw_client: RawGenAIClient, video_bytes: bytes, mime_type: str, workflow_id: str+) -> UploadedVideo:# `tmp_file.write` / `flush` are blocking disk I/O; offload the whole tempfile+upload block off the event loop.- uploaded_file = await asyncio.to_thread(- _write_and_upload, raw_client, video_bytes, asset.export_format, workflow_id- )+ uploaded_file = await asyncio.to_thread(_write_and_upload, raw_client, video_bytes, mime_type, workflow_id)if uploaded_file.name is None:# Non-retryable: a retry would re-upload before the cleanup sweep can reap the unnamed file Gemini may have created.@@ -144,7 +151,7 @@ async def _upload_video(inputs: UploadVideoToGeminiInputs) -> UploadedVideo:return UploadedVideo(file_uri=uploaded_file.uri,- mime_type=uploaded_file.mime_type or asset.export_format,+ mime_type=uploaded_file.mime_type or mime_type,gemini_file_name=gemini_file_name,)diff --git a/products/replay_vision/backend/temporal/gemini.py b/products/replay_vision/backend/temporal/gemini.pyindex 7580b3e16ecf..55728778003f 100644--- a/products/replay_vision/backend/temporal/gemini.py+++ b/products/replay_vision/backend/temporal/gemini.py@@ -28,6 +28,19 @@ _PROVIDER_TRANSPORT_ERRORS: tuple[type[BaseException], ...] = ()+# Gateway refusals a retry can clear: key, binding, budget and rate limits. Others repeat on every attempt.+_GATEWAY_CLEARING_CODES = frozenset({401, 402, 403, 408, 409, 429})+++def is_gateway_refusal(error: BaseException) -> bool:+ """A 4xx the AI gateway wrote itself, marked with a `google.rpc.ErrorInfo` in the posthog.com domain."""+ if not isinstance(error, APIError) or not isinstance(error.code, int) or not 400 <= error.code < 500:+ return False+ body = error.details.get("error") if isinstance(error.details, dict) else None+ details = body.get("details") if isinstance(body, dict) else None+ return isinstance(details, list) and any(isinstance(d, dict) and d.get("domain") == "posthog.com" for d in details)++def describe_gemini_error(error: BaseException) -> str:"""Fixed-shape summary for user-visible error reasons.@@ -52,6 +65,9 @@ def classify_gemini_error(error: BaseException) -> FailureKind | None:return FailureKind.PROVIDER_TRANSIENTif not isinstance(error, APIError):return None+ # A gateway refusal is a PostHog-side fault, never the input's.+ if is_gateway_refusal(error):+ return FailureKind.INFRA_TRANSIENT if error.code in _GATEWAY_CLEARING_CODES else FailureKind.INTERNAL_ERRORif error.code in _TRANSIENT_STATUS_CODES:return FailureKind.PROVIDER_TRANSIENT# A 4xx we didn't list is the provider refusing this input (unsupported video, too large, safety block).diff --git a/products/replay_vision/backend/temporal/pii_check.py b/products/replay_vision/backend/temporal/pii_check.pyindex 989f67799127..55cd5b3ff106 100644--- a/products/replay_vision/backend/temporal/pii_check.py+++ b/products/replay_vision/backend/temporal/pii_check.py@@ -14,7 +14,6 @@ from typing import Any, TypeVarimport structlogfrom google.genai import types-from posthoganalytics.ai.gemini import genaifrom prometheus_client import Counterfrom pydantic import BaseModel@@ -23,8 +22,9 @@ from products.ml_inference.backend.facade.contracts import DecisionQuestion, Decfrom products.ml_inference.backend.facade.enums import DecisionQuestionTypefrom products.replay_vision.backend.distinct_ids import replay_vision_distinct_idfrom products.replay_vision.backend.error_kinds import FailureKind+from products.replay_vision.backend.gemini_client import AI_PRODUCT, replay_gemini_clientfrom products.replay_vision.backend.temporal.errors import ScannerFailureError-from products.replay_vision.backend.temporal.gemini import gemini_api_key+from products.replay_vision.backend.temporal.gemini import is_gateway_refusallogger = structlog.get_logger(__name__)@@ -133,20 +133,22 @@ def contains_pii(*, team_id: int, text: dict[str, str], trace_id: str) -> bool |async def rewrite_without_pii(*, team_id: int, question: str, text: dict[str, str], trace_id: str) -> dict[str, str]:"""Rewrite the answer's text fields without the personal data; fields the rewrite drops keep their old text."""- client = genai.AsyncClient(- api_key=gemini_api_key(),- posthog_privacy_mode=True,- posthog_properties={"ai_product": "replay_vision", "feature": "pii_rewrite", "team_id": team_id},- )- response = await client.models.generate_content(- model=_REWRITE_MODEL,- contents=_REWRITE_INSTRUCTIONS.format(question=question, answer=json.dumps(text, ensure_ascii=False, indent=2)),- config=types.GenerateContentConfig(response_mime_type="application/json", response_schema=_Rewrite),- posthog_distinct_id=replay_vision_distinct_id(team_id),- posthog_trace_id=trace_id,- posthog_properties={"$ai_span_name": "pii_rewrite"},- posthog_groups={"project": str(team_id)},- )+ properties = {"ai_product": AI_PRODUCT, "feature": "pii_rewrite", "team_id": team_id}+ client = await replay_gemini_client(team_id, properties=properties)+ try:+ response = await client.models.generate_content(+ model=_REWRITE_MODEL,+ contents=_REWRITE_INSTRUCTIONS.format(+ question=question, answer=json.dumps(text, ensure_ascii=False, indent=2)+ ),+ config=types.GenerateContentConfig(response_mime_type="application/json", response_schema=_Rewrite),+ posthog_distinct_id=replay_vision_distinct_id(team_id),+ posthog_trace_id=trace_id,+ posthog_properties={"$ai_span_name": "pii_rewrite"},+ posthog_groups={"project": str(team_id)},+ )+ finally:+ await client.aclose()rewritten = _Rewrite.model_validate_json(response.text or "{}")return {field: new for field in text if isinstance(new := getattr(rewritten, field), str) and new.strip()}@@ -177,7 +179,7 @@ async def keep_unrequested_pii_out(When the question asks who the session belongs to, the session's own identity values are allowed and only therest of the answer is judged. Raises `ScannerFailureError(PII_DETECTED)` when the rewritten answer is still- flagged, or the rewrite fails.+ flagged, or the rewrite fails other than by a gateway refusal."""text = answer_text(output)if not text:@@ -197,6 +199,9 @@ async def keep_unrequested_pii_out()except Exception as e:record_pii_check("failed", scanner_type)+ # A gateway refusal is our fault; the activity boundary decides whether it retries.+ if is_gateway_refusal(e):+ raiseraise ScannerFailureError("The answer included personal data the scanner didn't ask for, and it could not be removed.",kind=FailureKind.PII_DETECTED,diff --git a/products/replay_vision/backend/tests/test_call_scanner_provider.py b/products/replay_vision/backend/tests/test_call_scanner_provider.pyindex 90204a6803e0..af32c8378208 100644--- a/products/replay_vision/backend/tests/test_call_scanner_provider.py+++ b/products/replay_vision/backend/tests/test_call_scanner_provider.py@@ -1,3 +1,4 @@+import contextlibfrom collections.abc import Callable, Sequencefrom typing import Any, cast@@ -6,9 +7,10 @@ from unittest.mock import AsyncMock, MagicMock, patchimport httpxfrom google.genai import types-from google.genai.errors import APIError+from google.genai.errors import APIError, ClientErrorfrom pydantic import BaseModel+from products.replay_vision.backend.gemini_client import ReplayGeminifrom products.replay_vision.backend.models.replay_scanner import ScannerTypefrom products.replay_vision.backend.temporal.activities.call_scanner_provider import (_key_moment_session_ms,@@ -99,6 +101,86 @@ async def _run()+def _gateway_scan_snapshot() -> MagicMock:+ snapshot = MagicMock()+ snapshot.scanner_type.value = "monitor"+ snapshot.model = "gemini-3-flash-preview"+ snapshot.provider = "gemini"+ return snapshot+++def _gemini(models: Any = None) -> MagicMock:+ gemini = MagicMock(spec=ReplayGemini)+ gemini.raw = MagicMock()+ gemini.models = models if models is not None else MagicMock()+ gemini.aclose = AsyncMock()+ return gemini++[email protected]("fails", [None, "mission", "cache"])+async def test_the_mission_runs_generate_and_caches_on_one_client_and_closes_it(fails: str | None) -> None:+ scanner = MagicMock()+ scanner.mission_steps.return_value = []+ scanner.assemble.return_value = (MagicMock(), [])+ gemini = _gemini()+ create_cache = AsyncMock(side_effect=RuntimeError("cache failed") if fails == "cache" else None, return_value=None)+ attempts = AsyncMock(side_effect=RuntimeError("mission failed") if fails == "mission" else None, return_value={})+ module = "products.replay_vision.backend.temporal.activities.call_scanner_provider"+ with (+ patch(f"{module}.replay_gemini_client", new=AsyncMock(return_value=gemini)),+ patch(f"{module}._maybe_create_video_cache", new=create_cache),+ patch(f"{module}._run_mission_attempts", new=attempts),+ patch(f"{module}.build_events_index", return_value={}),+ contextlib.suppress(RuntimeError),+ ):+ await _run_mission(+ scanner=scanner,+ snapshot=_gateway_scan_snapshot(),+ video_part=_VIDEO,+ video_clock=_IDENTITY_CLOCK,+ preamble_text="PRE",+ team_id=42,+ llm_inputs=MagicMock(),+ trace_id="trace-1",+ )++ assert create_cache.call_args.args[0] is gemini.raw+ if fails != "cache":+ assert attempts.call_args.kwargs["run"].keywords["client"] is gemini+ gemini.aclose.assert_awaited_once()+++async def test_the_mission_deletes_its_cache_before_closing_the_client() -> None:+ scanner = MagicMock()+ scanner.mission_steps.return_value = []+ scanner.assemble.return_value = (MagicMock(), [])+ order: list[str] = []+ gemini = _gemini()+ gemini.raw.aio.caches.create = AsyncMock(return_value=MagicMock(name="cache"))+ gemini.raw.aio.caches.delete = AsyncMock(side_effect=lambda **_: order.append("delete"))+ gemini.aclose = AsyncMock(side_effect=lambda: order.append("aclose"))+ module = "products.replay_vision.backend.temporal.activities.call_scanner_provider"+ with (+ patch(f"{module}.replay_gemini_client", new=AsyncMock(return_value=gemini)),+ patch(f"{module}._run_mission_attempts", new=AsyncMock(return_value={})),+ patch(f"{module}.build_events_index", return_value={}),+ ):+ await _run_mission(+ scanner=scanner,+ snapshot=_gateway_scan_snapshot(),+ video_part=types.Part(text="video"),+ video_clock=_IDENTITY_CLOCK,+ preamble_text="PRE",+ team_id=42,+ llm_inputs=MagicMock(),+ trace_id="trace-1",+ )++ assert order == ["delete", "aclose"]++@pytest.mark.asyncioasync def test_scanner_generations_include_team_attribution() -> None:scanner = MagicMock()@@ -111,9 +193,9 @@ async def test_scanner_generations_include_team_attribution() -> None:with (patch(- "products.replay_vision.backend.temporal.activities.call_scanner_provider.genai.AsyncClient"- ) as client_cls,- patch("products.replay_vision.backend.temporal.activities.call_scanner_provider.GoogleGenAIClient"),+ "products.replay_vision.backend.temporal.activities.call_scanner_provider.replay_gemini_client",+ new=AsyncMock(return_value=_gemini()),+ ) as client_factory,patch("products.replay_vision.backend.temporal.activities.call_scanner_provider._maybe_create_video_cache",new=AsyncMock(return_value=None),@@ -138,7 +220,7 @@ async def test_scanner_generations_include_team_attribution() -> None:trace_id="trace-1",)- assert client_cls.call_args.kwargs["posthog_properties"] == {+ assert client_factory.call_args.kwargs["properties"] == {"ai_product": "replay_vision","feature": "scanner","scanner_type": "monitor",@@ -372,8 +454,7 @@ async def test_signal_timestamps_use_recording_duration()module = "products.replay_vision.backend.temporal.activities.call_scanner_provider"with (- patch(f"{module}.genai.AsyncClient", return_value=client),- patch(f"{module}.GoogleGenAIClient"),+ patch(f"{module}.replay_gemini_client", new=AsyncMock(return_value=_gemini(client.models))),patch(f"{module}.build_events_index", return_value={}),patch(f"{module}._maybe_create_video_cache", new=AsyncMock(return_value=None)),):@@ -437,6 +518,39 @@ async def test_a_provider_error_on_a_non_required_step_leaves_the_scan_standing(assert "media" not in out+ "code,fails_the_pass",+ [(404, True), (413, True), (401, False), (402, False), (429, False)],+)+async def test_only_a_gateway_refusal_that_repeats_fails_the_pass_on_a_non_required_step(+ code: int, fails_the_pass: bool+) -> None:+ steps = [+ MissionStep(name="summary", instruction="sum", response_model=_Core),+ MissionStep(name="media", instruction="pick", response_model=_Side, required=False),+ ]+ refusal = ClientError(code, {"error": {"code": code, "details": [{"domain": "posthog.com"}]}})++ class _RefusingModels(_FakeModels):+ async def generate_content(self, **kwargs: Any) -> _Resp:+ if len(self.calls) >= 1:+ raise refusal+ return await super().generate_content(**kwargs)++ client = _FakeClient([])+ client.models = _RefusingModels([_Resp(text='{"verdict":"yes"}')])++ if fails_the_pass:+ with pytest.raises(ClientError) as raised:+ await _run(client, steps)+ assert raised.value is refusal+ else:+ out = await _run(client, steps)+ assert "summary" in out+ assert "media" not in out++@pytest.mark.asyncioasync def test_a_provider_error_on_a_required_step_still_fails_the_scan() -> None:steps = [MissionStep(name="summary", instruction="sum", response_model=_Core)]@@ -579,9 +693,11 @@ class TestRunPass:[APIError(429, {"error": {"message": "quota", "status": "RESOURCE_EXHAUSTED"}}),httpx.ConnectError("connection reset by peer"),+ APIError(402, {"error": {"code": 402, "details": [{"domain": "posthog.com", "reason": "cap_exceeded"}]}}),+ APIError(413, {"error": {"code": 413, "details": [{"domain": "posthog.com", "reason": "too_large"}]}}),],)- async def test_transient_provider_error_is_not_retried_inline(self, error: Exception) -> None:+ async def test_transient_or_gateway_error_is_not_retried_inline(self, error: Exception) -> None:run, calls = self._cached_run_failing_with(error)with pytest.raises(type(error)):await _run_pass(run=run, cache=self._Cache(), model="m")diff --git a/products/replay_vision/backend/tests/test_gemini_client.py b/products/replay_vision/backend/tests/test_gemini_client.pynew file mode 100644index 000000000000..2f57957bb88d--- /dev/null+++ b/products/replay_vision/backend/tests/test_gemini_client.py@@ -0,0 +1,406 @@+import json+import asyncio+from collections.abc import Callable+from typing import Any++import pytest+from unittest.mock import AsyncMock, MagicMock, patch++from django.test import override_settings++import httpx+from google.genai import (+ errors as genai_errors,+ types,+)++from posthog.llm.gateway_client import AIGatewayHostError++from products.replay_vision.backend.gemini_client import (+ GATEWAY_FLAG,+ ReplayGemini,+ _AsyncGatewayModels,+ _with_headers,+ assemble_stream,+ replay_gemini_client,+)+from products.replay_vision.backend.temporal import pii_check+from products.replay_vision.backend.temporal.activities.upload_video_to_gemini import _write_and_upload+from products.replay_vision.backend.temporal.errors import FailureKind+from products.replay_vision.backend.temporal.gemini import classify_gemini_error++_GATEWAY = {+ "AI_GATEWAY_URL": "https://ai-gateway.example/v1",+ "AI_GATEWAY_API_KEY": "phs_gateway",+ "REPLAY_VISION_GEMINI_API_KEY": "replay-vision-google-key",+}+_OK = {"candidates": [{"content": {"role": "model", "parts": [{"text": "ok"}]}, "finishReason": "STOP"}]}+_FLAG = "products.replay_vision.backend.gemini_client.feature_enabled_or_false"+++_ORG = "0190e1a2-0000-7000-8000-000000000042"++[email protected](autouse=True)+def flag_on() -> Any:+ with patch(_FLAG, return_value=True) as flag:+ yield flag++[email protected](autouse=True)+def team_org() -> Any:+ with patch("products.replay_vision.backend.gemini_client.Team") as team:+ team.objects.values_list.return_value.aget = AsyncMock(return_value=_ORG)+ yield team+++def _sse(*chunks: dict[str, Any]) -> bytes:+ return b"".join(b"data: " + json.dumps(chunk).encode() + b"\r\n\r\n" for chunk in chunks)+++def _ok(request: httpx.Request) -> httpx.Response:+ if request.url.path.endswith(":streamGenerateContent"):+ return httpx.Response(200, content=_sse(_OK), headers={"content-type": "text/event-stream"})+ return httpx.Response(200, json=_OK)+++async def _agateway_client(properties: dict[str, Any] | None = None) -> ReplayGemini:+ with override_settings(**_GATEWAY):+ client = await replay_gemini_client(42, properties=properties)+ assert isinstance(client.models, _AsyncGatewayModels)+ return client+++def _gateway_client(properties: dict[str, Any] | None = None) -> ReplayGemini:+ return asyncio.run(_agateway_client(properties))+++def _wired_gateway_client(+ properties: dict[str, Any], respond: Callable[[httpx.Request], httpx.Response] = _ok+) -> tuple[ReplayGemini, list[httpx.Request]]:+ client = _gateway_client(properties)+ return client, _wire(client, respond)+++def _wire(client: ReplayGemini, respond: Callable[[httpx.Request], httpx.Response] = _ok) -> list[httpx.Request]:+ seen: list[httpx.Request] = []++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request)+ return respond(request)++ # Swaps only the transport, so a client the SDK has closed still fails the call.+ api_client = client.raw._api_client+ assert api_client._httpx_client is not None and api_client._async_httpx_client is not None+ api_client._httpx_client._transport = httpx.MockTransport(handler)+ api_client._async_httpx_client._transport = httpx.MockTransport(handler)+ return seen+++class TestReplayGeminiClient:+ def test_flag_off_stays_direct(self, flag_on: MagicMock) -> None:+ flag_on.return_value = False+ with (+ override_settings(**_GATEWAY),+ patch("products.replay_vision.backend.gemini_client.build_gemini_client") as build,+ ):+ client = asyncio.run(replay_gemini_client(42))+ build.assert_not_called()+ assert client.raw._api_client.api_key == "replay-vision-google-key"+ assert not isinstance(client.models, _AsyncGatewayModels)++ def test_direct_mode_captures_with_privacy_mode_and_the_callers_properties(self, flag_on: MagicMock) -> None:+ flag_on.return_value = False+ with (+ override_settings(**_GATEWAY),+ patch("products.replay_vision.backend.gemini_client.genai.AsyncClient") as direct_client,+ ):+ client = asyncio.run(replay_gemini_client(42, properties={"feature": "scanner"}))+ direct_client.assert_called_once_with(+ api_key="replay-vision-google-key", posthog_privacy_mode=True, posthog_properties={"feature": "scanner"}+ )+ assert client.models is direct_client.return_value.models++ def test_gateway_mode_needs_the_direct_key_cleanup_deletes_with(self) -> None:+ settings = {**_GATEWAY, "REPLAY_VISION_GEMINI_API_KEY": "", "GEMINI_API_KEY": "shared-key"}+ with override_settings(**settings):+ client = asyncio.run(replay_gemini_client(42))+ assert client.raw._api_client.api_key == "shared-key"+ assert not isinstance(client.models, _AsyncGatewayModels)++ def test_flag_is_evaluated_locally_per_org_without_events(self, flag_on: MagicMock, team_org: MagicMock) -> None:+ _gateway_client()+ team_org.objects.values_list.return_value.aget.assert_awaited_once_with(id=42)+ flag_on.assert_called_once_with(+ GATEWAY_FLAG,+ "team-42",+ groups={"organization": _ORG, "project": "42"},+ group_properties={"organization": {"id": _ORG}, "project": {"id": "42"}},+ only_evaluate_locally=True,+ send_feature_flag_events=False,+ )++ def test_per_call_capture_kwargs_become_request_headers(self) -> None:+ client, seen = _wired_gateway_client({"feature": "scanner", "team_id": 42})++ asyncio.run(+ client.models.generate_content(+ model="models/gemini-test",+ contents=[types.Part.from_bytes(data=b"mp4", mime_type="video/mp4"), "hi"],+ config=types.GenerateContentConfig(temperature=0.2),+ posthog_distinct_id="user-1",+ posthog_trace_id="trace-1",+ posthog_properties={"$ai_span_name": "core", "feature": "override"},+ posthog_groups={"project": "42"},+ )+ )++ request = seen[0]+ assert str(request.url).startswith("https://ai-gateway.example/v1beta/models/gemini-test:streamGenerateContent")+ assert request.headers["x-posthog-trace-id"] == "trace-1"+ assert request.headers["x-posthog-distinct-id"] == "user-1"+ assert request.headers["x-posthog-privacy-mode"] == "true"+ assert request.headers["x-posthog-span-name"] == "core"+ assert json.loads(request.headers["x-posthog-properties"]) == {+ "feature": "override",+ "team_id": "42",+ "ai_product": "replay_vision",+ }+ body = json.loads(request.content)+ assert body["contents"][0]["parts"][0]["inlineData"]["mimeType"] == "video/mp4"+ assert "httpOptions" not in body and "http_options" not in body++ @pytest.mark.parametrize(+ "code, kind",+ [+ (401, FailureKind.INFRA_TRANSIENT),+ (402, FailureKind.INFRA_TRANSIENT),+ (403, FailureKind.INFRA_TRANSIENT),+ (408, FailureKind.INFRA_TRANSIENT),+ (409, FailureKind.INFRA_TRANSIENT),+ (429, FailureKind.INFRA_TRANSIENT),+ (400, FailureKind.INTERNAL_ERROR),+ (404, FailureKind.INTERNAL_ERROR),+ (413, FailureKind.INTERNAL_ERROR),+ (502, FailureKind.PROVIDER_TRANSIENT),+ ],+ )+ def test_the_gateways_own_refusal_is_ours_and_retries_only_when_it_can_clear(+ self, code: int, kind: FailureKind+ ) -> None:+ body = {+ "error": {+ "code": code,+ "details": [+ {"@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "x", "domain": "posthog.com"}+ ],+ }+ }+ client, _ = _wired_gateway_client({}, respond=lambda _: httpx.Response(code, json=body))++ with pytest.raises(genai_errors.APIError) as exc_info:+ asyncio.run(client.models.generate_content(model="gemini-test", contents="hi"))++ assert classify_gemini_error(exc_info.value) == kind++ @pytest.mark.parametrize("code", [400, 401, 403, 404])+ def test_a_refusal_google_wrote_stays_a_rejection(self, code: int) -> None:+ body = {+ "error": {+ "code": code,+ "details": [{"@type": "type.googleapis.com/google.rpc.ErrorInfo", "domain": "googleapis.com"}],+ }+ }+ client, _ = _wired_gateway_client({}, respond=lambda _: httpx.Response(code, json=body))++ with pytest.raises(genai_errors.ClientError) as exc_info:+ asyncio.run(client.models.generate_content(model="gemini-test", contents="hi"))++ kind = classify_gemini_error(exc_info.value)+ assert kind == FailureKind.PROVIDER_REJECTED+ assert kind is not None and not kind.is_retryable++ def test_request_headers_keep_the_callers_other_http_options(self) -> None:+ config = types.GenerateContentConfig(http_options=types.HttpOptions(timeout=5000, headers={"a": "1"}))+ merged = _with_headers(config, {"b": "2"})+ assert merged is not None and merged.http_options is not None+ assert merged.http_options.timeout == 5000+ assert merged.http_options.headers == {"a": "1", "b": "2"}+++def test_pii_rewrite_uses_the_gateway_when_configured() -> None:+ with (+ override_settings(**_GATEWAY),+ patch("posthog.llm.gateway_client.genai.Client") as gateway_client,+ patch("products.replay_vision.backend.gemini_client.genai.AsyncClient") as direct_client,+ ):+ sdk = gateway_client.return_value+ sdk.aio.models.generate_content_stream = AsyncMock(side_effect=RuntimeError("stop"))+ sdk.aio.aclose = AsyncMock()+ with pytest.raises(RuntimeError):+ asyncio.run(pii_check.rewrite_without_pii(team_id=1, question="q", text={"a": "b"}, trace_id="t"))++ direct_client.assert_not_called()+ request_headers = sdk.aio.models.generate_content_stream.call_args.kwargs["config"].http_options.headers+ assert json.loads(request_headers["X-PostHog-Properties"])["feature"] == "pii_rewrite"+ assert request_headers["X-PostHog-Span-Name"] == "pii_rewrite"+ sdk.close.assert_called_once()+ sdk.aio.aclose.assert_awaited_once()+++def _chunk(*parts: types.Part, finish: types.FinishReason | None = None) -> types.GenerateContentResponse:+ return types.GenerateContentResponse(+ candidates=[types.Candidate(content=types.Content(role="model", parts=list(parts)), finish_reason=finish)]+ )+++class TestAssembleStream:+ def test_chunks_fold_into_one_response_in_order(self) -> None:+ call = types.Part(function_call=types.FunctionCall(name="get_events", args={"n": 1}), thought_signature=b"sig")+ usage = types.GenerateContentResponseUsageMetadata(prompt_token_count=3, candidates_token_count=2)+ last = _chunk(types.Part(text="lo"), finish=types.FinishReason.STOP).model_copy(+ update={"usage_metadata": usage}+ )++ response = assemble_stream(+ [_chunk(types.Part(text="think", thought=True)), _chunk(call), _chunk(types.Part(text="Hel")), last]+ )++ assert response.candidates is not None and response.candidates[0].content is not None+ parts = response.candidates[0].content.parts+ assert parts is not None+ assert [p.text for p in parts] == ["think", None, "Hel", "lo"]+ assert parts[1].thought_signature == b"sig"+ assert response.text == "Hello"+ assert response.function_calls is not None and response.function_calls[0].name == "get_events"+ assert response.candidates[0].finish_reason == types.FinishReason.STOP+ assert response.usage_metadata is not None and response.usage_metadata.candidates_token_count == 2++ def test_a_blocked_prompt_keeps_its_feedback(self) -> None:+ blocked = types.GenerateContentResponse(+ prompt_feedback=types.GenerateContentResponsePromptFeedback(block_reason=types.BlockedReason.SAFETY)+ )++ response = assemble_stream([blocked])++ assert not response.candidates+ assert response.prompt_feedback is not None+ assert response.prompt_feedback.block_reason == types.BlockedReason.SAFETY++ def test_an_empty_stream_raises_a_transient_error(self) -> None:+ with pytest.raises(genai_errors.ServerError) as exc_info:+ assemble_stream([])++ assert classify_gemini_error(exc_info.value) == FailureKind.PROVIDER_TRANSIENT+++class TestGatewayFilesAndCaches:+ def test_aclose_closes_both_transports(self) -> None:+ client, _ = _wired_gateway_client({})+ api_client = client.raw._api_client+ asyncio.run(client.aclose())+ assert api_client._httpx_client is not None and api_client._httpx_client.is_closed+ assert api_client._async_httpx_client is not None and api_client._async_httpx_client.is_closed++ def test_every_call_is_tagged_replay_vision_with_the_gateway_key(self) -> None:+ def respond(request: httpx.Request) -> httpx.Response:+ path = request.url.path+ if path.endswith(":streamGenerateContent"):+ return _ok(request)+ if path == "/upload/v1beta/files" and "upload_id" not in request.url.params:+ upload_url = "https://ai-gateway.example/upload/v1beta/files?upload_id=u1&upload_protocol=resumable"+ return httpx.Response(200, json={}, headers={"x-goog-upload-url": upload_url})+ if path == "/upload/v1beta/files":+ file = {"name": "files/abc", "state": "PROCESSING"}+ return httpx.Response(200, json={"file": file}, headers={"x-goog-upload-status": "final"})+ if path.startswith("/v1beta/cachedContents"):+ return httpx.Response(200, json={"name": "cachedContents/c1"} if request.method == "POST" else {})+ return httpx.Response(200, json={"name": "files/abc", "state": "ACTIVE"} if request.method == "GET" else {})++ client, seen = _wired_gateway_client({"feature": "scanner"}, respond)++ async def calls() -> None:+ await client.models.generate_content(model="gemini-test", contents="hi")+ await client.raw.aio.caches.create(+ model="gemini-test", config=types.CreateCachedContentConfig(contents=[types.Part(text="video")])+ )+ await client.raw.aio.caches.delete(name="cachedContents/c1")++ asyncio.run(calls())+ uploaded = _write_and_upload(client.raw, b"mp4-bytes", "video/mp4", "wf-1")+ client.raw.files.get(name="files/abc")+ client.raw.files.delete(name="files/abc")++ assert uploaded.name == "files/abc"+ assert [(r.method, r.url.path, r.headers.get("x-goog-upload-command")) for r in seen] == [+ ("POST", "/v1beta/models/gemini-test:streamGenerateContent", None),+ ("POST", "/v1beta/cachedContents", None),+ ("DELETE", "/v1beta/cachedContents/c1", None),+ ("POST", "/upload/v1beta/files", "start"),+ ("POST", "/upload/v1beta/files", "upload, finalize"),+ ("GET", "/v1beta/files/abc", None),+ ("DELETE", "/v1beta/files/abc", None),+ ]+ assert seen[4].read() == b"mp4-bytes"+ for request in seen:+ assert request.headers["x-posthog-product"] == "replay_vision", request.url+ assert request.headers["x-goog-api-key"] == "phs_gateway", request.url++ def test_upload_chunks_never_leave_the_gateway_host(self) -> None:+ def respond(request: httpx.Request) -> httpx.Response:+ upload_url = "https://generativelanguage.googleapis.com/upload/v1beta/files?upload_id=u1"+ return httpx.Response(200, json={}, headers={"x-goog-upload-url": upload_url})++ client, seen = _wired_gateway_client({}, respond)++ with pytest.raises(AIGatewayHostError, match="generativelanguage.googleapis.com"):+ _write_and_upload(client.raw, b"mp4-bytes", "video/mp4", "wf-1")++ assert [request.url.host for request in seen] == ["ai-gateway.example"]++ def test_a_redirect_off_the_gateway_host_is_not_followed(self) -> None:+ def respond(request: httpx.Request) -> httpx.Response:+ return httpx.Response(+ 302, headers={"location": "https://generativelanguage.googleapis.com/v1beta/files/abc"}+ )++ client, seen = _wired_gateway_client({}, respond)++ with pytest.raises(AIGatewayHostError):+ client.raw.files.get(name="files/abc")++ assert [request.url.host for request in seen] == ["ai-gateway.example"]++ def test_a_gateway_refusal_on_an_upload_chunk_is_ours(self) -> None:+ refusal = {+ "error": {+ "code": 403,+ "details": [{"@type": "type.googleapis.com/google.rpc.ErrorInfo", "domain": "posthog.com"}],+ }+ }++ def respond(request: httpx.Request) -> httpx.Response:+ if "upload_id" not in request.url.params:+ upload_url = "https://ai-gateway.example/upload/v1beta/files?upload_id=u1"+ return httpx.Response(200, json={}, headers={"x-goog-upload-url": upload_url})+ return httpx.Response(403, json=refusal)++ client, _ = _wired_gateway_client({}, respond)++ with patch("google.genai._api_client.time.sleep"), pytest.raises(genai_errors.ClientError) as exc_info:+ _write_and_upload(client.raw, b"mp4-bytes", "video/mp4", "wf-1")++ assert classify_gemini_error(exc_info.value) == FailureKind.INFRA_TRANSIENT++ def test_an_in_band_error_frame_raises(self) -> None:+ frame = {"error": {"code": 503, "message": "gateway shutting down", "status": "UNAVAILABLE"}}++ def respond(_: httpx.Request) -> httpx.Response:+ return httpx.Response(200, content=_sse(frame), headers={"content-type": "text/event-stream"})++ client, _ = _wired_gateway_client({}, respond)++ with pytest.raises(genai_errors.ServerError) as exc_info:+ asyncio.run(client.models.generate_content(model="gemini-test", contents="hi"))++ assert classify_gemini_error(exc_info.value) == FailureKind.PROVIDER_TRANSIENTdiff --git a/products/replay_vision/backend/tests/test_pii_check.py b/products/replay_vision/backend/tests/test_pii_check.pyindex 1e49abc5d702..606c6a18eeda 100644--- a/products/replay_vision/backend/tests/test_pii_check.py+++ b/products/replay_vision/backend/tests/test_pii_check.py@@ -1,6 +1,7 @@import pytestfrom unittest.mock import AsyncMock, patch+from google.genai import errors as genai_errorsfrom parameterized import parameterizedfrom products.replay_vision.backend.error_kinds import FailureKind@@ -18,11 +19,14 @@ def _detector(*, team_id: int, text: dict[str, str], trace_id: str) -> bool:return _SUBJECT in joined or _PHONE.lower() in joined-async def _check(reasoning: str, asks: bool | None, rewrite_to: str = _CLEAN) -> MonitorOutput:+async def _check(+ reasoning: str, asks: bool | None, rewrite_to: str = _CLEAN, rewrite_error: Exception | None = None+) -> MonitorOutput:+ rewrite = AsyncMock(return_value={"reasoning": rewrite_to}, side_effect=rewrite_error)with (patch.object(pii_check, "asks_for_identity", return_value=asks),patch.object(pii_check, "contains_pii", side_effect=_detector),- patch.object(pii_check, "rewrite_without_pii", new=AsyncMock(return_value={"reasoning": rewrite_to})),+ patch.object(pii_check, "rewrite_without_pii", new=rewrite),):return await pii_check.keep_unrequested_pii_out(MonitorOutput(verdict="yes", reasoning=reasoning, confidence=0.9),@@ -57,3 +61,23 @@ async def test_answer_still_flagged_after_the_rewrite_fails_the_observation() ->await _check(f"(t 12) {_SUBJECT} paid.", False, rewrite_to=f"(t 12) {_SUBJECT} paid, again.")assert raised.value.kind == FailureKind.PII_DETECTEDassert raised.value.non_retryable+++def _refusal(domain: str, code: int = 402) -> genai_errors.ClientError:+ return genai_errors.ClientError(code, {"error": {"code": code, "details": [{"domain": domain}]}})++[email protected]("code", [402, 404, 429])+async def test_a_gateway_refusal_of_the_rewrite_reaches_the_activity_boundary(code: int) -> None:+ refusal = _refusal("posthog.com", code)+ with pytest.raises(genai_errors.ClientError) as raised:+ await _check(f"(t 12) {_SUBJECT} paid.", False, rewrite_error=refusal)+ assert raised.value is refusal+++async def test_a_provider_refusal_of_the_rewrite_fails_the_observation() -> None:+ with pytest.raises(ScannerFailureError) as raised:+ await _check(f"(t 12) {_SUBJECT} paid.", False, rewrite_error=_refusal("googleapis.com"))+ assert raised.value.kind == FailureKind.PII_DETECTEDdiff --git a/products/replay_vision/backend/tests/test_temporal.py b/products/replay_vision/backend/tests/test_temporal.pyindex 575568eb6ba5..062fa2a62a0b 100644--- a/products/replay_vision/backend/tests/test_temporal.py+++ b/products/replay_vision/backend/tests/test_temporal.py@@ -1033,9 +1033,7 @@ class TestEgressConsentRecheck:team = await sync_to_async(self._team_without_consent)()# The activity derives the team from the asset row (no team_id in the payload), so it must exist.asset = await ExportedAsset.objects.acreate(team=team, export_format="video/mp4")- with patch(- "products.replay_vision.backend.temporal.activities.upload_video_to_gemini.RawGenAIClient"- ) as mock_client:+ with patch("products.replay_vision.backend.gemini_client.RawGenAIClient") as mock_client:with pytest.raises(ConsentWithdrawnError) as exc_info:await ActivityEnvironment().run(upload_video_to_gemini_activity,@@ -3720,10 +3718,12 @@ class TestUploadedFileNotActive:state=types.FileState.FAILED,error=types.FileStatus(code=code, message="the provider quoted 'wf-secret-name' back at us"),)- with patch(- "products.replay_vision.backend.temporal.activities.upload_video_to_gemini.RawGenAIClient"- ) as mock_client:+ with (+ patch("products.replay_vision.backend.gemini_client.RawGenAIClient") as mock_client,+ patch("products.replay_vision.backend.gemini_client.genai.AsyncClient"),+ ):mock_client.return_value.files.upload.return_value = failed_file+ mock_client.return_value.aio.aclose = AsyncMock()with patch("products.replay_vision.backend.temporal.activities.upload_video_to_gemini.track_uploaded_file",AsyncMock(),@@ -3740,6 +3740,72 @@ class TestUploadedFileNotActive:assert exc_info.value.message == f"The AI provider could not process the video (error code {code})"[email protected]_db(transaction=True)+class TestUploadThroughTheGateway:+ @staticmethod+ def _team() -> Team:+ org = Organization.objects.create(name="gateway-upload-org")+ return Team.objects.create(organization=org, name="gateway-upload-team")++ @pytest.mark.asyncio+ async def test_the_upload_runs_on_the_teams_client_and_closes_it(self) -> None:+ team = await sync_to_async(self._team)()+ asset = await ExportedAsset.objects.acreate(team=team, export_format="video/mp4", content=b"mp4-bytes")+ uri = "https://generativelanguage.googleapis.com/v1beta/files/abc"+ gemini = MagicMock()+ gemini.aclose = AsyncMock()+ gemini.raw.files.upload.return_value = types.File(name="files/abc", state=types.FileState.PROCESSING)+ gemini.raw.files.get.return_value = types.File(+ name="files/abc", state=types.FileState.ACTIVE, uri=uri, mime_type="video/mp4"+ )+ module = "products.replay_vision.backend.temporal.activities.upload_video_to_gemini"+ with (+ patch(f"{module}.replay_gemini_client", new=AsyncMock(return_value=gemini)) as factory,+ patch(f"{module}.track_uploaded_file", AsyncMock()),+ patch(f"{module}.asyncio.sleep", AsyncMock()),+ ):+ uploaded = await ActivityEnvironment().run(+ upload_video_to_gemini_activity, UploadVideoToGeminiInputs(asset_id=asset.id)+ )++ factory.assert_awaited_once_with(team.id)+ upload = gemini.raw.files.upload.call_args.kwargs+ assert "http_options" not in upload and upload["config"].http_options is None+ assert upload["config"].mime_type == "video/mp4"+ gemini.raw.files.get.assert_called_once_with(name="files/abc")+ assert (uploaded.file_uri, uploaded.gemini_file_name) == (uri, "files/abc")+ gemini.aclose.assert_awaited_once()++ @pytest.mark.asyncio+ async def test_flag_off_uploads_direct_with_the_gateway_configured(self) -> None:+ team = await sync_to_async(self._team)()+ asset = await ExportedAsset.objects.acreate(team=team, export_format="video/mp4", content=b"mp4-bytes")+ module = "products.replay_vision.backend.temporal.activities.upload_video_to_gemini"+ gateway_settings = {+ "AI_GATEWAY_URL": "https://ai-gateway.example/v1",+ "AI_GATEWAY_API_KEY": "phs_gateway",+ "REPLAY_VISION_GEMINI_API_KEY": "replay-vision-google-key",+ }+ with (+ override_settings(**gateway_settings),+ patch("products.replay_vision.backend.gemini_client.feature_enabled_or_false", return_value=False),+ patch("products.replay_vision.backend.gemini_client.build_gemini_client") as build,+ patch("products.replay_vision.backend.gemini_client.RawGenAIClient") as raw_client,+ patch(f"{module}.track_uploaded_file", AsyncMock()),+ ):+ raw_client.return_value.aio.aclose = AsyncMock()+ raw_client.return_value.files.upload.return_value = types.File(+ name="files/abc", state=types.FileState.ACTIVE, uri="https://g/files/abc"+ )+ uploaded = await ActivityEnvironment().run(+ upload_video_to_gemini_activity, UploadVideoToGeminiInputs(asset_id=asset.id)+ )++ raw_client.assert_called_once_with(api_key="replay-vision-google-key")+ build.assert_not_called()+ assert uploaded.gemini_file_name == "files/abc"++class TestGeminiErrorRedaction:"""`error_reason` is shown to the user verbatim, and a Gemini error body can quote request content(prompt text, file references). The provider-facing activities must surface only the fixed summary."""