fix(openapi): decode error bodies with the declared charset - #7210
Open
1aifanatic wants to merge 1 commit into
Open
1aifanatic wants to merge 1 commit into
1aifanatic wants to merge 1 commit into
Conversation
RestApiTool built the error message for a failed call with
response.content.decode("utf-8"). A non-UTF-8 error body (a Latin-1
page from a legacy server, a binary body) raised UnicodeDecodeError
inside the HTTPStatusError handler, where the sibling ValueError
handler cannot catch it, and ended the whole agent run instead of
reporting the HTTP error to the model.
Use response.text, as the non-JSON success path already does: it
honors the declared charset and replaces undecodable bytes.
Fixes google#7206
Claude-Session: https://claude.ai/code/session_013vXxD1ga1hnCq2uFRwNks7
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.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
On a failed call,
RestApiTool.call()built the model-facing error withresponse.content.decode("utf-8"). A non-UTF-8 error body, such as a Latin-1 page from a legacy server or a binary body, raisedUnicodeDecodeErrorinside theexcept httpx.HTTPStatusErrorhandler, where the siblingexcept ValueErrorcan't catch it. The whole agent run then ended instead of the model getting the HTTP error.Solution:
Use
response.text, which is what the non-JSON success path a few lines below already does. It decodes with the response's declared charset and replaces undecodable bytes instead of raising.Testing Plan
Unit Tests:
I have added or updated unit tests for my change.
All unit tests pass locally.
New
test_call_http_failure_decodes_body_with_declared_charset: a realhttpx.Response(404)with acharset=iso-8859-1body is reported asStatus Code: 404, Commande introuvable : échec. Fails onmainwithUnicodeDecodeError.test_call_http_failurenow sets the mock's.textinstead of.content, because that's the attribute the tool reads.Every failure also fails on an unmodified
origin/main(3f4bb8f) on the same machine, where the same run gives 47 failed. Most are deterministic Windows-specific tests (cli/deploy, conformance,scripts/check_new_py_files, path normalization, the unsafe local code executor, the import allowlist, yaml). The rest are skill-telemetry schema cases intelemetry/test_functional.pythat fail intermittently on cleanmainhere: rerun 3 times each, they failed 1, 1, 1 times onmainand 1, 1, 3 times on this branch. I ran on Python 3.12 only, not the fulltoxmatrix.Manual End-to-End (E2E) Tests:
A real
Runner+LlmAgent+OpenAPIToolsetwith a mocked model and anhttpx.MockTransportbackend that answers404with a Latin-1 body. The script is in #7206.Before (
main@ 3f4bb8f):After:
Checklist
🤖 Generated with Claude Code
https://claude.ai/code/session_013vXxD1ga1hnCq2uFRwNks7