Skip to content

: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.

fix(replay-vision): retry a provider-side video processing failure (#98760)

posthog[bot]40eeac77fb87
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.py
index b7d7d276dcea..5ac20332ddbc 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
@@ -20,7 +20,13 @@ from products.exports.backend.models.exported_asset import ExportedAsset
from products.replay_vision.backend.consent import is_ai_data_processing_approved
from products.replay_vision.backend.temporal.decorators import track_activity
from products.replay_vision.backend.temporal.errors import ConsentWithdrawnError, FailureKind, ScannerFailureError
-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,
+ 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_file
from products.replay_vision.backend.temporal.types import UploadedVideo, UploadVideoToGeminiInputs
@@ -119,10 +125,17 @@ async def _upload_video(inputs: UploadVideoToGeminiInputs) -> UploadedVideo:
final_state = uploaded_file.state.name if uploaded_file.state else None
if final_state != "ACTIVE":
- raise ScannerFailureError(
- f"Gemini file {gemini_file_name} reached non-ACTIVE state {final_state!r}",
- kind=FailureKind.PROVIDER_REJECTED,
+ file_error = uploaded_file.error
+ kind = classify_gemini_file_error(file_error)
+ # The provider's own message can quote request content, so only its code reaches the log and the user.
+ logger.warning(
+ "replay_vision.upload_video_to_gemini.file_not_active",
+ state=final_state,
+ kind=kind.value,
+ error_code=file_error.code if file_error else None,
+ gemini_file_name=gemini_file_name,
)
+ raise ScannerFailureError(describe_gemini_file_error(file_error), kind=kind)
if not uploaded_file.uri:
raise ScannerFailureError(
f"Gemini file {gemini_file_name} reached ACTIVE but has no URI",
diff --git a/products/replay_vision/backend/temporal/gemini.py b/products/replay_vision/backend/temporal/gemini.py
index 38862d96ce6c..7580b3e16ecf 100644
--- a/products/replay_vision/backend/temporal/gemini.py
+++ b/products/replay_vision/backend/temporal/gemini.py
@@ -4,6 +4,7 @@ from django.conf import settings
import httpx
import aiohttp
+from google.genai import types
from google.genai.errors import APIError
from products.replay_vision.backend.error_kinds import FailureKind
@@ -57,3 +58,38 @@ def classify_gemini_error(error: BaseException) -> FailureKind | None:
if isinstance(error.code, int) and 400 <= error.code < 500:
return FailureKind.PROVIDER_REJECTED
return None
+
+
+# `File.error` carries a `google.rpc.Status`, so its code is a canonical gRPC code rather than an HTTP status.
+# Only the codes that mean the provider refused this input are terminal.
+_REJECTING_FILE_ERROR_CODES = frozenset(
+ {
+ 3, # INVALID_ARGUMENT
+ 5, # NOT_FOUND
+ 7, # PERMISSION_DENIED
+ 9, # FAILED_PRECONDITION
+ 11, # OUT_OF_RANGE
+ 12, # UNIMPLEMENTED
+ 16, # UNAUTHENTICATED
+ }
+)
+
+
+def classify_gemini_file_error(error: types.FileStatus | None) -> FailureKind:
+ """Map the provider's own per-file error onto a failure kind for a file that never became ACTIVE.
+
+ A failure inside the provider's own video processing usually clears on a re-upload, so anything the codes
+ don't place as a refused input stays transient and gets the activity's retry budget. A kind that claims the
+ provider won't recover is terminal, and tells the user a retry reaches the same answer.
+ """
+ code = error.code if error else None
+ if code in _REJECTING_FILE_ERROR_CODES:
+ return FailureKind.PROVIDER_REJECTED
+ return FailureKind.PROVIDER_TRANSIENT
+
+
+def describe_gemini_file_error(error: types.FileStatus | None) -> str:
+ """Fixed-shape summary for user-visible error reasons; the provider's message can quote request content."""
+ if error and error.code is not None:
+ return f"The AI provider could not process the video (error code {error.code})"
+ return "The AI provider could not process the video"
diff --git a/products/replay_vision/backend/tests/test_temporal.py b/products/replay_vision/backend/tests/test_temporal.py
index f61e89ae8965..9dd61e399653 100644
--- a/products/replay_vision/backend/tests/test_temporal.py
+++ b/products/replay_vision/backend/tests/test_temporal.py
@@ -15,6 +15,7 @@ import httpx
import aiohttp
import psycopg.errors
from asgiref.sync import sync_to_async
+from google.genai import types
from google.genai.errors import APIError
from parameterized import parameterized
from posthoganalytics.exception_utils import exceptions_from_error_tuple
@@ -94,7 +95,7 @@ from products.replay_vision.backend.temporal.errors import (
IneligibleSessionKind,
ScannerFailureError,
)
-from products.replay_vision.backend.temporal.gemini import classify_gemini_error
+from products.replay_vision.backend.temporal.gemini import classify_gemini_error, classify_gemini_file_error
from products.replay_vision.backend.temporal.gemini_cleanup_sweep.constants import (
REDIS_INDEX_KEY as _GEMINI_REDIS_INDEX_KEY,
REDIS_KEY_PREFIX as _GEMINI_REDIS_KEY_PREFIX,
@@ -3233,6 +3234,67 @@ class TestUploadFinalizeFailures:
assert exc_info.value.message == "The AI provider did not finish the video upload"
[email protected]_db(transaction=True)
+class TestUploadedFileNotActive:
+ """A file that never reaches ACTIVE takes its kind from the provider's own file error. A kind of
+ `provider_rejected` stops Temporal retrying and tells the user to pick a different recording, so only a
+ refused input may claim it."""
+
+ @parameterized.expand(
+ [
+ ("no_error_reported", None, FailureKind.PROVIDER_TRANSIENT),
+ ("internal", 13, FailureKind.PROVIDER_TRANSIENT),
+ ("unavailable", 14, FailureKind.PROVIDER_TRANSIENT),
+ ("invalid_argument", 3, FailureKind.PROVIDER_REJECTED),
+ ("unauthenticated", 16, FailureKind.PROVIDER_REJECTED),
+ ]
+ )
+ def test_maps_file_error_codes(self, _label: str, code: int | None, expected: FailureKind) -> None:
+ error = types.FileStatus(code=code) if code is not None else None
+ assert classify_gemini_file_error(error) is expected
+
+ @staticmethod
+ def _team() -> Team:
+ org = Organization.objects.create(name="upload-org")
+ return Team.objects.create(organization=org, name="upload-team")
+
+ @parameterized.expand(
+ [
+ ("processing_failure", 13, FailureKind.PROVIDER_TRANSIENT, False),
+ ("input_rejection", 3, FailureKind.PROVIDER_REJECTED, True),
+ ]
+ )
+ @pytest.mark.asyncio
+ async def test_activity_classifies_a_failed_file(
+ self, _label: str, code: int, expected: FailureKind, expected_non_retryable: bool
+ ) -> None:
+ team = await sync_to_async(self._team)()
+ asset = await ExportedAsset.objects.acreate(team=team, export_format="video/mp4", content=b"mp4-bytes")
+ failed_file = types.File(
+ name="files/abc",
+ 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:
+ mock_client.return_value.files.upload.return_value = failed_file
+ with patch(
+ "products.replay_vision.backend.temporal.activities.upload_video_to_gemini.track_uploaded_file",
+ AsyncMock(),
+ ):
+ with pytest.raises(ScannerFailureError) as exc_info:
+ await ActivityEnvironment().run(
+ upload_video_to_gemini_activity, UploadVideoToGeminiInputs(asset_id=asset.id)
+ )
+
+ assert exc_info.value.kind is expected
+ # A provider-side processing failure must reach Temporal as retryable.
+ assert exc_info.value.non_retryable is expected_non_retryable
+ # The provider's message can quote request content, so only the shape of the failure reaches the user.
+ assert exc_info.value.message == f"The AI provider could not process the video (error code {code})"
+
+
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."""