Skip to content

Fix ValueError when parsing malformed rate limit header - #1

Open
IFAKA wants to merge 1 commit into
romanmichaelpaolucci:mainfrom
IFAKA:fix/rate-limit-header-parsing
Open

Fix ValueError when parsing malformed rate limit header#1
IFAKA wants to merge 1 commit into
romanmichaelpaolucci:mainfrom
IFAKA:fix/rate-limit-header-parsing

Conversation

@IFAKA

@IFAKA IFAKA commented Dec 19, 2025

Copy link
Copy Markdown

Problem

When the API returns a 429 status with a malformed X-RateLimit-Reset header (e.g., empty string, float, or non-numeric value), the SDK crashes with an unhandled ValueError:

# client.py:156
retry_after=int(retry_after) if retry_after else None
# If retry_after is "invalid" or "12.5", int() raises ValueError

Fix

Wrap the integer conversion in a try/except block to gracefully handle malformed headers:

if retry_after_header:
    try:
        retry_after = int(retry_after_header)
    except (ValueError, TypeError):
        pass  # Keep retry_after as None

Test

Test 1: Malformed header 'invalid'
  ✓ RateLimitError raised correctly
  ✓ retry_after=None (None expected)

Test 2: Valid header '60'
  ✓ RateLimitError raised correctly
  ✓ retry_after=60 (60 expected)

Test 3: Float header '12.5'
  ✓ RateLimitError raised correctly
  ✓ retry_after=None (None expected)

Test 4: Empty header ''
  ✓ RateLimitError raised correctly
  ✓ retry_after=None (None expected)

Test 5: Missing header
  ✓ RateLimitError raised correctly
  ✓ retry_after=None (None expected)

==================================================
All tests passed!

Handle edge case where X-RateLimit-Reset header contains
non-integer value by catching ValueError/TypeError.
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.

1 participant