Skip to content

fix(web): raise SlackApiError instead of JSONDecodeError on a malformed body - #2072

Open
agustin18 wants to merge 2 commits into
slackapi:mainfrom
agustin18:fix/web-malformed-json-body
Open

agustin18 wants to merge 2 commits into
slackapi:mainfrom
agustin18:fix/web-malformed-json-body

Conversation

@agustin18

@agustin18 agustin18 commented Nov 19, 2026 •

Copy link
Copy Markdown

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.

…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.
@agustin18
agustin18 requested a review from a team as a code owner November 19, 2026 23:02
@mwbrooks mwbrooks added bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented semver:major web-client labels Nov 19, 2026
@mwbrooks mwbrooks added this to the 4.x milestone Nov 19, 2026

@mwbrooks mwbrooks left a comment

Copy link
Copy Markdown
Member

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?

try:
parsed_body = json.loads(body_string)
resp["parsed_body"] = parsed_body
except json.decoder.JSONDecodeError:

Copy link
Copy Markdown
Member

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.

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'}

Copy link
Copy Markdown
Member

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.

@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ 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.

@WilliamBergamin

Copy link
Copy Markdown
Contributor

@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

@WilliamBergamin

WilliamBergamin commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Member

Thanks @WilliamBergamin! Agreed, let's skip the dual error class and keep this open for the 4.x release. 🙇

_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug M-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documented cla:signed semver:major web-client

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WebClient raises JSONDecodeError instead of SlackApiError on a malformed JSON body when retry handlers are set

3 participants