Skip to content

fix: Datetime format mismatch causing a bug in chainlit_data_layer.py#2821

Closed
jakubgajski wants to merge 5 commits intoChainlit:mainfrom
jakubgajski:chainlit_data_layer_get_step_createdAt_format_mismatch
Closed

fix: Datetime format mismatch causing a bug in chainlit_data_layer.py#2821
jakubgajski wants to merge 5 commits intoChainlit:mainfrom
jakubgajski:chainlit_data_layer_get_step_createdAt_format_mismatch

Conversation

@jakubgajski
Copy link
Copy Markdown

@jakubgajski jakubgajski commented Mar 2, 2026

Problem

The ChainlitDataLayer methods were generating ISO-formatted datetime strings without the Z suffix (e.g., 2026-03-02T11:54:54.670000), while the widely-used utc_now() utility function in backend/chainlit/utils.py consistently produces timestamps with the Z suffix (e.g., 2026-03-02T11:54:54.670000Z).
This inconsistency caused ValueError exceptions when parsing datetime strings:

# lines 373-375
        created_at = step_dict.get("createdAt")
        if created_at:
            timestamp = datetime.strptime(created_at, ISO_FORMAT)
ValueError: time data '2026-03-02T10:29:07.146000' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'

While the original log was:

{"event": "Task exception was never retrieved\nfuture: <Task finished name='Task-359' coro=<ChainlitDataLayer.update_step() done, defined at /<project-name-redacted>/.venv/lib/python3.13/site-packages/chainlit/data/utils.py:10> exception=ValueError(\"time data '2026-03-02T11:54:54.670000' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'\")>", "exc_info": ["<class 'ValueError'>", "ValueError(\"time data '2026-03-02T11:54:54.670000' does not match format '%Y-%m-%dT%H:%M:%S.%fZ'\")", "<traceback object at 0x10ef46080>"], "level": "error", "logger": "asyncio", "timestamp": "2026-03-02T12:10:30.840355Z"}

Solution

Added Z suffix to all ISO-formatted datetime strings in ChainlitDataLayer to ensure consistency with the utc_now() format used throughout the codebase.

Modified methods:
get_user() - line 98
create_user() - line 124
list_threads() - line 500
get_thread() - line 564
_convert_step_row_to_dict() - lines 684, 687, 689 (for createdAt, start, and end fields)

Changes

Fix (cc120b7): Modified 6 locations in chainlit_data_layer.py to append "Z" to .isoformat() calls

Tests (b7ebd72): Added comprehensive test coverage (206 lines) verifying:

  • Z suffix presence across all affected methods
  • Proper handling of None timestamps in _convert_step_row_to_dict()
  • Alignment with utc_now() datetime format

Impact

Prevents ValueError exceptions during datetime parsing
Ensures consistent UTC timezone representation across the data layer
Maintains compatibility with existing parsing logic that expects the Z suffix


Summary by cubic

Standardize datetime strings in ChainlitDataLayer by appending the "Z" suffix to ISO timestamps to match utc_now(). This prevents parsing errors and ensures all timestamps are UTC.

  • Bug Fixes
    • Append "Z" to ISO timestamps in get_user, create_user, list_threads, get_thread, and _convert_step_row_to_dict (createdAt, start, end).
    • Add tests confirming the "Z" suffix across methods and correct handling of None timestamps.

Written for commit 3e25918. Summary will update on new commits.

…rings for compliance with widely used utc_now() from backend/chainlit/utils.py
…ce with `Z` suffix

- Added tests to ensure `createdAt`, `startTime`, and `endTime` align with `utc_now()` compliance.
- Verified `Z` suffix is present across `get_user`, `create_user`, `list_threads`, `get_thread`, and `_convert_step_row_to_dict`.
- Added coverage for handling `None` timestamps in `_convert_step_row_to_dict`.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working data layer Pertains to data layers. unit-tests Has unit tests. labels Mar 2, 2026
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@jakubgajski jakubgajski changed the title Datetime format mismatch causing a bug in chainlit_data_layer.py fix: Datetime format mismatch causing a bug in chainlit_data_layer.py Mar 3, 2026
@dokterbob
Copy link
Copy Markdown
Collaborator

Thanks for the contrib. However, this seems a dup of #2798. If I am wrong, please, reopen!

@dokterbob dokterbob closed this Mar 31, 2026
@dokterbob
Copy link
Copy Markdown
Collaborator

The other (approved) PR is just pending merge conflicts. Feel free to solve them and shoot a new PR yourself. Or poke the original author. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working data layer Pertains to data layers. size:L This PR changes 100-499 lines, ignoring generated files. unit-tests Has unit tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants