Skip to content

Errors: content-length cap on error body drops the typed-exception class #252

Closed

Description

@TexasCoding

Summary

A 429 (or 401, 409, 422, 504) whose body advertises >16KB returns a generic KalshiError from _map_error. Two concrete consequences:

  1. The retry loop's isinstance(error, KalshiRateLimitError) Retry-After branch is bypassed, so a hostile-but-otherwise-valid 429 with a verbose body falls back to computed backoff and ignores the server's Retry-After hint.
  2. Caller code structured as except KalshiRateLimitError / except KalshiAuthError / except KalshiConflictError silently misses these responses.

The size cap (#203) is correct in principle; only the exception class is wrong.

Location

  • kalshi/_base_client.py:51-62

Evidence

content_length = response.headers.get("content-length")
if content_length:
    try:
        if int(content_length) > MAX_ERROR_BODY_BYTES:
            return KalshiError(
                message=f"HTTP {status} (body {content_length} bytes, suppressed)",
                status_code=status,
            )
    except ValueError:
        pass

Recommended fix

After the size-check short-circuit, still dispatch on status to the type-specific exception with the suppressed-body message. Easiest: build message = f"HTTP {status} (body {content_length} bytes, suppressed)", set body = {}, then fall through to the existing status dispatch.

Severity & category

medium / consistency

Activity

  1. added 2 commits that reference this issue on May 21, 2026
    9d96785
    a93bf9b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions