Fix inconsistent ISO timestamp formatting causing ValueError on step update#2798
Fix inconsistent ISO timestamp formatting causing ValueError on step update#2798hztBUAA wants to merge 3 commits intoChainlit:mainfrom
Conversation
…update Timestamps were saved with trailing Z (UTC indicator) but read back via .isoformat() without Z, causing a ValueError when update_step tried to re-parse the createdAt string via strptime with the Z-requiring format. Changes: - Add _parse_iso_datetime() helper that handles both with/without Z - Add _datetime_to_utc_iso() helper that ensures trailing Z on output - Replace all .isoformat() calls with _datetime_to_utc_iso() - Replace strptime(created_at, ISO_FORMAT) with _parse_iso_datetime() - Switch datetime.now() to datetime.utcnow() for UTC consistency - Add tests covering parse/format roundtrip and step row conversion Fixes Chainlit#2491
|
Would love to merge this, please run |
dokterbob
left a comment
There was a problem hiding this comment.
Great contrib! Please make requested fixups and ensure only code related to the problem at hand is in there! (E.g. stuff described in the PR description.)
If more changes are needed, knowing WHY is essential!
|
|
||
| def test_parse_without_z_raises_on_bad_format(self): | ||
| """Test that invalid format still raises ValueError.""" | ||
| with pytest.raises(ValueError): |
There was a problem hiding this comment.
This is actually correct, please add comment telling ruff to ignore this (explaining that it's the exception from underlying code).
| mime=str(row["mime"]), | ||
| objectKey=str(row["objectKey"]), | ||
| mime=str(row["mime"]) if row.get("mime") else None, | ||
| objectKey=row.get("objectKey"), |
There was a problem hiding this comment.
Changes here seem unrelated to the problem at hand.
| "Failed to get read URL for element '%s': %s", | ||
| elem.get("id", "unknown"), | ||
| e, | ||
| ) |
There was a problem hiding this comment.
Not sure it's a good idea to just log general Exception. This definitely should not be part of a fix regarding date time conversion!
|
Addressed review feedback in commit Changes made:
The PR is now scoped to the timestamp parsing/formatting fix. |
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="backend/chainlit/data/chainlit_data_layer.py">
<violation number="1">
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.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| @@ -1,4 +1,5 @@ | |||
| import json | |||
There was a problem hiding this comment.
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
Check if this issue is valid — if so, understand the root cause and fix it. At backend/chainlit/data/chainlit_data_layer.py, line 299:
<comment>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.</comment>
<file context>
@@ -296,10 +296,10 @@ async def get_element(
threadId=str(row["threadId"]),
type=metadata.get("type", "file"),
- url=row.get("url"),
+ url=str(row["url"]),
name=str(row["name"]),
- mime=str(row["mime"]) if row.get("mime") else None,
</file context>
|
Follow-up after running full local verification in a project-specific environment. Updated branch head: Validation run (from
Results:
This PR is now validated end-to-end locally with no pending formatter/lint/test issues on the touched scope. |
Summary
Fixes #2491
ChainlitDataLayersaves timestamps with a trailingZ(e.g.,2025-09-04T02:00:42.164000Z) viaISO_FORMAT, but reads them back using Python's.isoformat()which omits theZ(e.g.,2025-09-04T02:00:42.164000). Whenupdate_stepcallscreate_stepwith the read-backcreatedAtstring,strptimefails with:Changes
_parse_iso_datetime()helper that tolerates both with and without trailingZ_datetime_to_utc_iso()helper that ensures all datetime-to-string conversions consistently include the trailingZ.isoformat()calls with_datetime_to_utc_iso()across user, thread, and step serializationdatetime.strptime(created_at, ISO_FORMAT)with_parse_iso_datetime(created_at)increate_stepdatetime.now()todatetime.utcnow()inget_current_timestamp()andupdate_thread()for correct UTC semanticsTest plan
_parse_iso_datetimecorrectly parses both"...Z"and"..."formats_datetime_to_utc_isoalways produces strings ending withZcreate_stepValueErrorSummary by cubic
Fix inconsistent ISO timestamp handling to prevent ValueError during step updates (#2491). Standardizes UTC timestamps with a trailing Z and makes parsing accept both formats.
Written for commit ce95553. Summary will update on new commits.