Skip to content

perf(postgrest): speed up JSON parsing by 22x using json.loads - #1571

Open
Samearth17 wants to merge 1 commit into
supabase:mainfrom
Samearth17:perf/speed-up-postgrest-json
Open

perf(postgrest): speed up JSON parsing by 22x using json.loads#1571
Samearth17 wants to merge 1 commit into
supabase:mainfrom
Samearth17:perf/speed-up-postgrest-json

Conversation

@Samearth17

Copy link
Copy Markdown

Summary

Resolves #1486 by replacing the slow JSONAdapter.validate_json call in base_request_builder.py with the standard library json.loads.

Context

The current JSONAdapter (a Pydantic TypeAdapter over the JSON type alias) performs redundant runtime validation. Because standard JSON parsing already guarantees the output matches the Union[None, bool, str, int, float, Sequence[JSON], Mapping[str, JSON]] schema, we can safely skip the Pydantic validation pass entirely.

This matches the exact, proven pattern already used successfully by SingleAPIResponse.from_http_request_response in the exact same file.

Benchmarks

As reported in #1486 and verified by community members:

  • Before (JSONAdapter): ~43ms
  • After (json.loads): ~1.9ms
    This represents a ~22x speedup for large payloads, requires zero new dependencies, and maintains exact backward compatibility.

Changes

  1. Removed ValidationError from Pydantic imports as it is no longer used.
  2. Removed unused JSONAdapter import.
  3. Switched APIResponse.from_http_request_response to use json.loads + JSONDecodeError fallback.

@Samearth17
Samearth17 requested review from a team and o-santi as code owners August 11, 2026 18:21
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.

postgrest.types.JSONAdapter is much slower than TypeAdapter(JsonValue)

1 participant