Skip to content

Speed up postgrest JSON validation#1487

Open
snoopuppy582 wants to merge 2 commits into
supabase:mainfrom
snoopuppy582:fix-postgrest-jsonadapter-jsonvalue-1486
Open

Speed up postgrest JSON validation#1487
snoopuppy582 wants to merge 2 commits into
supabase:mainfrom
snoopuppy582:fix-postgrest-jsonadapter-jsonvalue-1486

Conversation

@snoopuppy582
Copy link
Copy Markdown

Fixes #1486

Summary

  • switch postgrest.types.JSONAdapter to Pydantic's optimized JsonValue schema
  • keep the public JSON type alias unchanged for request typing
  • add regression coverage that guards the JsonValue adapter path and nested JSON byte validation

Local validation

  • Before fix benchmark from the issue payload: JSONAdapter.validate_json(payload) averaged 44.78 ms; TypeAdapter(JsonValue) averaged 3.93 ms
  • After fix benchmark from the issue payload: JSONAdapter.validate_json(payload) averaged 3.56 ms; TypeAdapter(JsonValue) averaged 3.74 ms
  • uv run --project src/postgrest pytest src/postgrest/tests/test_types.py -q
  • uv run --project src/postgrest pytest src/postgrest/tests/test_types.py src/postgrest/tests/test_utils.py src/postgrest/tests/_sync/test_request_builder.py src/postgrest/tests/_async/test_request_builder.py -q
  • uv run --project src/postgrest ruff check src/postgrest/src/postgrest/types.py src/postgrest/tests/test_types.py

Notes

  • I tried the full src/postgrest/tests suite locally, but it did not finish before the 4-minute timeout in my Windows environment. The targeted request-builder/type tests above completed successfully.
  • uv run --locked currently reports that the existing workspace lockfile needs an update, so the PR intentionally does not include lockfile changes.

@snoopuppy582 snoopuppy582 requested review from a team and o-santi as code owners May 12, 2026 01:09
Copy link
Copy Markdown

@AmaLS367 AmaLS367 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR — the performance improvement is impressive and the change is clean. The core fix looks correct.

One suggestion on the test test_json_adapter_uses_pydantic_json_value_schema:

assert JSONAdapter.core_schema["schema"]["schema_ref"].startswith(
    "pydantic.types.JsonValue:"
)

The intent is good (guarding the fast path), but the nested ["schema"]["schema_ref"] path and the hardcoded "pydantic.types.JsonValue:" string tie the test to Pydantic's internal schema structure. A more resilient alternative would be to compare against a reference adapter:

def test_json_adapter_schema_matches_json_value():
    from pydantic.types import JsonValue as PydanticJsonValue
    from pydantic import TypeAdapter
    assert JSONAdapter.core_schema == TypeAdapter(PydanticJsonValue).core_schema

This guards the same regression (someone reverting to the slow TypeAliasType path) without hardcoding internal schema details. Just a suggestion though — not a blocker.

@snoopuppy582
Copy link
Copy Markdown
Author

Thanks for the suggestion. I updated the test to compare JSONAdapter.core_schema against TypeAdapter(JsonValue).core_schema instead of asserting the nested Pydantic schema ref string.

Verified locally:

  • uv run pytest tests/test_types.py -q -> 2 passed

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)

2 participants