-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix inconsistent ISO timestamp formatting causing ValueError on step update #2798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hztBUAA
wants to merge
3
commits into
Chainlit:main
Choose a base branch
from
hztBUAA:fix/timestamp-parsing-2491
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−10
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
|
|
||
| from chainlit.data.chainlit_data_layer import ( | ||
| ChainlitDataLayer, | ||
| _datetime_to_utc_iso, | ||
| _parse_iso_datetime, | ||
| ) | ||
|
|
||
|
|
||
| class TestParseIsoDatetime: | ||
| """Test suite for _parse_iso_datetime helper.""" | ||
|
|
||
| def test_parse_with_z_suffix(self): | ||
| """Test parsing ISO datetime string with trailing Z.""" | ||
| result = _parse_iso_datetime("2025-09-04T02:00:42.164000Z") | ||
| assert result == datetime(2025, 9, 4, 2, 0, 42, 164000) | ||
|
|
||
| def test_parse_without_z_suffix(self): | ||
| """Test parsing ISO datetime string without trailing Z (the bug case).""" | ||
| result = _parse_iso_datetime("2025-09-04T02:00:42.164000") | ||
| assert result == datetime(2025, 9, 4, 2, 0, 42, 164000) | ||
|
|
||
| def test_parse_without_z_raises_on_bad_format(self): | ||
| """Test that invalid format still raises ValueError.""" | ||
| with pytest.raises(ValueError, match="does not match format"): | ||
| _parse_iso_datetime("2025-09-04 02:00:42") | ||
|
|
||
| def test_roundtrip_with_z(self): | ||
| """Test that parsing a Z-suffixed string and formatting round-trips.""" | ||
| original = "2025-09-04T02:00:42.164000Z" | ||
| dt = _parse_iso_datetime(original) | ||
| formatted = _datetime_to_utc_iso(dt) | ||
| assert formatted == original | ||
|
|
||
| def test_roundtrip_without_z(self): | ||
| """Test that parsing a non-Z string and formatting produces Z-suffixed output.""" | ||
| original = "2025-09-04T02:00:42.164000" | ||
| dt = _parse_iso_datetime(original) | ||
| formatted = _datetime_to_utc_iso(dt) | ||
| assert formatted == original + "Z" | ||
|
|
||
|
|
||
| class TestDatetimeToUtcIso: | ||
| """Test suite for _datetime_to_utc_iso helper.""" | ||
|
|
||
| def test_adds_z_suffix(self): | ||
| """Test that Z is always appended.""" | ||
| dt = datetime(2025, 9, 4, 2, 0, 42, 164000) | ||
| result = _datetime_to_utc_iso(dt) | ||
| assert result == "2025-09-04T02:00:42.164000Z" | ||
|
|
||
| def test_no_double_z(self): | ||
| """Test that Z is not duplicated.""" | ||
| dt = datetime(2025, 1, 1, 0, 0, 0, 0) | ||
| result = _datetime_to_utc_iso(dt) | ||
| assert not result.endswith("ZZ") | ||
| assert result.endswith("Z") | ||
|
|
||
| def test_zero_microseconds(self): | ||
| """Test formatting with zero microseconds.""" | ||
| dt = datetime(2025, 1, 1, 12, 30, 45) | ||
| result = _datetime_to_utc_iso(dt) | ||
| assert result == "2025-01-01T12:30:45Z" | ||
| assert result.endswith("Z") | ||
|
|
||
|
|
||
| class TestConvertStepRowTimestamps: | ||
| """Test that _convert_step_row_to_dict produces timestamps with trailing Z.""" | ||
|
|
||
| def _make_layer(self): | ||
| return ChainlitDataLayer(database_url="postgresql://fake", storage_client=None) | ||
|
|
||
| def _make_step_row(self, **overrides): | ||
| row = { | ||
| "id": "step-1", | ||
| "threadId": "thread-1", | ||
| "parentId": None, | ||
| "name": "test_step", | ||
| "type": "run", | ||
| "input": "{}", | ||
| "output": "{}", | ||
| "metadata": "{}", | ||
| "createdAt": datetime(2025, 9, 4, 2, 0, 42, 164000), | ||
| "startTime": datetime(2025, 9, 4, 2, 0, 42, 164000), | ||
| "endTime": datetime(2025, 9, 4, 2, 0, 43, 0), | ||
| "showInput": "json", | ||
| "isError": False, | ||
| "feedback_id": None, | ||
| } | ||
| row.update(overrides) | ||
| return row | ||
|
|
||
| def test_step_timestamps_have_z_suffix(self): | ||
| """Test that step createdAt, start, end all end with Z.""" | ||
| layer = self._make_layer() | ||
| row = self._make_step_row() | ||
|
|
||
| result = layer._convert_step_row_to_dict(row) | ||
|
|
||
| assert result["createdAt"].endswith("Z"), ( | ||
| f"createdAt should end with Z, got: {result['createdAt']}" | ||
| ) | ||
| assert result["start"].endswith("Z"), ( | ||
| f"start should end with Z, got: {result['start']}" | ||
| ) | ||
| assert result["end"].endswith("Z"), ( | ||
| f"end should end with Z, got: {result['end']}" | ||
| ) | ||
|
|
||
| def test_step_timestamps_can_be_reparsed(self): | ||
| """Test that timestamps from _convert_step_row_to_dict can be parsed back. | ||
|
|
||
| This is the exact scenario from bug #2491: after reading a step from DB, | ||
| the createdAt string should be parseable when passed back to | ||
| create_step/update_step. | ||
| """ | ||
| layer = self._make_layer() | ||
| row = self._make_step_row() | ||
|
|
||
| result = layer._convert_step_row_to_dict(row) | ||
|
|
||
| # Simulate what create_step does when update_step feeds back the step dict | ||
| parsed = _parse_iso_datetime(result["createdAt"]) | ||
| assert parsed == datetime(2025, 9, 4, 2, 0, 42, 164000) | ||
|
|
||
| def test_step_none_timestamps_preserved(self): | ||
| """Test that None timestamps are preserved as None.""" | ||
| layer = self._make_layer() | ||
| row = self._make_step_row(createdAt=None, startTime=None, endTime=None) | ||
|
|
||
| result = layer._convert_step_row_to_dict(row) | ||
|
|
||
| assert result["createdAt"] is None | ||
| assert result["start"] is None | ||
| assert result["end"] is None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: get_element now coerces nullable url/mime/objectKey fields to strings, turning NULLs into the literal "None" and changing API semantics for consumers expecting None/falsy values.
Prompt for AI agents