GeezerVison is showing github.com — fix(web): raise SlackApiError instead of JSONDecodeError on a malformed body by agustin18 · Pull Request #2072 · slackapi/python-slack-sdk
Conversation
agustin18
commented
Nov 19, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
Fixes #2070.
A sync WebClient parses any 200 response body that starts with { inside the retry loop in _perform_urllib_http_request, so the retry handlers receive a parsed body. That json.loads had no guard: a truncated body raised JSONDecodeError from the loop before _urllib_api_call could run its non-JSON fallback, so callers got a decode error instead of SlackApiError.
_request_for_pagination had its own unguarded json.loads, and raised the same way even with no retry handlers set.
Both sync paths now go through _parse_response_body, which returns the existing {"ok": False, "error": ...} payload when the body is not JSON. _urllib_api_call and SlackResponse.validate() then raise SlackApiError with Received a response in a non-JSON format, so callers see the same raised type on both paths.
Per the discussion with @mwbrooks and @WilliamBergamin, this is expected to wait for the 4.x release because JSONDecodeError is a ValueError and SlackApiError is not.
Testing
test_non_json_response_body_starting_with_brace_raises_api_error: fails on main with json.decoder.JSONDecodeError: Expecting value: line 1 column 8 (char 7) and passes here.
test_non_json_response_body_during_pagination_raises_api_error: same body on the second page; fails on main with JSONDecodeError and raises SlackApiError here.
python -m pytest tests/ -> 1082 passed, 5 skipped, 45 subtests passed. One run of tests/slack_sdk/oauth/state_store/test_sqlalchemy.py::test_expiration failed once and passed on the next two runs; it is time-based and unrelated to these files.
python scripts/codegen.py --path . produces no changes; base_client.py is not a generated file.
./scripts/format.sh, ./scripts/lint.sh and ./scripts/run_mypy.sh pass, mypy clean on 107 source files.
Category
- slack_sdk.web.WebClient (sync/async) (Web API client)
- slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
- slack_sdk.socket_mode (Socket Mode client)
- slack_sdk.signature (Request Signature Verifier)
- slack_sdk.oauth (OAuth Flow Utilities)
- slack_sdk.models (UI component builders)
- slack_sdk.scim (SCIM API client)
- slack_sdk.audit_logs (Audit Logs API client)
- slack_sdk.rtm_v2 (RTM client)
-
/docs (Documents)
-
/tutorial (PythOnBoardingBot tutorial)
-
tests/integration_tests (Automated tests for this library)
Requirements
- I've read and understood the Contributing Guidelines and have done my best effort to follow them.
- I've read and agree to the Code of Conduct.
- I have signed the Salesforce CLA (required before your PR can be reviewed or merged).
- I've run
python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ed body
The retry-handler branch parses any 200 response body that starts with
"{" to build its RetryHttpResponse. A truncated or otherwise malformed
body raised JSONDecodeError from json.loads before the caller could run
its non-JSON fallback, so sync WebClient calls with the default retry
handlers surfaced a decode error instead of SlackApiError.
Leave parsed_body unset when the parse fails, which restores the
existing fallback in _urllib_api_call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @agustin18, the fix is exactly what #2070 proposed! 🙇
Changing the raised type from JSONDecodeError to SlackApiError can break callers who catch ValueError today, so as-is this would wait for the 4.x release.
@WilliamBergamin do you have an alternative suggestion on how we could merge this without introducing a breaking change?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
try:
|
|
|
parsed_body = json.loads(body_string)
|
|
|
resp["parsed_body"] = parsed_body
|
|
|
except json.decoder.JSONDecodeError:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (non-blocking): Could we guard the json.loads in _request_for_pagination in this PR too? #2070 has this as a 4.x breaking change, so fixing both sync paths together means callers see the raised type change once instead of across two releases.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
def test_non_json_response_body_starting_with_brace_raises_api_error(self):
|
|
|
# The retry-handler branch parses any 200 body that starts with "{", so a
|
|
|
# truncated body must not escape from it as a JSONDecodeError.
|
|
|
response = {"status": 200, "headers": {}, "body": '{"ok": tru'}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: Thanks for reusing the exact repro from #2070, so this fails on main for the reason the issue describes.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.72%. Comparing base (4482fe7) to head (82e7728).
✅ All tests successful. No failed tests found.
Additional details and impacted files
@@ Coverage Diff @@
## main #2072 +/- ##
=======================================
Coverage 84.71% 84.72%
=======================================
Files 118 118
Lines 14122 14125 +3
=======================================
+ Hits 11964 11967 +3
Misses 2158 2158
☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@mwbrooks I looked into this yesterday and figured it would be better to just keep the issue open until the next major release 😅
Reverting back to the previous behavior might be a bug fix more then an actual breaking change, I opened the issue mainly to figure this out before we open a PR for it
|
mwbrooks reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
An alternative I though of was to create a new error class that extends SlackApiError and JSONDecodeError and throw that instead but it feels like a code smell and adds complexity
|
mwbrooks reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks @WilliamBergamin! Agreed, let's skip the dual error class and keep this open for the 4.x release. 🙇
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
_request_for_pagination parsed the body with its own unguarded json.loads,
so a malformed body raised JSONDecodeError there as well, even without
retry handlers set.
Both sync paths now share _parse_response_body, which returns the error
payload instead of raising. Pagination then fails through
SlackResponse.validate, so callers see SlackApiError in both places.
This branch has not been deployed
No deployments
Reviewers
At least 1 approving review is required to merge this pull request.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|