Skip to content

Fix UnicodeDecodeError in DefaultResponseHandler when response content is binary#68495

Merged
eladkal merged 3 commits into
apache:mainfrom
davidnzhang:fix/msgraph-default-response-handler-unicode-responses
Jul 1, 2026
Merged

Fix UnicodeDecodeError in DefaultResponseHandler when response content is binary#68495
eladkal merged 3 commits into
apache:mainfrom
davidnzhang:fix/msgraph-default-response-handler-unicode-responses

Conversation

@davidnzhang

Copy link
Copy Markdown
Contributor

What

DefaultResponseHandler.get_value suppresses JSONDecodeError when response.json() fails and falls back to response.content or headers. This PR extends the suppression to also include UnicodeDecodeError when the response body contains invalid bytes.

Note: This seems to be specific to the httpx transport used by this response handler, which calls CPython's json.loads() without any additional exception handling. The json.loads() function can raise both JSONDecodeError and `UnicodeDecodeError'.

Why

Hooks that extend the KiotaRequestAdapterHook may call API endpoints that return binary responses. One such use case is to allow users to extend the PowerBIHook to support exporting reports to PDF. In these cases, response.json() raises a UnicodeDecodeError rather than a JSONDecodeError, causing the request to fail before reaching the response.content fall through path. Suppressing UnicodeDecodeError alongside JSONDecodeError allows these binary responses to be handled correctly and returned as bytes.


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

Generated-by: Claude Sonnet 4.6 following the guidelines


  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@boring-cyborg

boring-cyborg Bot commented Jun 13, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@davidnzhang
davidnzhang marked this pull request as ready for review June 13, 2026 04:48
@davidnzhang
davidnzhang requested a review from dabla as a code owner June 13, 2026 04:48
@davidnzhang
davidnzhang force-pushed the fix/msgraph-default-response-handler-unicode-responses branch 2 times, most recently from 0302f44 to b4ea670 Compare June 16, 2026 05:34
@eladkal
eladkal requested a review from shahar1 June 16, 2026 16:02
@davidnzhang
davidnzhang force-pushed the fix/msgraph-default-response-handler-unicode-responses branch from b4ea670 to 8481bd8 Compare June 17, 2026 10:12
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Jun 22, 2026
@davidnzhang
davidnzhang force-pushed the fix/msgraph-default-response-handler-unicode-responses branch from dedc34b to f51a35e Compare June 23, 2026 00:12
@dabla

dabla commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@davidnzhang Did you try passing response_type="bytes" to the MSGraphAsyncOperator to see if you still have above issue?

@davidnzhang

Copy link
Copy Markdown
Contributor Author

@dabla Thanks for suggesting this - I just tested passing response_type="bytes" into the run method of the hook and can confirm that this works, as it completely bypasses the route where DefaultResponseHandler gets involved.

That said, I'd argue the case for this change still stands, as the DefaultResponseHandler docstring already states it "returns JSON payload or content in bytes or response headers". Currently it fails to reach the bytes return path altogether for non-decodable binary responses, as UnicodeDecodeError is not caught by the suppress block.

@dabla

dabla commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

@dabla Thanks for suggesting this - I just tested passing response_type="bytes" into the run method of the hook and can confirm that this works, as it completely bypasses the route where DefaultResponseHandler gets involved.

That said, I'd argue the case for this change still stands, as the DefaultResponseHandler docstring already states it "returns JSON payload or content in bytes or response headers". Currently it fails to reach the bytes return path altogether for non-decodable binary responses, as UnicodeDecodeError is not caught by the suppress block.

Thx for testing this. Ok, once CI is green, we can merge this.

@davidnzhang
davidnzhang force-pushed the fix/msgraph-default-response-handler-unicode-responses branch 3 times, most recently from 7c033d3 to 557466d Compare June 24, 2026 23:18
@davidnzhang

davidnzhang commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Thx for testing this. Ok, once CI is green, we can merge this.

thanks @dabla, are there any pending actions on me at this stage?

@davidnzhang
davidnzhang force-pushed the fix/msgraph-default-response-handler-unicode-responses branch from 557466d to 0feb2da Compare June 26, 2026 00:50
@davidnzhang

Copy link
Copy Markdown
Contributor Author

@dabla CI is green on the latest commit, happy for this to go in whenever you have a moment - thanks again for the review

@eladkal
eladkal merged commit 5d4453d into apache:main Jul 1, 2026
81 checks passed
@boring-cyborg

boring-cyborg Bot commented Jul 1, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:microsoft-azure Azure-related issues ready for maintainer review Set after triaging when all criteria pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants