Repository navigation
fix(inference): retry transient API errors and stop writing null completions - #140
Open
ddaihao2022 wants to merge 1 commit into
Open
ddaihao2022 wants to merge 1 commit into
ddaihao2022 wants to merge 1 commit into
Conversation
…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 Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #123
What was broken
inference_api()dispatched every request concurrently with no error handling:_post_chat_completion_asynccalledresp.raise_for_status(); a single 429 / 5xx / timeout propagated out ofasyncio.as_completedand aborted everything. Sinceinference_apiwipes the output file on start, one blip cost an entire 80k-request run."content": nullwas written out as-is, soevaluate()later failed thecompletion must be strassertion for the whole predictions file (refusals, some reasoning models, tool-call responses).max_concurrency/raise_on_error, unlikeinference_function, which already had both.What this changes
_post_with_retries()retries HTTP 429 / 5xx, timeouts and connection resets with exponential backoff, honouringRetry-Afterwhen the server sends it. Controlled bymax_retries(default3),retry_base_delay(default1.0) andretry_max_delay(default30.0);max_retries=0disables retries. Permanent errors (400, 401, …) are raised immediately instead of burning attempts.resp.status) rather than only viaraise_for_status(), because aiohttp'sClientResponseErrordoes not carry the response headers — that is the only way to readRetry-After.raise_on_error(defaultFalse, likeinference_function): a request that still fails is logged and recorded as an empty completion, so the run finishes. Withraise_on_error=Truethe 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(default32, likeinference_function) bounds in-flight requests with anasyncio.Semaphore;Nonekeeps the previous unbounded behaviour.nullcontent is coerced to"", and an emptychoiceslist is treated as a failure instead of raisingIndexError.No behaviour change for successful responses:
_post_chat_completion_asynckeeps its signature and itsValueErrorfor a missingchoiceskey, 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):max_retries; connection errors are retried;_retry_delayprefersRetry-After, doubles otherwise and is capped_post_chat_completion_asyncraises_HTTPStatusErrorcarrying the parsedRetry-Aftercontent: null→""; permanent failure withraise_on_error=False→ empty completion and the run completes;raise_on_error=Truepropagates; a single 500 is retried and the run succeeds;choices: []is treated as a failure; invalidmax_concurrency/max_retriesraise;max_concurrency=1serialises requestsEach behaviour was verified by reverting the corresponding hunk and confirming the new tests fail (1 / 4 / 2 failures respectively).
Checklist
ruff checkclean on both changed filespytest 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 onmain(test_rate_limiter_waits,test_lazy_import_inference_transformers), no new onespdm run pytest): not runnable here —tests/inference/test_inference_transformers_*,test_limit_argumentand the vLLM suites needtorch/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.