Skip to content

fix(requests): fall back to stdlib json in Response.json() when kwargs are passed (#639) - #796

Open
apoorvdarshan wants to merge 1 commit into
lexiforest:mainfrom
apoorvdarshan:fix/issue-639-json-orjson-kwargs
Open

fix(requests): fall back to stdlib json in Response.json() when kwargs are passed (#639)#796
apoorvdarshan wants to merge 1 commit into
lexiforest:mainfrom
apoorvdarshan:fix/issue-639-json-orjson-kwargs

Conversation

@apoorvdarshan

@apoorvdarshan apoorvdarshan commented Jul 9, 2026

Copy link
Copy Markdown

Fixes #639

Root cause

Response.json(**kw) forwards its keyword arguments straight to loads():

return loads(self.content, **kw)

In curl_cffi/requests/models.py, loads is bound to orjson.loads when orjson is installed and to json.loads otherwise. orjson.loads() accepts no keyword arguments, whereas the stdlib json.loads() does. A call such as response.json(parse_float=Decimal) therefore raises TypeError only when orjson is installed.

Fix

Keep the faster orjson.loads for the common no-kwargs case, but fall back to the stdlib json.loads whenever the caller passes keyword arguments. The no-kwargs fast path is unchanged.

Tests

Added test_json_kwargs_with_orjson, which verifies that r.json(parse_float=Decimal) returns a Decimal when orjson is installed.

  • The regression test fails on unmodified code with TypeError.
  • The regression test and surrounding JSON/redirect tests pass with this fix.
  • ruff check and ruff format --check are clean on the changed files.

Review checklist

  • I have manually reviewed the changes and fully understand the code.

Disclosure: prepared with AI assistance; manually reviewed and verified locally.

…s are passed

Response.json(**kw) forwards keyword arguments to loads(), but loads
resolves to orjson.loads when orjson is installed, and orjson.loads()
accepts no keyword arguments. This made calls like
response.json(parse_float=Decimal) raise TypeError when orjson is
present, while working fine when it is absent.

Use stdlib json.loads whenever the caller passes keyword arguments, and
keep the faster orjson.loads for the common no-kwargs case, so behavior
no longer depends on whether orjson happens to be installed.

Fixes lexiforest#639
@lexiforest lexiforest added this to the v0.17 milestone Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

orjson.loads does not take keyword args.

2 participants