Skip to content

Serialization problem with DataModel and evaluator suite .add_case method #426

@marcinwolder-miquido

Description

@marcinwolder-miquido

There is an issue with JSON serialization of DataModel that contain datetime attribute.

Expected Behavior

There should be a new case in the storage json file.

[
    {
        "identifier": "test",
        "parameters": {
            "date": "2025-09-25T11:42:09.804558"
        }
    }
]

Current Behavior

Draive fails to serialize a DataModel object.

Traceback:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/marcinwolder/Desktop/repo/Casualty-AI/local/__main__.py", line 28, in <module>
    asyncio.run(main())
    ~~~~~~~~~~~^^^^^^^^
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/Users/marcinwolder/Desktop/repo/Casualty-AI/local/__main__.py", line 24, in main
    await test_evaluator_suite.add_case(test_obj)
  File "/Users/marcinwolder/Desktop/repo/Casualty-AI/.venv/lib/python3.13/site-packages/draive/evaluation/suite.py", line 675, in add_case
    await self._cases_storage.save(self._cases_cache)
  File "/Users/marcinwolder/Desktop/repo/Casualty-AI/.venv/lib/python3.13/site-packages/draive/evaluation/suite.py", line 930, in save
    await self._file_save(cases)
  File "/Users/marcinwolder/Desktop/repo/Casualty-AI/.venv/lib/python3.13/site-packages/haiway/helpers/asynchrony.py", line 113, in asynchronous
    return await (loop or get_running_loop()).run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
    )
    ^
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/marcinwolder/Desktop/repo/Casualty-AI/.venv/lib/python3.13/site-packages/draive/evaluation/suite.py", line 938, in _file_save
    file.write(json.dumps([case.to_mapping() for case in cases]).encode("utf-8"))
               ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
  File "/Users/marcinwolder/.local/share/uv/python/cpython-3.13.7-macos-aarch64-none/lib/python3.13/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
                    f'is not JSON serializable')
TypeError: Object of type datetime is not JSON serializable

Possible Solution

DataModel should use its .to_json method before evaluation suite tries to save it.

Steps to Reproduce

  1. Create main.py:

    import asyncio
    from collections.abc import Sequence
    from datetime import datetime
    
    from draive import DataModel, Field, ctx
    from draive.evaluation import EvaluatorResult, EvaluatorScenarioResult, evaluator_suite
    
    
    class TestModel(DataModel):
        date: datetime = Field(default=datetime.now())
    
    
    @evaluator_suite(TestModel, storage="test_suite.json")
    async def test_evaluator_suite(
        parameters: TestModel,
    ) -> Sequence[EvaluatorScenarioResult | EvaluatorResult]:
        return []
    
    
    async def main() -> None:
        async with ctx.scope("main"):
            test_obj = TestModel()
            print(test_obj.to_json())  # This prints json with serialized datetime (casted to str)
            await test_evaluator_suite.add_case(test_obj)
    
    
    if __name__ == "__main__":
        asyncio.run(main())
  2. Create test_suite.json:

    []
  3. Run python3 main.py

Context (Environment)

  • python version: 3.13.7
  • requirements:
    Package           Version  Editable project location
    ----------------- -------- --------------------------------------------
    annotated-types   0.7.0
    anyio             4.11.0
    asyncpg           0.30.0
    bandit            1.8.6
    casuality         0.1.0    /Users/marcinwolder/Desktop/repo/Casualty-AI
    certifi           2025.8.3
    cffi              2.0.0
    click             8.3.0
    coverage          7.10.7
    cryptography      46.0.1
    distro            1.9.0
    draive            0.85.5
    fastapi-slim      0.117.1
    grpcio            1.75.0
    h11               0.16.0
    h2                4.3.0
    haiway            0.32.0
    hpack             4.1.0
    httpcore          1.0.9
    httpx             0.28.1
    hyperframe        6.1.0
    idna              3.10
    iniconfig         2.1.0
    jiter             0.11.0
    joserfc           1.3.4
    markdown-it-py    4.0.0
    mdurl             0.1.2
    nodeenv           1.9.1
    numpy             2.3.3
    openai            1.108.2
    packaging         25.0
    pillow            11.3.0
    pluggy            1.6.0
    portalocker       3.2.0
    protobuf          6.32.1
    pyaudio           0.2.14
    pycparser         2.23
    pydantic          2.11.9
    pydantic-core     2.33.2
    pygments          2.19.2
    pypdfium2         4.30.0
    pyright           1.1.405
    pytest            8.4.2
    pytest-asyncio    1.2.0
    pytest-cov        7.0.0
    pyyaml            6.0.2
    qdrant-client     1.15.1
    rich              14.1.0
    ruff              0.13.1
    sniffio           1.3.1
    starlette         0.48.0
    stevedore         5.5.0
    tqdm              4.67.1
    typing-extensions 4.15.0
    typing-inspection 0.4.1
    urllib3           2.5.0
    uvicorn           0.36.0
    websocket-client  1.8.0
    websockets        15.0.1
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions