Skip to content

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

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

agustin18 wants to merge 1 commit into
slackapi:mainfrom
agustin18:fix/web-malformed-json-body

Conversation

@agustin18

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.

The parse now leaves parsed_body unset when the body is malformed. That is the same state a body not starting with { produces, so the existing fallback builds the Received a response in a non-JSON format error.

_request_for_pagination has the same unguarded json.loads on the response body, independent of retry handlers. I left it out of this change; happy to handle it here or in a follow-up.

Testing

  • New test in tests/slack_sdk/web/test_web_client.py: test_non_json_response_body_starting_with_brace_raises_api_error. It fails on main with json.decoder.JSONDecodeError: Expecting value: line 1 column 8 (char 7) and passes with the change.
  • python -m pytest tests/ -> 1082 passed, 5 skipped, 45 subtests passed.
  • 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.

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

2 participants