Skip to content

Commit fc833af

Browse files
authored
Merge pull request #84 from WorkflowAI/fix/print-datetime
Fix: Correct datetime serialization
2 parents 1e1fe81 + 66cf600 commit fc833af

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

workflowai/core/domain/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def format_output(self) -> str:
110110
# Format the output string
111111
output: list[str] = []
112112
# In case of partial validation, it is possible that the output is an empty model
113-
if dumped_output := self.output.model_dump():
113+
if dumped_output := self.output.model_dump(mode="json"):
114+
# Use model_dump_json which handles datetime serialization correctly
114115
output += [
115116
"\nOutput:",
116117
"=" * 50,

workflowai/core/domain/run_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from datetime import datetime, timezone
12
from unittest.mock import Mock, patch
23

34
import pytest
@@ -17,6 +18,11 @@ class _TestOutput(BaseModel):
1718
message: str
1819

1920

21+
class _TestOutputWithDatetime(BaseModel):
22+
timestamp: datetime
23+
message: str
24+
25+
2026
@pytest.fixture
2127
def mock_agent() -> Mock:
2228
mock = Mock(spec=_AgentBase)
@@ -169,6 +175,29 @@ def test_format_output_tool_call_requests(self):
169175
URL: https://workflowai.hello/_/agents/agent-id/runs/run-id"""
170176
)
171177

178+
def test_format_output_with_datetime(self):
179+
"""Test that datetimes in the output model are correctly serialized to ISO strings."""
180+
test_dt = datetime(2024, 1, 1, 12, 30, 0, tzinfo=timezone.utc)
181+
run = Run[_TestOutputWithDatetime](
182+
id="run-dt-id",
183+
agent_id="agent-dt-id",
184+
schema_id=2,
185+
output=_TestOutputWithDatetime(timestamp=test_dt, message="datetime test"),
186+
duration_seconds=0.5,
187+
cost_usd=0.0001,
188+
)
189+
190+
expected_json_part = '{\n "timestamp": "2024-01-01T12:30:00Z",\n "message": "datetime test"\n}'
191+
expected = f"""\nOutput:
192+
==================================================
193+
{expected_json_part}
194+
==================================================
195+
Cost: $ 0.00010
196+
Latency: 0.50s
197+
URL: https://workflowai.hello/_/agents/agent-dt-id/runs/run-dt-id"""
198+
199+
assert run.format_output() == expected
200+
172201

173202
class TestRunURL:
174203
# The @patch decorator from unittest.mock temporarily replaces the value of an attribute

0 commit comments

Comments
 (0)