From 93b12894011abd555ced5d401bcf27b42a668316 Mon Sep 17 00:00:00 2001 From: daiyu Date: Sat, 10 Oct 2026 23:42:05 +0800 Subject: [PATCH] fix(inference): retry transient API errors and stop writing null completions inference_api had no error handling at all, so a single HTTP 429/5xx or timeout propagated out of asyncio.as_completed and aborted the whole run - with the output file already wiped, one transient error cost an entire 80k-question run. - retry transient failures (429/5xx/timeouts/connection resets) with exponential backoff, honouring Retry-After when the server sends it; configurable via max_retries / retry_base_delay / retry_max_delay. Permanent errors (400, 401, ...) are raised immediately - add raise_on_error (default False) and max_concurrency (default 32) so the error policy matches inference_function: a failed request is logged and recorded as an empty completion instead of killing the run - coerce "content": null to "" - written out as-is it made evaluate() fail the "completion must be str" assertion for the whole file - guard against an empty choices list (previously an IndexError) _http_post_completion_async keeps its signature and behaviour for non-error responses, so the existing tests stay green. Closes #123 --- llmsql/inference/inference_api.py | 235 ++++++++++++++++- tests/inference/test_inference_api.py | 363 +++++++++++++++++++++++++- 2 files changed, 586 insertions(+), 12 deletions(-) diff --git a/llmsql/inference/inference_api.py b/llmsql/inference/inference_api.py index 7746cb7..cc3e4c1 100644 --- a/llmsql/inference/inference_api.py +++ b/llmsql/inference/inference_api.py @@ -39,6 +39,69 @@ load_dotenv() +# Retry policy for transient API failures (see ``inference_api``). +DEFAULT_MAX_RETRIES = 3 +DEFAULT_RETRY_BASE_DELAY = 1.0 +DEFAULT_RETRY_MAX_DELAY = 30.0 + +# Statuses worth retrying: request timeouts, conflicts, rate limiting. +# Anything >= 500 is retried as well; 4xx client errors are not (they will +# fail identically on every attempt). +RETRYABLE_STATUS_CODES = frozenset({408, 409, 425, 429}) + +# Same default as ``inference_function`` so both backends behave alike. +DEFAULT_MAX_CONCURRENCY = 32 + + +class _HTTPStatusError(RuntimeError): + """Raised when the API answers with an HTTP error status. + + ``aiohttp``'s ``raise_for_status()`` does not expose the response headers, + so the status is checked explicitly and the ``Retry-After`` hint (when the + server sends one) is carried along for the retry loop to honour. + """ + + def __init__(self, status: int, retry_after: float | None = None, + message: str = "") -> None: + super().__init__(message or f"API returned HTTP {status}") + self.status = status + self.retry_after = retry_after + + +def _retry_after_seconds(headers: Any) -> float | None: + """Parse the ``Retry-After`` header (seconds form), if present.""" + try: + value = headers.get("Retry-After") + except (AttributeError, TypeError): + return None + if value is None: + return None + try: + seconds = float(str(value).strip()) + except (TypeError, ValueError): + # HTTP-date form — not parsed; the exponential backoff is used instead. + return None + return seconds if seconds >= 0 else None + + +def _is_retryable_error(exc: BaseException) -> bool: + """Whether a failed request is worth another attempt.""" + status = getattr(exc, "status", None) + if isinstance(status, int): + return status in RETRYABLE_STATUS_CODES or status >= 500 + # Connection resets, DNS hiccups and read timeouts. + return isinstance(exc, (asyncio.TimeoutError, aiohttp.ClientConnectionError)) + + +def _retry_delay(exc: BaseException, attempt: int, base_delay: float, + max_delay: float) -> float: + """Seconds to wait before retry ``attempt + 1`` (``attempt`` is 0-based).""" + hint = getattr(exc, "retry_after", None) + if hint is None: + hint = base_delay * (2 ** attempt) + return max(0.0, min(float(hint), max_delay)) + + class _AsyncRateLimiter: """ Token-bucket style async rate limiter. @@ -88,6 +151,14 @@ async def _post_chat_completion_async( async with session.post( url, json=payload, timeout=aiohttp.ClientTimeout(total=timeout) ) as resp: + status = getattr(resp, "status", None) + if isinstance(status, int) and status >= 400: + retry_after = _retry_after_seconds(getattr(resp, "headers", None)) + try: + detail = (await resp.text())[:200] + except Exception: # pragma: no cover - body may be unreadable + detail = "" + raise _HTTPStatusError(status, retry_after, detail) resp.raise_for_status() parsed: dict[str, Any] = await resp.json() @@ -96,6 +167,47 @@ async def _post_chat_completion_async( return parsed +async def _post_with_retries( + *, + session: aiohttp.ClientSession, + base_url: str, + endpoint: str, + payload: dict[str, Any], + timeout: float, + max_retries: int, + retry_base_delay: float, + retry_max_delay: float, +) -> dict[str, Any]: + """Send one chat completion request, retrying transient failures. + + A single HTTP 429 / 5xx or a timeout used to propagate out of + ``asyncio.as_completed`` and abort the entire run — with the output file + already wiped, one transient error cost a whole benchmark. Transient + failures are therefore retried with exponential backoff (honouring + ``Retry-After``); permanent ones (400, 401, ...) are raised immediately. + """ + attempt = 0 + while True: + try: + return await _post_chat_completion_async( + session=session, + base_url=base_url, + endpoint=endpoint, + payload=payload, + timeout=timeout, + ) + except Exception as exc: # noqa: BLE001 - re-raised below when final + if attempt >= max_retries or not _is_retryable_error(exc): + raise + delay = _retry_delay(exc, attempt, retry_base_delay, retry_max_delay) + log.warning( + f"API request failed ({type(exc).__name__}: {exc}); retrying in " + f"{delay:.2f}s ({attempt + 1}/{max_retries})." + ) + attempt += 1 + await asyncio.sleep(delay) + + async def _inference_api_async( model_name: str, *, @@ -104,6 +216,11 @@ async def _inference_api_async( headers: dict[str, str], timeout: float, requests_per_minute: float | None, + max_concurrency: int | None, + raise_on_error: bool, + max_retries: int, + retry_base_delay: float, + retry_max_delay: float, api_kwargs: dict[str, Any], questions: list[dict[str, Any]], tables: dict[str, Any], @@ -111,16 +228,20 @@ async def _inference_api_async( output_file: str, ) -> list[dict[str, str]]: limiter = _AsyncRateLimiter(requests_per_minute) + semaphore = ( + asyncio.Semaphore(max_concurrency) if max_concurrency is not None else None + ) all_results: list[dict[str, str]] = [] # Lock to serialise file writes while allowing concurrent HTTP calls. write_lock = asyncio.Lock() + n_failed = 0 async with aiohttp.ClientSession(headers=headers) as session: # Pre-build all prompts using the shared function prompts = build_all_requests(questions, tables, prompt_builder) - async def process_question(q: dict[str, Any], prompt: str) -> dict[str, str]: + async def request_completion(q: dict[str, Any], prompt: str) -> str: payload = { "model": model_name, "messages": [ @@ -133,14 +254,48 @@ async def process_question(q: dict[str, Any], prompt: str) -> dict[str, str]: # the HTTP round-trip time doesn't count against the interval. await limiter.acquire() - response = await _post_chat_completion_async( + response = await _post_with_retries( session=session, base_url=base_url, endpoint=endpoint, payload=payload, timeout=timeout, + max_retries=max_retries, + retry_base_delay=retry_base_delay, + retry_max_delay=retry_max_delay, ) - completion = response["choices"][0]["message"]["content"] + + choices = response.get("choices") or [] + if not choices: + raise ValueError("API response contains no choices.") + + content = choices[0].get("message", {}).get("content") + # Some APIs answer with "content": null (refusals, reasoning and + # tool-call responses). Written out as-is it makes `evaluate()` + # fail the `completion must be str` assertion for the whole file. + return "" if content is None else content + + async def call_with_policy(q: dict[str, Any], prompt: str) -> str: + try: + return await request_completion(q, prompt) + except Exception as e: # noqa: BLE001 - policy applied below + if raise_on_error: + raise + nonlocal n_failed + n_failed += 1 + qid = q.get("question_id", q.get("id", "")) + log.error( + f"API request failed for question_id={qid!r}: " + f"{type(e).__name__}: {e}. Recording an empty completion." + ) + return "" + + async def process_question(q: dict[str, Any], prompt: str) -> dict[str, str]: + if semaphore is not None: + async with semaphore: + completion = await call_with_policy(q, prompt) + else: + completion = await call_with_policy(q, prompt) result = { "question_id": q.get("question_id", q.get("id", "")), @@ -152,14 +307,30 @@ async def process_question(q: dict[str, Any], prompt: str) -> dict[str, str]: return result - tasks = [process_question(q, p) for q, p in zip(questions, prompts)] - for coro in tqdm( - asyncio.as_completed(tasks), - total=len(tasks), - desc="Generating", - ): - result = await coro - all_results.append(result) + tasks = [ + asyncio.ensure_future(process_question(q, p)) + for q, p in zip(questions, prompts, strict=False) + ] + try: + for coro in tqdm( + asyncio.as_completed(tasks), + total=len(tasks), + desc="Generating", + ): + result = await coro + all_results.append(result) + except BaseException: + # Stop all in-flight / pending requests before propagating. + for t in tasks: + t.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + raise + + if n_failed: + log.warning( + f"{n_failed}/{len(tasks)} API requests failed; their completions " + "were recorded as empty strings." + ) return all_results @@ -172,6 +343,11 @@ def inference_api( api_key: str | None = None, timeout: float = 120.0, requests_per_minute: float | None = None, + max_concurrency: int | None = DEFAULT_MAX_CONCURRENCY, + raise_on_error: bool = False, + max_retries: int = DEFAULT_MAX_RETRIES, + retry_base_delay: float = DEFAULT_RETRY_BASE_DELAY, + retry_max_delay: float = DEFAULT_RETRY_MAX_DELAY, api_kwargs: dict[str, Any] | None = None, request_headers: dict[str, str] | None = None, version: Literal["1.0", "2.0"] = DEFAULT_LLMSQL_VERSION, @@ -194,6 +370,21 @@ def inference_api( base_url: e.g. "https://api.openai.com/v1/" endpoint: e.g. "chat/completions" + max_concurrency: Maximum number of HTTP requests in flight at the same + time, or ``None`` to leave it unbounded (aiohttp's connector limit + then applies). Defaults to 32, like ``inference_function``. + raise_on_error: If ``True``, re-raise the first error and cancel the + remaining requests. If ``False`` (default), log the error and + record an empty completion for that question so a single failure + cannot abort the whole run. + max_retries: How many times a *transient* failure (HTTP 429/5xx, + timeout, connection reset) is retried before giving up. ``0`` + disables retries. Permanent errors (e.g. 400, 401) are never + retried. + retry_base_delay: First retry delay in seconds; doubled on every + further attempt, unless the server sends ``Retry-After``. + retry_max_delay: Upper bound for a single retry delay. + # Benchmark: version: LLMSQL version output_file: Path to write outputs (will be overwritten). @@ -213,6 +404,23 @@ def inference_api( api_kwargs = api_kwargs or {} request_headers = request_headers or {} + if max_concurrency is not None and ( + isinstance(max_concurrency, bool) + or not isinstance(max_concurrency, int) + or max_concurrency <= 0 + ): + raise ValueError( + f"`max_concurrency` must be a positive integer or None, got {max_concurrency!r}." + ) + if ( + isinstance(max_retries, bool) + or not isinstance(max_retries, int) + or max_retries < 0 + ): + raise ValueError( + f"`max_retries` must be a non-negative integer, got {max_retries!r}." + ) + workdir = resolve_workdir_path(workdir_path) repo_id = get_repo_id(version) @@ -255,6 +463,11 @@ def inference_api( headers=headers, timeout=timeout, requests_per_minute=requests_per_minute, + max_concurrency=max_concurrency, + raise_on_error=raise_on_error, + max_retries=max_retries, + retry_base_delay=retry_base_delay, + retry_max_delay=retry_max_delay, api_kwargs=api_kwargs, questions=questions, tables=tables, diff --git a/tests/inference/test_inference_api.py b/tests/inference/test_inference_api.py index 56af026..4a9eb07 100644 --- a/tests/inference/test_inference_api.py +++ b/tests/inference/test_inference_api.py @@ -11,7 +11,12 @@ import pytest import llmsql.inference.inference_api as api_mod -from llmsql.inference.inference_api import _AsyncRateLimiter, inference_api +from llmsql.inference.inference_api import ( + _AsyncRateLimiter, + _HTTPStatusError, + _retry_delay, + inference_api, +) # --------------------------------------------------------------------------- # Helpers @@ -162,6 +167,177 @@ async def _run(): with pytest.raises(ValueError, match="does not contain `choices`"): asyncio.run(_run()) + def test_http_error_status_raises_with_retry_after(self): + """A 4xx/5xx answer becomes ``_HTTPStatusError`` carrying ``Retry-After``.""" + resp = _FakeHttpResponse( + status=429, payload=None, headers={"Retry-After": "2"}, text="slow down" + ) + session = MagicMock() + session.post = MagicMock(return_value=resp) + + async def _run(): + return await api_mod._post_chat_completion_async( + session=session, + base_url="http://fake/v1", + endpoint="chat/completions", + payload={}, + timeout=5.0, + ) + + with pytest.raises(_HTTPStatusError) as exc_info: + asyncio.run(_run()) + + assert exc_info.value.status == 429 + assert exc_info.value.retry_after == 2.0 + + +class _FakeHttpResponse: + """Minimal stand-in for an ``aiohttp`` response with a real status code.""" + + def __init__(self, status=200, payload=None, headers=None, text=""): + self.status = status + self.headers = headers or {} + self._payload = payload if payload is not None else {} + self._text = text + + async def __aenter__(self): + return self + + async def __aexit__(self, *args): + return False + + def raise_for_status(self): + return None + + async def json(self): + return self._payload + + async def text(self): + return self._text + + +# --------------------------------------------------------------------------- +# Retry policy +# --------------------------------------------------------------------------- + + +class TestRetryPolicy: + def test_retry_delay_prefers_retry_after(self): + """A server-supplied ``Retry-After`` wins over the exponential backoff.""" + assert _retry_delay(_HTTPStatusError(429, retry_after=2.5), 0, 1.0, 30.0) == 2.5 + + def test_retry_delay_doubles_and_is_capped(self): + assert _retry_delay(ValueError("boom"), 0, 1.0, 30.0) == 1.0 + assert _retry_delay(ValueError("boom"), 1, 1.0, 30.0) == 2.0 + assert _retry_delay(ValueError("boom"), 4, 1.0, 5.0) == 5.0 + + def test_retry_delay_is_never_negative(self): + assert _retry_delay(_HTTPStatusError(429, retry_after=-1), 0, 1.0, 30.0) == 0.0 + + def test_transient_error_is_retried_then_succeeds(self, monkeypatch): + """429 -> 200: the run must continue with the retried response.""" + calls = {"n": 0} + + async def fake_post(**_kwargs): + calls["n"] += 1 + if calls["n"] == 1: + raise _HTTPStatusError(429) + return {"choices": [{"message": {"content": "SELECT 1"}}]} + + monkeypatch.setattr(api_mod, "_post_chat_completion_async", fake_post) + + result = asyncio.run( + api_mod._post_with_retries( + session=None, + base_url="http://fake/v1", + endpoint="chat/completions", + payload={}, + timeout=5.0, + max_retries=3, + retry_base_delay=0.0, + retry_max_delay=0.0, + ) + ) + + assert result["choices"][0]["message"]["content"] == "SELECT 1" + assert calls["n"] == 2 + + def test_permanent_error_is_not_retried(self, monkeypatch): + """A 400 will fail identically every time — don't waste attempts.""" + calls = {"n": 0} + + async def fake_post(**_kwargs): + calls["n"] += 1 + raise _HTTPStatusError(400) + + monkeypatch.setattr(api_mod, "_post_chat_completion_async", fake_post) + + with pytest.raises(_HTTPStatusError): + asyncio.run( + api_mod._post_with_retries( + session=None, + base_url="http://fake/v1", + endpoint="chat/completions", + payload={}, + timeout=5.0, + max_retries=3, + retry_base_delay=0.0, + retry_max_delay=0.0, + ) + ) + + assert calls["n"] == 1 + + def test_retries_are_bounded_by_max_retries(self, monkeypatch): + calls = {"n": 0} + + async def fake_post(**_kwargs): + calls["n"] += 1 + raise _HTTPStatusError(503) + + monkeypatch.setattr(api_mod, "_post_chat_completion_async", fake_post) + + with pytest.raises(_HTTPStatusError): + asyncio.run( + api_mod._post_with_retries( + session=None, + base_url="http://fake/v1", + endpoint="chat/completions", + payload={}, + timeout=5.0, + max_retries=2, + retry_base_delay=0.0, + retry_max_delay=0.0, + ) + ) + + assert calls["n"] == 3 # 1 initial attempt + 2 retries + + def test_connection_errors_are_retried(self, monkeypatch): + calls = {"n": 0} + + async def fake_post(**_kwargs): + calls["n"] += 1 + if calls["n"] == 1: + raise api_mod.aiohttp.ClientConnectionError("connection reset") + return {"choices": [{"message": {"content": "SELECT 1"}}]} + + monkeypatch.setattr(api_mod, "_post_chat_completion_async", fake_post) + + asyncio.run( + api_mod._post_with_retries( + session=None, + base_url="http://fake/v1", + endpoint="chat/completions", + payload={}, + timeout=5.0, + max_retries=1, + retry_base_delay=0.0, + retry_max_delay=0.0, + ) + ) + assert calls["n"] == 2 + # --------------------------------------------------------------------------- # inference_api integration-style tests (HTTP mocked) @@ -345,3 +521,188 @@ async def _run_inside_loop(): assert apply_called["n"] >= 1, "nest_asyncio.apply() should have been called" assert len(results) == 2 + + +# --------------------------------------------------------------------------- +# Failure handling (issue #123) +# --------------------------------------------------------------------------- + + +class TestInferenceApiFailureHandling: + """One bad request must not cost the whole run.""" + + @staticmethod + def _patch_post(monkeypatch, handler): + monkeypatch.setattr(api_mod, "_post_chat_completion_async", handler) + + def test_null_content_becomes_empty_string(self, monkeypatch, tmp_path): + """ + Regression: ``"content": null`` used to be written out as-is, and + ``evaluate()`` then failed the ``completion must be str`` assertion + for the entire predictions file. + """ + + async def handler(**_kwargs): + return {"choices": [{"message": {"content": None}}]} + + self._patch_post(monkeypatch, handler) + _, _, outpath = _make_fixtures(tmp_path) + + results = inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + ) + + assert [r["completion"] for r in results] == ["", ""] + for line in outpath.read_text().strip().splitlines(): + assert json.loads(line)["completion"] == "" + + def test_permanent_failure_records_empty_completion(self, monkeypatch, tmp_path): + """With ``raise_on_error=False`` a dead request yields an empty completion.""" + + async def handler(**_kwargs): + raise _HTTPStatusError(400) + + self._patch_post(monkeypatch, handler) + _, _, outpath = _make_fixtures(tmp_path) + + results = inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + limit=1, + max_retries=0, + ) + + assert results == [{"question_id": "q1", "completion": ""}] + + def test_raise_on_error_propagates(self, monkeypatch, tmp_path): + """With ``raise_on_error=True`` the failure aborts the run, as before.""" + + async def handler(**_kwargs): + raise _HTTPStatusError(400) + + self._patch_post(monkeypatch, handler) + _, _, outpath = _make_fixtures(tmp_path) + + with pytest.raises(_HTTPStatusError): + inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + limit=1, + max_retries=0, + raise_on_error=True, + ) + + def test_transient_failure_is_retried_and_run_completes( + self, monkeypatch, tmp_path + ): + """A single 500 is retried, so the run still produces real completions.""" + calls = {"n": 0} + + async def handler(**_kwargs): + calls["n"] += 1 + if calls["n"] == 1: + raise _HTTPStatusError(500) + return {"choices": [{"message": {"content": "SELECT 1"}}]} + + self._patch_post(monkeypatch, handler) + _, _, outpath = _make_fixtures(tmp_path) + + results = inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + limit=1, + max_retries=2, + retry_base_delay=0.0, + retry_max_delay=0.0, + ) + + assert results == [{"question_id": "q1", "completion": "SELECT 1"}] + assert calls["n"] == 2 + + def test_empty_choices_are_treated_as_failure(self, monkeypatch, tmp_path): + """``choices: []`` must not raise IndexError out of the run.""" + + async def handler(**_kwargs): + return {"choices": []} + + self._patch_post(monkeypatch, handler) + _, _, outpath = _make_fixtures(tmp_path) + + results = inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + limit=1, + max_retries=0, + ) + + assert results == [{"question_id": "q1", "completion": ""}] + + def test_invalid_max_concurrency_raises(self, monkeypatch, tmp_path): + self._patch_post(monkeypatch, _fake_post_success()) + _, _, outpath = _make_fixtures(tmp_path) + + with pytest.raises(ValueError, match="max_concurrency"): + inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + max_concurrency=0, + ) + + def test_invalid_max_retries_raises(self, monkeypatch, tmp_path): + self._patch_post(monkeypatch, _fake_post_success()) + _, _, outpath = _make_fixtures(tmp_path) + + with pytest.raises(ValueError, match="max_retries"): + inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + max_retries=-1, + ) + + def test_concurrency_cap_is_respected(self, monkeypatch, tmp_path): + """``max_concurrency`` must bound how many requests run at once.""" + in_flight = {"now": 0, "peak": 0} + + async def handler(**_kwargs): + in_flight["now"] += 1 + in_flight["peak"] = max(in_flight["peak"], in_flight["now"]) + await asyncio.sleep(0.01) + in_flight["now"] -= 1 + return {"choices": [{"message": {"content": "SELECT 1"}}]} + + self._patch_post(monkeypatch, handler) + _, _, outpath = _make_fixtures(tmp_path) + + inference_api( + model_name="dummy", + base_url="http://localhost:9999/v1", + output_file=str(outpath), + workdir_path=str(tmp_path), + max_concurrency=1, + ) + + assert in_flight["peak"] == 1 + + +def _fake_post_success(): + """Return a handler that always answers with a valid completion.""" + + async def handler(**_kwargs): + return {"choices": [{"message": {"content": "SELECT 1"}}]} + + return handler