Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion langfuse/_utils/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading