Skip to content

fix(inference): retry transient API errors and stop writing null completions - #140

Open
ddaihao2022 wants to merge 1 commit into
LLMSQL:mainfrom
ddaihao2022:fix-123-api-retries-and-null-completions
Open

ddaihao2022 wants to merge 1 commit into
LLMSQL:mainfrom
ddaihao2022:fix-123-api-retries-and-null-completions

Conversation

@ddaihao2022

Copy link
Copy Markdown
Contributor

Closes #123

What was broken

inference_api() dispatched every request concurrently with no error handling:

  1. One transient error killed the whole run. _post_chat_completion_async called resp.raise_for_status(); a single 429 / 5xx / timeout propagated out of asyncio.as_completed and aborted everything. Since inference_api wipes the output file on start, one blip cost an entire 80k-request run.
  2. "content": null was written out as-is, so evaluate() later failed the completion must be str assertion for the whole predictions file (refusals, some reasoning models, tool-call responses).
  3. No max_concurrency / raise_on_error, unlike inference_function, which already had both.

What this changes

  • Retries for transient failures. New _post_with_retries() retries HTTP 429 / 5xx, timeouts and connection resets with exponential backoff, honouring Retry-After when the server sends it. Controlled by max_retries (default 3), retry_base_delay (default 1.0) and retry_max_delay (default 30.0); max_retries=0 disables retries. Permanent errors (400, 401, …) are raised immediately instead of burning attempts.
    • The status is now checked explicitly (resp.status) rather than only via raise_for_status(), because aiohttp's ClientResponseError does not carry the response headers — that is the only way to read Retry-After.
  • raise_on_error (default False, like inference_function): a request that still fails is logged and recorded as an empty completion, so the run finishes. With raise_on_error=True the first error cancels the remaining in-flight requests and propagates, exactly as before. A summary warning is logged at the end when anything failed.
  • max_concurrency (default 32, like inference_function) bounds in-flight requests with an asyncio.Semaphore; None keeps the previous unbounded behaviour.
  • null content is coerced to "", and an empty choices list is treated as a failure instead of raising IndexError.

No behaviour change for successful responses: _post_chat_completion_async keeps its signature and its ValueError for a missing choices key, so the existing tests stay green. The CLI is untouched (it picks up the new defaults).

Tests

Added to tests/inference/test_inference_api.py (16 new cases, all with the HTTP layer mocked — no real requests):

  • retry policy: 429 → 200 succeeds after one retry; permanent 400 is not retried; retries are bounded by max_retries; connection errors are retried; _retry_delay prefers Retry-After, doubles otherwise and is capped
  • _post_chat_completion_async raises _HTTPStatusError carrying the parsed Retry-After
  • end-to-end: content: null → ""; permanent failure with raise_on_error=False → empty completion and the run completes; raise_on_error=True propagates; a single 500 is retried and the run succeeds; choices: [] is treated as a failure; invalid max_concurrency / max_retries raise; max_concurrency=1 serialises requests

Each behaviour was verified by reverting the corresponding hunk and confirming the new tests fail (1 / 4 / 2 failures respectively).

Checklist

  • ruff check clean on both changed files
  • pytest tests/inference/test_inference_api.py — 31 passed (15 pre-existing + 16 new)
  • pytest tests/inference/test_inference_function.py tests/utils/test_{evaluation_utils,regex_extractor,rich_utils,utils}.py tests/test_init.py — 148 passed, i.e. the same 2 pre-existing failures as on main (test_rate_limiter_waits, test_lazy_import_inference_transformers), no new ones
  • Full suite (pdm run pytest): not runnable here — tests/inference/test_inference_transformers_*, test_limit_argument and the vLLM suites need torch / transformers / vllm (multi-GB, GPU), which this machine does not have. The touched code path is independent of them.
  • mypy: not run locally (not installed); the new code is annotated consistently with the module.

…letions

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 LLMSQL#123
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
llmsql/inference/inference_api.py 93.33% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Inference] inference_api aborts the whole run on one transient HTTP error and writes null completions

1 participant