Skip to content

fix: add flexible Json type annotation for Pydantic models (#1597) - #1599

Open
Sanjeev888 wants to merge 1 commit into
supabase:mainfrom
Sanjeev888:fix/json-deserialization-issue-1597
Open

fix: add flexible Json type annotation for Pydantic models (#1597)#1599
Sanjeev888 wants to merge 1 commit into
supabase:mainfrom
Sanjeev888:fix/json-deserialization-issue-1597

Conversation

@Sanjeev888

Copy link
Copy Markdown

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

PostgREST returns JSON/JSONB columns as already-deserialized Python objects (dict, list, int, etc.). When validating these objects against Pydantic models typed with pydantic.Json, Pydantic raises a ValidationError because pydantic.Json requires raw string input.

Fixes #1597

What is the new behavior?

Added a flexible, subscriptable Json type annotation in postgrest.types (re-exported in supabase and supabase.types) using pydantic.BeforeValidator. It accepts both already-deserialized Python data objects and raw JSON-encoded strings.

Usage

from supabase import Json

class MyTable(BaseModel):
    id: int
    data: Json                   # accepts dict, list, scalar, or json string
    typed_data: Json[MySubModel] # parses json string if needed, validates as MySubModel

@Sanjeev888
Sanjeev888 requested review from a team and o-santi as code owners August 31, 2026 06:59
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.

Json columns don't deserialize correctly

1 participant