Skip to content

Commit dba8163

Browse files
chengzeyiclaude
andcommitted
Only re-run a task when the task itself reported failure
Poll-side HTTP errors, poll retry exhaustion and wait timeouts no longer trigger a second submit when max_retries > 0, since the task already exists and may be charged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 3e92666 commit dba8163

2 files changed

Lines changed: 79 additions & 27 deletions

File tree

‎src/wavespeed/api/client.py‎

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class _SubmissionError(RuntimeError):
4444
"""A task submission failed and must not be retried automatically."""
4545

4646

47+
class _TaskFailedError(RuntimeError):
48+
"""The task itself reported status ``failed``; a task-level re-run is allowed."""
49+
50+
4751
class Client:
4852
"""WaveSpeed API client.
4953
@@ -332,44 +336,26 @@ def _wait(
332336

333337
if status in ("failed", "cancelled", "timeout", "deleted"):
334338
error = data.get("error") or "Unknown error"
335-
raise RuntimeError(
336-
f"Prediction {status} (task_id: {request_id}): {error}"
337-
)
339+
error_cls = _TaskFailedError if status == "failed" else RuntimeError
340+
raise error_cls(f"Prediction {status} (task_id: {request_id}): {error}")
338341

339342
time.sleep(poll_interval)
340343

341344
def _is_retryable_error(self, error: Exception) -> bool:
342-
"""Determine if an error is worth retrying at the task level.
345+
"""Determine if an error is worth re-running the task for.
343346
344347
Args:
345348
error: The exception to check.
346349
347350
Returns:
348351
True if the error is retryable.
349352
"""
350-
# Submission errors are ambiguous: the server may already have created
351-
# the task, so never turn them into another POST automatically.
352-
if isinstance(error, _SubmissionError):
353-
return False
354-
355-
# Retry timeout and connection errors from result-query GETs.
356-
if isinstance(
357-
error,
358-
(
359-
requests.exceptions.Timeout,
360-
requests.exceptions.ConnectionError,
361-
TimeoutError,
362-
),
363-
):
364-
return True
365-
366-
# Retry server errors (5xx) and rate limiting (429)
367-
if isinstance(error, RuntimeError):
368-
error_str = str(error)
369-
if "HTTP 5" in error_str or "HTTP 429" in error_str:
370-
return True
371-
372-
return False
353+
# Only a failure reported by the task itself is re-run. Submission
354+
# errors are ambiguous (the task may already exist), and poll-side HTTP
355+
# errors, poll retry exhaustion and client-side wait timeouts all happen
356+
# after the task was created and may be charged, so none of them may
357+
# turn into another POST.
358+
return isinstance(error, _TaskFailedError)
373359

374360
def _format_sync_mode_error(self, data: dict[str, Any]) -> str:
375361
"""Build an actionable error for a non-completed sync-mode response."""

‎tests/test_api.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,72 @@ def test_task_retries_do_not_repeat_failed_submission(self, mock_post):
149149

150150
mock_post.assert_called_once()
151151

152+
@staticmethod
153+
def _response(status_code, payload=None, text=""):
154+
response = MagicMock()
155+
response.status_code = status_code
156+
response.text = text
157+
response.json.return_value = payload
158+
return response
159+
160+
@patch("wavespeed.api.client.time.sleep")
161+
@patch("wavespeed.api.client.requests.get")
162+
@patch("wavespeed.api.client.requests.post")
163+
def test_task_retries_do_not_repost_after_poll_retry_exhaustion(
164+
self, mock_post, mock_get, _mock_sleep
165+
):
166+
"""A poll that exhausts its GET retries must not create a second task."""
167+
mock_post.return_value = self._response(200, {"data": {"id": "req-1"}})
168+
mock_get.return_value = self._response(502, text="Bad Gateway")
169+
170+
client = Client(api_key="test-key", max_retries=2, max_connection_retries=1)
171+
with self.assertRaises(RuntimeError) as ctx:
172+
client.run("wavespeed-ai/z-image/turbo", {"prompt": "test"})
173+
174+
self.assertIn("req-1", str(ctx.exception))
175+
self.assertIn("HTTP 502", str(ctx.exception))
176+
self.assertEqual(mock_post.call_count, 1)
177+
self.assertEqual(mock_get.call_count, 2)
178+
179+
@patch("wavespeed.api.client.time.sleep")
180+
@patch("wavespeed.api.client.requests.get")
181+
@patch("wavespeed.api.client.requests.post")
182+
def test_task_retries_do_not_repost_after_wait_timeout(
183+
self, mock_post, mock_get, _mock_sleep
184+
):
185+
"""A client-side wait timeout leaves the task alive and must not re-POST."""
186+
mock_post.return_value = self._response(200, {"data": {"id": "req-1"}})
187+
mock_get.return_value = self._response(200, {"data": {"status": "processing"}})
188+
189+
client = Client(api_key="test-key", max_retries=2)
190+
with self.assertRaises(TimeoutError) as ctx:
191+
client.run("wavespeed-ai/z-image/turbo", {"prompt": "test"}, timeout=0)
192+
193+
self.assertIn("req-1", str(ctx.exception))
194+
self.assertEqual(mock_post.call_count, 1)
195+
196+
@patch("wavespeed.api.client.time.sleep")
197+
@patch("wavespeed.api.client.requests.get")
198+
@patch("wavespeed.api.client.requests.post")
199+
def test_task_retries_rerun_when_task_reports_failed(
200+
self, mock_post, mock_get, _mock_sleep
201+
):
202+
"""A task that itself reports failed is re-run when max_retries > 0."""
203+
mock_post.side_effect = [
204+
self._response(200, {"data": {"id": "req-1"}}),
205+
self._response(200, {"data": {"id": "req-2"}}),
206+
]
207+
mock_get.side_effect = [
208+
self._response(200, {"data": {"status": "failed", "error": "oom"}}),
209+
self._response(200, {"data": {"status": "completed", "outputs": ["url"]}}),
210+
]
211+
212+
client = Client(api_key="test-key", max_retries=1)
213+
result = client.run("wavespeed-ai/z-image/turbo", {"prompt": "test"})
214+
215+
self.assertEqual(result, {"outputs": ["url"]})
216+
self.assertEqual(mock_post.call_count, 2)
217+
152218
@patch("wavespeed.api.client.requests.get")
153219
def test_get_result_success(self, mock_get):
154220
"""Test successful result retrieval."""

0 commit comments

Comments
 (0)