Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

fix(serde): round-trip bytes fields in pydantic models (#2817)

Raphael Malikian0c6758eaa9e6
diff --git a/python/cocoindex/_internal/serde.py b/python/cocoindex/_internal/serde.py
index 0a0980f4..c7a42c20 100644
--- a/python/cocoindex/_internal/serde.py
+++ b/python/cocoindex/_internal/serde.py
@@ -292,6 +292,19 @@ def _raise_if_class_not_unpickle_safe(obj: Any) -> None:
)
+def _dump_pydantic(model: Any) -> Any:
+ """Dump a pydantic model to data msgspec can encode.
+
+ JSON mode turns every type pydantic knows (URLs, IP addresses, ...) into
+ plain data, but decodes ``bytes`` as UTF-8. Only when that fails, dump in
+ python mode, which keeps the bytes as bytes.
+ """
+ try:
+ return model.model_dump(mode="json")
+ except UnicodeDecodeError:
+ return model.model_dump(mode="python")
+
+
# ---------------------------------------------------------------------------
# Serialization hooks (cross-pollination bridge)
# ---------------------------------------------------------------------------
@@ -313,7 +326,7 @@ def _enc_hook(obj: Any) -> Any:
return msgspec.msgpack.Ext(100, _strict_pickle_dumps(obj))
# B: Bridge Pydantic into msgspec
if _is_pydantic_instance(obj):
- return obj.model_dump(mode="json")
+ return _dump_pydantic(obj)
raise NotImplementedError(f"Cannot serialize {type(obj).__name__}")
@@ -338,8 +351,9 @@ def _dec_hook(type_hint: Any, obj: Any) -> Any:
Called when msgspec encounters a type it doesn't natively support.
Only three cases reach here:
- 1. Pydantic models β€” enc_hook serialized via model_dump(mode="json"),
- so *obj* is a dict that needs model_validate to reconstruct.
+ 1. Pydantic models β€” enc_hook serialized via _dump_pydantic (JSON mode,
+ falling back to python mode only when JSON mode fails on non-UTF-8
+ bytes), so *obj* is a dict that needs model_validate to reconstruct.
2. Pickle-quarantined values β€” ext_hook already reconstructed the
correct object; just pass through.
3. Numpy scalar *classes* (e.g. ``np.float32``) pickled as global
@@ -500,7 +514,7 @@ def serialize(value: Any) -> bytes:
# B: Pydantic BaseModel
if _is_pydantic_instance(value):
payload = msgspec.msgpack.encode(
- value.model_dump(mode="json"),
+ _dump_pydantic(value),
enc_hook=_enc_hook,
)
return b"\x02" + payload
diff --git a/python/tests/core/test_serde.py b/python/tests/core/test_serde.py
index 9b4125a5..400abc5a 100644
--- a/python/tests/core/test_serde.py
+++ b/python/tests/core/test_serde.py
@@ -1,6 +1,9 @@
"""Comprehensive tests for cocoindex._internal.serde serialize/deserialize."""
import datetime
+import decimal
+import enum
+import ipaddress
import pathlib
import pickle
import uuid
@@ -249,6 +252,123 @@ class TestRoundtripPydantic:
assert result == value
assert isinstance(result, MyModel)
+ def test_pydantic_bytes_field(self) -> None:
+ """Non-UTF-8 bytes in a pydantic model round-trip instead of raising.
+
+ model_dump(mode="json") decoded bytes as UTF-8, so any non-UTF-8
+ payload (raw file bytes, a hash digest) crashed serialize() with
+ UnicodeDecodeError. mode="python" keeps bytes as msgpack bin.
+ """
+ pydantic = pytest.importorskip("pydantic")
+
+ class WithBytes(pydantic.BaseModel): # type: ignore[misc,name-defined]
+ text: str
+ raw: bytes
+
+ value = WithBytes(text="hello", raw=b"\x89PNG\r\n\x1a\n\x00\xff")
+ data = serialize(value)
+ assert _routing_byte(data) == 0x02
+ result = deserialize(data, WithBytes)
+ assert result == value
+ assert isinstance(result, WithBytes)
+ assert result.raw == b"\x89PNG\r\n\x1a\n\x00\xff"
+
+ def test_pydantic_bytes_field_nested(self) -> None:
+ """Bytes survive in a nested pydantic model and in a dataclass wrapper."""
+ pydantic = pytest.importorskip("pydantic")
+
+ class Inner(pydantic.BaseModel): # type: ignore[misc,name-defined]
+ raw: bytes
+
+ class Outer(pydantic.BaseModel): # type: ignore[misc,name-defined]
+ inner: Inner
+
+ value = Outer(inner=Inner(raw=b"\xff\x00"))
+ data = serialize(value)
+ assert _routing_byte(data) == 0x02
+ assert deserialize(data, Outer) == value
+
+ @dataclass
+ class HasPyd:
+ inner: Inner
+ n: int
+
+ wrapped = HasPyd(inner=Inner(raw=b"\xff\x00"), n=3)
+ data = serialize(wrapped)
+ assert _routing_byte(data) == 0x01
+ assert deserialize(data, HasPyd) == wrapped
+
+ def test_pydantic_ipaddress_field(self) -> None:
+ """An ipaddress field round-trips via the JSON-mode dump.
+
+ model_dump(mode="json") stringifies ipaddress objects, and
+ model_validate reconstructs them on the way back, so no unpickle
+ registration is needed for these fields.
+ """
+ pydantic = pytest.importorskip("pydantic")
+
+ class WithIP(pydantic.BaseModel): # type: ignore[misc,name-defined]
+ host: ipaddress.IPv4Address
+
+ value = WithIP(host=ipaddress.IPv4Address("1.2.3.4"))
+ data = serialize(value)
+ assert _routing_byte(data) == 0x02
+ result = deserialize(data, WithIP)
+ assert result == value
+ assert isinstance(result.host, ipaddress.IPv4Address)
+
+ def test_pydantic_json_mode_fields_unchanged(self) -> None:
+ """Fields that JSON mode turns into strings round-trip as before."""
+ pydantic = pytest.importorskip("pydantic")
+
+ class Doc(pydantic.BaseModel): # type: ignore[misc,name-defined]
+ url: pydantic.HttpUrl # type: ignore[name-defined]
+ when: datetime.datetime
+
+ plus5 = datetime.timezone(datetime.timedelta(hours=5))
+ value = Doc(
+ url="https://example.com/a",
+ when=datetime.datetime(2024, 1, 2, 3, 4, 5, tzinfo=plus5),
+ )
+ result = deserialize(serialize(value), Doc)
+ assert result == value
+ assert result.when.utcoffset() == datetime.timedelta(hours=5)
+
+ def test_pydantic_mixed_types_roundtrip(self) -> None:
+ """Guards the dump-mode change across every scalar the dump can emit."""
+ pydantic = pytest.importorskip("pydantic")
+
+ class Color(enum.Enum):
+ RED = 1
+
+ class KitchenSink(pydantic.BaseModel): # type: ignore[misc,name-defined]
+ b: bytes
+ when: datetime.datetime
+ day: datetime.date
+ dec: decimal.Decimal
+ u: uuid.UUID
+ c: Color
+ p: pathlib.Path
+ s: set[int]
+ tup: tuple[int, str]
+
+ value = KitchenSink(
+ b=b"\x89PNG\x00\xff",
+ when=datetime.datetime(2024, 1, 2, 3, 4, 5),
+ day=datetime.date(2024, 1, 2),
+ dec=decimal.Decimal("3.14"),
+ u=uuid.UUID(int=7),
+ c=Color.RED,
+ p=pathlib.Path("/a/b"),
+ s={1, 2},
+ tup=(1, "x"),
+ )
+ data = serialize(value)
+ assert _routing_byte(data) == 0x02
+ result = deserialize(data, KitchenSink)
+ assert result == value
+ assert isinstance(result, KitchenSink)
+
class TestRoundtripSerializeByPickle:
"""Test 7: @serialize_by_pickle dataclass round-trip with 0x80."""