feat: configurable response JSON decoder

Open
#965 5 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Active
Tech stack
python
Domain
api, backend

Research direction

Start by reading build_request(), Request.extensions, response.json(), and the response send handling described in the issue. Review related issue #259 and the open questions about API levels, naming, propagation, and the decoder contract. Done means the project agrees on the design and the proposed client- and request-level semantics are specified well enough to implement.

Written by the indexing model from the issue text.

Description

Configurable response JSON decoder

While working on #945, it became clear that request-side JSON serialization and response-side JSON decoding may not belong to the same API surface.

This discussion focuses only on configurable response JSON decoding.

Motivation

The original issue was primarily about customizing how response JSON is decoded, for example using orjson.loads instead of the standard library decoder.

Today response.json() is effectively tied to the stdlib implementation, which makes it difficult to:

  • use alternative JSON libraries globally
  • optimize JSON decoding performance
  • customize decoding behavior consistently across a client

Proposed scope

Only response-side decoding.

No request-side JSON serialization customization.

Proposed API shape

Client-level decoder:

client = httpx.Client(
    json_deserializer=orjson.loads,
)

Per-request override:

response = client.get(
    "https://example.org",
    json_deserializer=custom_decoder,
)

Explicit disable:

response = client.get(
    "https://example.org",
    json_deserializer=None,
)

Proposed semantics

  • request-level decoder overrides client-level decoder
  • None explicitly disables the custom decoder
  • manually-created Request(...) objects without decoder configuration fall back to the client-level decoder
  • redirect requests preserve decoder configuration

Internal design direction

Current direction is:

  • keep all config merging in build_request()
  • propagate decoder configuration through Request.extensions
  • configure the Response object during send handling
  • avoid introducing new abstractions or generalized serialization hooks

Example internal extension key:

"httpx2.json_deserializer"

Type contract

Current proposal:

JsonDeserializer = typing.Callable[[bytes], typing.Any]

The intention is to keep the contract strict and compatible with libraries like orjson.

Open questions

  1. Does the proposed API shape make sense?
  2. Should the decoder be configurable at the client level, request level, or both?
  3. Is json_deserializer the right naming?
  4. Is Request.extensions an acceptable propagation mechanism for this configuration?
  5. Should the decoder contract accept only bytes, or str | bytes?

I’d appreciate feedback before continuing the implementation work in the PR.

See also issue #259 for related discussion.

Dominant language
Python
Stars
1.6k
Forks
103
Avg merge
1d 19h
Merged PRs (30d)
25

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/httpx2

All issues in pydantic/httpx2

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.