fix(tools): decode RestApiTool HTTP error bodies with declared charset - #7207
Open
chelsealong wants to merge 1 commit into
Open
chelsealong wants to merge 1 commit into
chelsealong wants to merge 1 commit into
Conversation
response.content.decode("utf-8") raises UnicodeDecodeError for a
non-UTF-8 error body (e.g. a Latin-1 page from a legacy server), inside
the except httpx.HTTPStatusError handler where the sibling except
ValueError can't catch it, crashing the whole agent run. Use
response.text instead, matching the existing non-JSON success path a
few lines below, which decodes with the response's declared charset
and replaces undecodable bytes rather than raising.
Fixes google#7206
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):
Fixes #7206
Problem:
RestApiTool.call()builds the error message for the model inits
except httpx.HTTPStatusErrorhandler with:This forces UTF-8 regardless of the charset the server declares. A
non-UTF-8 error body (e.g. a Latin-1/Windows-1252 page from a legacy
ERP or IIS server) raises
UnicodeDecodeError. Because the exceptionis raised inside the
except httpx.HTTPStatusErrorhandler, thesibling
except ValueErrorcan't catch it, so it propagates out of thetool and ends the whole agent run — the model never gets the chance to
see and recover from the HTTP error.
Solution: Use
response.textinstead, matching the existingnon-JSON success path a few lines below (
except ValueError: ... response.text). httpx decodesresponse.textusing the response'sdeclared charset and replaces undecodable bytes instead of raising.
Testing Plan
Unit Tests:
Added
test_call_http_failure_non_utf8_body, which builds a realhttpx.Response(404,Content-Type: text/plain; charset=iso-8859-1,body
"Commande introuvable : échec"encoded as Latin-1) and assertsRestApiTool.call()returns the normal error dict instead of raising.Also updated the existing
test_call_http_failuremock to set.textalongside.content, since the code path now reads thatattribute.
Regression proof — reverting only the source fix
(
git checkout HEAD~1 -- src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py)and running the new test:
With the fix restored:
Also ran the repo's formatters/linters on the changed files (both clean):
AI assistance disclosure
This change was prepared with the assistance of Claude Code (Anthropic),
under human review before submission.