Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion haystack/utils/base_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def _serialize_value_with_schema(payload: Any) -> dict[str, Any]: # noqa: PLR09
# Handle pydantic
if isinstance(payload, pydantic.BaseModel):
type_name = generate_qualified_class_name(type(payload))
return {"serialization_schema": {"type": type_name}, "serialized_data": payload.model_dump()}
# JSON mode turns fields such as datetime, date, UUID and Decimal into JSON-native values so the result
# can be written to a snapshot file. `model_validate` in the deserialization path parses them back.
return {"serialization_schema": {"type": type_name}, "serialized_data": payload.model_dump(mode="json")}

# Handle dictionary case - iterate through fields
if isinstance(payload, dict):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Fix pipeline breakpoints raising ``TypeError`` instead of ``BreakpointException`` when a pipeline input or
output is a pydantic model with a ``datetime``, ``date``, ``UUID`` or ``Decimal`` field and snapshot saving is
enabled. Pydantic models are now serialized in JSON mode, so the snapshot file is written correctly and can be
loaded to resume the pipeline. The same serialization is used for the ``Agent`` ``State``.
33 changes: 33 additions & 0 deletions test/core/pipeline/test_breakpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import json
import logging
from dataclasses import replace
from datetime import datetime
from typing import Any

import pydantic
import pytest

from haystack import component
Expand All @@ -29,6 +31,11 @@
from haystack.utils import _deserialize_value_with_schema
from haystack.utils.base_serialization import _serialize_value_with_schema


class _Event(pydantic.BaseModel):
happened_at: datetime


_EMPTY_OBJECT_PAYLOAD = {"serialization_schema": {"type": "object", "properties": {}}, "serialized_data": {}}


Expand Down Expand Up @@ -186,6 +193,32 @@ def run(self, v: dict[int, str]) -> dict[str, dict[int, str]]:
assert resumed == {"sink": {"v": {1: "a", 2: "b"}}}


def test_breakpoint_with_pydantic_model_with_datetime_field_saves_resumable_snapshot(tmp_path, monkeypatch):
monkeypatch.setenv(HAYSTACK_PIPELINE_SNAPSHOT_SAVE_ENABLED, "true")

@component
class Echo:
@component.output_types(v=_Event)
def run(self, v: _Event) -> dict[str, _Event]:
return {"v": v}

pipeline = Pipeline()
pipeline.add_component("echo", Echo())

event = _Event(happened_at=datetime(2024, 1, 2, 3, 4, 5))
break_point = Breakpoint(component_name="echo", visit_count=0, snapshot_file_path=str(tmp_path))

# json.dump of the snapshot used to raise TypeError here instead of BreakpointException
with pytest.raises(BreakpointException):
pipeline.run(data={"echo": {"v": event}}, break_point=break_point)

snapshot_files = list(tmp_path.glob("echo_*.json"))
assert len(snapshot_files) == 1

resumed = pipeline.run(data={}, pipeline_snapshot=load_pipeline_snapshot(snapshot_files[0]))
assert resumed == {"echo": {"v": event}}


@component
class _AppendingComponent:
@component.output_types(result=str)
Expand Down
26 changes: 25 additions & 1 deletion test/utils/test_base_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import json
import logging
from datetime import datetime
from datetime import date, datetime
from decimal import Decimal
from enum import Enum
from uuid import UUID

import pydantic
import pytest
Expand All @@ -31,6 +33,13 @@ class CustomModel(pydantic.BaseModel):
name: str


class ModelWithNonJsonFields(pydantic.BaseModel):
created_at: datetime
day: date
ident: UUID
amount: Decimal


class CustomEnum(Enum):
ONE = "one"
TWO = "two"
Expand Down Expand Up @@ -296,6 +305,21 @@ def test_serialize_and_deserialize_complex_types(value, result):
assert _deserialize_value_with_schema(result) == value


def test_serialize_pydantic_model_with_non_json_fields_is_json_safe():
model = ModelWithNonJsonFields(
created_at=datetime(2024, 1, 2, 3, 4, 5),
day=date(2024, 1, 2),
ident=UUID("12345678-1234-5678-1234-567812345678"),
amount=Decimal("1.50"),
)

serialized = _serialize_value_with_schema(model)

# `json.dumps` raises TypeError if a datetime, date, UUID or Decimal is left in the payload
restored = _deserialize_value_with_schema(json.loads(json.dumps(serialized)))
assert restored == model


def test_serialize_and_deserialize_value_with_schema_with_various_types():
data = {
"numbers": 1,
Expand Down