Skip to content

Commit 1f7e1c9

Browse files
niklassemmlerclaude
andcommitted
fix(media): stop retrying API errors the API client already retried
media.get_upload_url and media.patch go through the generated client, which retries 429/5xx itself. The outer backoff retried them again, turning one 429 into 9 requests. Refs LFE-17361 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 1a301c7 commit 1f7e1c9

2 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎langfuse/_task_manager/media_manager.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,10 @@ def _request_with_backoff(
509509
self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs
510510
) -> T:
511511
def _should_give_up(e: Exception) -> bool:
512+
# The API client already retried 408/409/429/5xx responses before
513+
# raising, so retrying here would multiply the requests.
512514
if isinstance(e, ApiError):
513-
return (
514-
e.status_code is not None
515-
and 400 <= e.status_code < 500
516-
and e.status_code != 429
517-
)
515+
return True
518516
if isinstance(e, httpx.HTTPStatusError):
519517
return (
520518
e.response is not None

‎tests/unit/test_media_manager.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from queue import Queue
23
from types import SimpleNamespace
34
from unittest.mock import Mock
@@ -6,6 +7,8 @@
67
import pytest
78

89
from langfuse._task_manager.media_manager import MediaManager
10+
from langfuse.api import LangfuseAPI
11+
from langfuse.api.core.api_error import ApiError
912
from langfuse.media import LangfuseMedia
1013

1114

@@ -277,3 +280,32 @@ def test_find_and_process_media_gemini_inline_data_non_string_data_passes_throug
277280

278281
assert result == data
279282
assert queue.empty()
283+
284+
285+
@pytest.mark.parametrize("status_code", [429, 503])
286+
def test_media_api_calls_do_not_multiply_client_retries(monkeypatch, status_code):
287+
monkeypatch.setattr(time, "sleep", lambda _seconds: None)
288+
requests = []
289+
290+
def handler(request: httpx.Request) -> httpx.Response:
291+
requests.append(request)
292+
return httpx.Response(status_code, json={})
293+
294+
api_client = LangfuseAPI(
295+
base_url="http://test-host",
296+
username="pk",
297+
password="sk",
298+
httpx_client=httpx.Client(transport=httpx.MockTransport(handler)),
299+
)
300+
manager = MediaManager(
301+
api_client=api_client,
302+
httpx_client=Mock(),
303+
media_upload_queue=Queue(),
304+
max_retries=3,
305+
)
306+
307+
with pytest.raises(ApiError):
308+
manager._process_upload_media_job(data=_upload_job())
309+
310+
# the API client's own retries (2) are the only ones: 3 requests in total
311+
assert len(requests) == 3

0 commit comments

Comments
 (0)