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.

feat(ai-observability): route translation through the Go ai-gateway (#124794)

Jonathan Mielooff67925ba07d
diff --git a/products/ai_observability/backend/api/test/test_translate.py b/products/ai_observability/backend/api/test/test_translate.py
index 60cc37dbc84d..47a7922b352f 100644
--- a/products/ai_observability/backend/api/test/test_translate.py
+++ b/products/ai_observability/backend/api/test/test_translate.py
@@ -13,7 +13,7 @@ from products.ai_observability.backend.translation.constants import (
TRANSLATION_MODEL,
)
-MOCK_PATH = "products.ai_observability.backend.translation.llm.get_llm_client"
+MOCK_PATH = "products.ai_observability.backend.translation.llm.build_openai_client"
def mock_llm_client(mock_get_client: MagicMock) -> MagicMock:
diff --git a/products/ai_observability/backend/translation/llm.py b/products/ai_observability/backend/translation/llm.py
index b71402a73e62..222e8467549d 100644
--- a/products/ai_observability/backend/translation/llm.py
+++ b/products/ai_observability/backend/translation/llm.py
@@ -3,7 +3,7 @@
import structlog
from openai import APITimeoutError
-from posthog.llm.gateway_client import get_llm_client
+from posthog.llm.gateway_client import build_openai_client
from .constants import SUPPORTED_LANGUAGES, TRANSLATION_MODEL, TRANSLATION_TIMEOUT_SECONDS
@@ -34,7 +34,12 @@ def translate_text(text: str, target_language: str, user_distinct_id: str | None
# way. The SDK retries twice by default, which makes the caller wait three timeouts before
# seeing an error. One attempt with a larger budget fails sooner and leaves a text at the
# 10,000 character cap more room to finish.
- client = get_llm_client("llma_translation").with_options(max_retries=0)
+ distinct_id = user_distinct_id or "llma-translation"
+ client = build_openai_client(
+ "llma_translation",
+ ai_product="llma_translation",
+ distinct_id=distinct_id,
+ ).with_options(max_retries=0)
target_name = SUPPORTED_LANGUAGES.get(target_language, target_language)
@@ -50,7 +55,7 @@ def translate_text(text: str, target_language: str, user_distinct_id: str | None
{"role": "user", "content": text},
],
timeout=TRANSLATION_TIMEOUT_SECONDS,
- user=user_distinct_id or "llma-translation",
+ user=distinct_id,
)
except APITimeoutError as e:
raise TranslationTimeoutError from e
diff --git a/products/ai_observability/backend/translation/test/test_llm.py b/products/ai_observability/backend/translation/test/test_llm.py
index fc3f57faa97c..64214217ed9a 100644
--- a/products/ai_observability/backend/translation/test/test_llm.py
+++ b/products/ai_observability/backend/translation/test/test_llm.py
@@ -10,7 +10,7 @@ from parameterized import parameterized
from products.ai_observability.backend.translation.constants import SUPPORTED_LANGUAGES, TRANSLATION_MODEL
from products.ai_observability.backend.translation.llm import TranslationTimeoutError, translate_text
-MOCK_PATH = "products.ai_observability.backend.translation.llm.get_llm_client"
+MOCK_PATH = "products.ai_observability.backend.translation.llm.build_openai_client"
def mock_llm_client(mock_get_client: MagicMock) -> MagicMock:
@@ -72,7 +72,9 @@ class TestTranslateText:
translate_text("Test", "es")
- mock_get_client.assert_called_once_with("llma_translation")
+ mock_get_client.assert_called_once_with(
+ "llma_translation", ai_product="llma_translation", distinct_id="llma-translation"
+ )
@patch(MOCK_PATH)
def test_disables_client_retries(self, mock_get_client):