From 075c82a0920fb52063302c0eef8abd4777a50be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Luiz=20Santos=20Neto?= Date: Wed, 18 Nov 2026 11:36:01 -0300 Subject: [PATCH] fix(prompts): retry transient HTTPX response hook errors --- langfuse/_utils/retry.py | 4 +++- tests/unit/test_prompt.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/langfuse/_utils/retry.py b/langfuse/_utils/retry.py index d2f53a1ea..d249bacf8 100644 --- a/langfuse/_utils/retry.py +++ b/langfuse/_utils/retry.py @@ -31,9 +31,11 @@ def is_retryable_status(status_code: int) -> bool: def is_retryable_api_call_error(error: Exception) -> bool: - """Return True for network errors and 408/409/429/5xx API errors.""" + """Return True for network errors and 408/409/429/5xx responses.""" if isinstance(error, httpx.TransportError): return True + if isinstance(error, httpx.HTTPStatusError): + return is_retryable_status(error.response.status_code) if isinstance(error, ApiError) and error.status_code is not None: return is_retryable_status(error.status_code) diff --git a/tests/unit/test_prompt.py b/tests/unit/test_prompt.py index 9afec0507..495927a8e 100644 --- a/tests/unit/test_prompt.py +++ b/tests/unit/test_prompt.py @@ -880,3 +880,35 @@ def handler(request: httpx.Request) -> httpx.Response: assert len(requests) == 1 assert sleeps == [] + + +@pytest.mark.parametrize("status_code", [400, 429, 503]) +def test_get_prompt_honors_retry_policy_with_httpx_response_hook( + langfuse, sleeps, status_code +): + name = f"prompt_with_response_hook_{status_code}" + requests = [] + + def handler(request: httpx.Request) -> httpx.Response: + requests.append(request) + if len(requests) == 1: + return httpx.Response(status_code, headers={"Retry-After": "3"}, json={}) + return httpx.Response(200, json=_prompt_json(name)) + + with httpx.Client( + transport=httpx.MockTransport(handler), + event_hooks={"response": [httpx.Response.raise_for_status]}, + ) as http_client: + langfuse.api = LangfuseAPI( + base_url="http://test-host", httpx_client=http_client + ) + + if status_code == 400: + with pytest.raises(httpx.HTTPStatusError): + langfuse.get_prompt(name, max_retries=1) + assert len(requests) == 1 + assert sleeps == [] + else: + assert langfuse.get_prompt(name, max_retries=1).prompt == "Make me laugh" + assert len(requests) == 2 + assert sleeps == [3]