Skip to content

Commit 7525eb9

Browse files
committed
Enhance PydanticJSONB serialization by integrating to_jsonable_python for non-BaseModel types in lists and dictionaries
1 parent 636a45c commit 7525eb9

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

sqlmodel/sql/sqltypes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313

1414
from pydantic import BaseModel
15+
from pydantic_core import to_jsonable_python
1516
from sqlalchemy import types
1617
from sqlalchemy.dialects.postgresql import JSONB # for Postgres JSONB
1718
from sqlalchemy.engine.interfaces import Dialect
@@ -59,18 +60,20 @@ def process_bind_param(
5960
return value.model_dump(mode="json")
6061
if isinstance(value, list):
6162
return [
62-
m.model_dump(mode="json") if isinstance(m, BaseModel) else m
63+
m.model_dump(mode="json")
64+
if isinstance(m, BaseModel)
65+
else to_jsonable_python(m)
6366
for m in value
6467
]
6568
if isinstance(value, dict):
6669
return {
67-
k: v.model_dump(mode="json") if isinstance(v, BaseModel) else v
70+
k: v.model_dump(mode="json")
71+
if isinstance(v, BaseModel)
72+
else to_jsonable_python(v)
6873
for k, v in value.items()
6974
}
7075

71-
raise TypeError(
72-
f"Unsupported type for PydanticJSONB: {type(value)}. Expected a Pydantic model, a list of Pydantic models, or a dictionary of Pydantic models."
73-
)
76+
return to_jsonable_python(value)
7477

7578
def process_result_value(
7679
self, value: Any, dialect: Any

0 commit comments

Comments
 (0)