fix: handle non-string request bodies in error-logging round-trip dump - #1497
Open
mittalpk wants to merge 1 commit into
Open
fix: handle non-string request bodies in error-logging round-trip dump#1497mittalpk wants to merge 1 commit into
mittalpk wants to merge 1 commit into
Conversation
RoundTrip.generate() passed request.body directly to _redacted_dump(), which assumes a str. For a streamed upload (a file-like object passed as data=), body is left as the original file object by requests, and _redacted_dump's len(body) call raised TypeError: object of type '...' has no len(). For a non-UTF8 bytes body, the JSONDecodeError fallback path called body.encode() on already-decoded bytes, raising AttributeError: 'bytes' object has no attribute 'encode'. Both crashes happened inside the "unable to parse response" debug-log path itself, masking the actual API error behind an unrelated Python traceback. Added _request_body_str() to normalize body to a string before it reaches _redacted_dump: str passes through, bytes/bytearray decode with errors="replace", and any other (stream/file-like) object is represented with a fixed "[stream body]" placeholder rather than read, since reading it here would consume the stream (which may already be at an unknown position after the actual send) with no guarantee its content is UTF-8 text. Fixes databricks#1489 Signed-off-by: Praveen Mittal <pkmittal28@gmail.com>
|
If integration tests don't run automatically, an authorized user can run them manually by following the instructions below: Trigger: Inputs:
Checks will be approved automatically on success. |
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.
Summary
RoundTrip.generate()(databricks/sdk/logger/round_trip_logger.py) passesrequest.bodydirectly to_redacted_dump(), which assumes astr. This crashes in two ways, both inside the "unable to parse response" debug-log path itself — so the crash masks the actual API error behind an unrelated Python traceback:data=torequests, e.g.dbutils.files.uploadfrom a local file):requestsleavesrequest.bodyas the original file object rather than reading it._redacted_dump'slen(body)call then raisesTypeError: object of type '...' has no len().body.encode(...)on an already-bytesvalue, raisingAttributeError: 'bytes' object has no attribute 'encode'.Fixes #1489 (the streamed-upload crash); the non-UTF8-bytes crash is the same root cause (an assumption that
request.bodyis alwaysstr) so it's fixed and tested alongside it rather than left for a follow-up.Fix
Added
_request_body_str()to normalizerequest.bodyinto a string before it reaches_redacted_dump:strpasses through unchanged.bytes/bytearraydecode witherrors="replace"(matching the existing pattern already used forresponse.contenta few lines below)."[stream body]"placeholder rather than read — reading it here would consume the stream (which may already be at an unknown position after the actual send) with no guarantee its content is UTF-8 text.Test plan
tests/test_errors.py::test_get_api_errorcovering both crash scenarios (streamed body viaio.BytesIO, non-UTF8bytesbody), extending the existingfake_raw_responsehelper with an optionalrequest_bodyparameter that's passed through torequests.Request(..., data=...).prepare()— this reproduces exactly howrequestsrepresents each body type in production, rather than hand-constructing a fake object.git stashthe fix inround_trip_logger.py, rerun — one raisesTypeError, the otherAttributeError, matching the two reported/found crash shapes) and pass post-fix.pytest tests/test_errors.py: 52 passed.pytest tests/(excludingtests/integration): 2109 passed; the only failures (test_open_ai_mixin.py, 7 tests) are pre-existing in this environment due to the optionalopenai/langchainpackages not being installed, unrelated to this change — confirmed by running that file alone and seeing the sameModuleNotFoundErroron unmodifiedmain.ruff check/ruff format --check: clean on both changed files.