redis/redis-py · error · ValueError

Provide either json_body or data, not both.

Error message

Provide either json_body or data, not both.

What it means

Raised as ValueError by HttpClient._prepare_body (redis/http/http_client.py:356) when both json_body and data are supplied to the same request. The two arguments produce the request body in mutually exclusive ways (json_body is JSON-serialized, data is sent verbatim), so providing both is ambiguous and rejected immediately rather than picking one silently.

Source

Thrown at redis/http/http_client.py:356

        resp = self.request(
            method=method,
            path=path,
            params=params,
            headers=headers,
            body=body,
            timeout=timeout,
        )
        if not (200 <= resp.status < 400):
            raise HttpError(resp.status, resp.url, resp.text())
        if expect_json:
            return resp.json()
        return resp

    def _prepare_body(
        self, json_body: Optional[Any] = None, data: Optional[Union[bytes, str]] = None
    ) -> Optional[Union[bytes, str]]:
        if json_body is not None and data is not None:
            raise ValueError("Provide either json_body or data, not both.")
        if json_body is not None:
            return json.dumps(json_body, ensure_ascii=False, separators=(",", ":"))
        return data

    def _build_url(
        self,
        path: str,
        params: Optional[
            Mapping[str, Union[None, str, int, float, bool, list, tuple]]
        ] = None,
    ) -> str:
        url = urljoin(self.base_url or "", path)
        if params:
            # urlencode with doseq=True supports list/tuple values
            query = urlencode(
                {k: v for k, v in params.items() if v is not None}, doseq=True
            )
            separator = "&" if ("?" in url) else "?"

View on GitHub (pinned to da03cdc7e8)

Solutions

  1. Pass only one of json_body or data per request.
  2. If you have pre-serialized JSON, pass it as data and drop json_body.
  3. If you have a Python object, pass json_body and drop data.
  4. Audit call sites after migrating between the two body styles.

Example fix

// before
client.post('/items', json_body={'k': 1}, data=b'already-serialized')

// after
client.post('/items', json_body={'k': 1})
Defensive patterns

Strategy: validation

Validate before calling

if json_body is not None and data is not None:
    raise ValueError('pass json_body OR data, not both')
client.post('/items', json_body=json_body, data=data)

Prevention

When it happens

Trigger: Calling client.post/put/patch(..., json_body=obj, data=raw) on the HttpClient with both arguments non-None.

Common situations: Refactoring a call from raw bytes to JSON and leaving the old data= argument in place; passing a pre-serialized JSON string as data alongside json_body; copy-paste between endpoints.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/586bea9a7db4362d.json. Report an issue: GitHub.