fix: Retry-After-Ms header silently ignored due to str > int TypeError#766
Open
devteamaegis wants to merge 1 commit into
Open
fix: Retry-After-Ms header silently ignored due to str > int TypeError#766devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
`_parse_retry_after` retrieved the `Retry-After-Ms` response header and then compared the raw *string* value to an integer with `>`: ```python return int(retry_after_ms) / 1000 if retry_after_ms > 0 else 0 ``` In Python 3, comparing `str > int` raises `TypeError`, which was silently swallowed by the surrounding `except Exception: pass`. The practical effect is that every `Retry-After-Ms` header was completely ignored — the client always fell through to the `Retry-After` header or exponential back-off instead of honouring the server-supplied millisecond delay. Fix: convert to `int` first, then compare. ```python ms = int(retry_after_ms) return ms / 1000 if ms > 0 else 0 ``` Adds `tests/test_http_client.py` with unit tests that cover positive, zero, negative, fractional, invalid, and combined header scenarios.
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.
Bug
_parse_retry_afterinsrc/cohere/core/http_client.pyretrieves theRetry-After-Msheader and then compares the raw header string against an integer:In Python 3,
"1000" > 0raisesTypeError: '>' not supported between instances of 'str' and 'int'.That exception is caught and silently discarded, so the
Retry-After-Msheader is always ignored — every response that carries one falls through toRetry-Afteror exponential back-off instead.Fix
Convert the header value to
intbefore the comparison:Reproduction
Tests
Added
tests/test_http_client.pywith 10 unit tests covering positive values, zero, negative values, fractional milliseconds, invalid header content, header priority (Retry-After-Ms vs Retry-After), and the no-header case.Note
Low Risk
Low risk: narrow bugfix in retry-delay parsing plus unit tests; only affects how backoff delay is computed when
retry-after-msis present.Overview
Fixes
_parse_retry_afterto correctly handleretry-after-msby converting the header string to anintbefore comparing/clamping, preventing aTypeErrorthat previously caused the header to be silently ignored.Adds a new
tests/test_http_client.pysuite coveringretry-after-msandretry-afterparsing (valid/invalid values, priority, and no-header cases) to prevent regressions.Reviewed by Cursor Bugbot for commit 02c2a8f. Bugbot is set up for automated code reviews on this repo. Configure here.