Optionally raise instead of swallowing JSON decode errors - #1027
Open
jakobovski-arb wants to merge 1 commit into
Open
Optionally raise instead of swallowing JSON decode errors#1027jakobovski-arb wants to merge 1 commit into
jakobovski-arb wants to merge 1 commit into
Conversation
_get and _paginate_iter decode every response inside `except ValueError: logger.error(...); return []`, so a truncated or malformed body becomes an empty result that a caller cannot distinguish from "the API had nothing". In _paginate_iter it is worse: the bare return ends iteration early, so a partial page silently truncates a result set that still looks complete. RESTClient(raise_on_decode_error=True) raises ResponseDecodeError instead. The default is unchanged, so this is not a breaking change.
jakobovski-arb
requested review from
jbonzo,
justinpolygon,
kschoche,
lukeoleson,
mmoghaddam385 and
suever
as code owners
August 4, 2026 13:12
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.
The problem
_getand_paginate_iterboth decode responses like this:A truncated or malformed body therefore becomes an empty result, which a caller cannot distinguish from "the API had nothing to return". The only trace is a log line, and the call itself succeeds.
In
_paginate_iterthis is worse than an empty result: the barereturnends the generator early, so a partial page silently truncates a result set that still looks complete to the caller.This is not hypothetical. In a single multi-hour backfill over
/v3/trades, we logged 12 of these:Each one produced an empty page that our pipeline recorded as a real absence — "this ticker had no auction print that day" — and cached permanently, because an empty response from a working API is a legitimate answer in our domain. We only found out by reading the log afterwards. For data ordered newest-first, where the row of interest sits on the last page, a truncated early page can drop exactly the record you were querying for.
The change
RESTClient(raise_on_decode_error=True)raises a newResponseDecodeErrorfrom both decode sites instead of returning[]. The flag threads toVXClient; otherBaseClientsubclasses inherit it throughRESTClient'ssuper().__init__().Default is
False, so existing behaviour is unchanged and this is not a breaking change.Caveats, in the interest of full disclosure
_paginate_iterhalf is the more important one, and it is arguably a bug rather than a design choice: silently returning a truncated result set is different in kind from returning an empty one, and no flag setting makes a partially-consumed generator obvious to the caller. If you'd rather fix only that site, or always raise there while keeping_getlenient, that would still address the dangerous case.urllib3retries a failed connection, rather than pushing that on every caller. We didn't include it here to keep this diff to one concern.raise_on_decode_error/ResponseDecodeErrorseemed closest to the existingBadResponse, but rename freely.test_rest/models/test_requests.py::RequestTest::test_clint_headers_concatalready fails on a cleanmastercheckout in our environment (verified by stashing this change), so it is unrelated to this PR. The other 47 tests pass, including the 4 added here.Tests
test_rest/test_decode_errors.pycovers: the default still returns an empty result, the opt-in raisesResponseDecodeError, the flag reachesclient.vx, and a well-formed body is unaffected either way.Context: we're the authors of #1026 (
maxsizeon the connection pool). Both issues surfaced from the same workload — a high-concurrency historical backfill of/v3/trades— where silent data loss is expensive and hard to detect after the fact.🤖 Generated with Claude Code