Skip to content

perf(postgrest): use JsonValue for JSON adapter - #1666

Closed
mdmoazzamg wants to merge 1 commit into
supabase:mainfrom
mdmoazzamg:perf/postgrest-json-adapter
Closed

mdmoazzamg wants to merge 1 commit into
supabase:mainfrom
mdmoazzamg:perf/postgrest-json-adapter

Conversation

@mdmoazzamg

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix, feature, docs update, ...

What is the current behavior?

Please link any relevant issues here.

What is the new behavior?

Feel free to include screenshots if it includes visual changes.

Additional context

Add any other context or screenshots.

@mdmoazzamg
mdmoazzamg requested review from a team and o-santi as code owners September 24, 2026 11:36
@mdmoazzamg

Copy link
Copy Markdown
Author

Description

Use Pydantic's built-in JsonValue type for the PostgREST JSONAdapter.

The previous implementation used a recursive custom JSON type with TypeAdapter(JSON). This change switches the adapter to TypeAdapter(JsonValue) while keeping the existing JSON type alias unchanged.

Motivation

TypeAdapter(JsonValue) showed significantly better validation performance in local benchmarking compared with the recursive JSON adapter.

In the local benchmark, JsonValue was approximately 8x faster for the tested payload.

Changes

  • Import JsonValue from Pydantic.
  • Change JSONAdapter to use TypeAdapter(JsonValue).
  • Keep the existing JSON type alias unchanged.

Testing

  • uv run pytest src/postgrest/tests/_async/test_request_builder.py src/postgrest/tests/_sync/test_request_builder.py
  • Result: 90 passed

Also verified:

  • git diff --check passes.
  • JSON validation behavior remains compatible with the previous adapter.

Closes #1486

@spydon

spydon commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Superseded by #1902

@spydon spydon closed this Nov 2, 2026
spydon added a commit that referenced this pull request Nov 2, 2026
#1902)

## What

Removes the pydantic v1 compatibility layer from `supabase_auth` and
`postgrest`, and aligns their pydantic floor with storage and realtime
at `>=2.11.7`, so every package in the repo has the same minimum.
Resolves SDK-1694.

Both packages already failed to import on pydantic 1.x, because they use
`TypeAdapter` at module scope, so the v1 fallbacks were dead code and
the declared ranges were wrong.

### postgrest (`pydantic >=1.9,<3.0` to `pydantic >=2.11.7,<3`)

- Removed the `model_validate_json` fallback from `utils.py`. Call sites
now call `APIErrorFromJSON.model_validate_json(...)` directly.
- Removed `JSONAdapter` from `types.py`. Nothing has used it since #1571
and it was never exported. This supersedes #1666.

### auth (`pydantic >=1.10,<3` to `pydantic >=2.11.7,<3`)

- Removed `model_validate`, `model_dump` and `model_dump_json` from
`helpers.py`. Call sites now use the v2 model methods directly.
- Replaced the `model_validator`/`root_validator` fallback in `types.py`
with `@model_validator(mode="before")` and `@classmethod`.
- Deleted the three tests that only exercised the v1 fallbacks.
- Dropped the stale `with_config` comment about older pydantic versions.

## Why 2.11.7 for both

`supabase` pins all five packages to the same release, and storage and
realtime already require `>=2.11.7`, so that is already the effective
floor for anyone installing `supabase`. A single floor also matches what
CI tests, since CI runs against the locked version.

For reference, the code alone would work on lower versions. Checked
locally on Python 3.9: auth needs 2.7 (for the top-level `with_config`
import) and postgrest works on 2.0.

`uv.lock` only changes in the two `requires-dist` specifiers; resolved
versions are unchanged.

## Testing

- mypy passes for both packages.
- `_sync/` was regenerated with `build-sync` and mirrors the `_async/`
changes exactly.
- Integration suites are left to CI.
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.

2 participants