-
Notifications
You must be signed in to change notification settings - Fork 8.6k
fix(gateway): return ISO 8601 timestamps from threads endpoints #2599
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
Merged
WillemJiang
merged 3 commits into
bytedance:main
from
fancyboi999:fix/2594-thread-iso-timestamps
May 2, 2026
Merged
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
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,75 @@ | ||
| """ISO 8601 timestamp helpers for the Gateway and embedded runtime. | ||
|
|
||
| DeerFlow stores and serializes thread/run timestamps as ISO 8601 UTC | ||
| strings to match the LangGraph Platform schema (see | ||
| ``langgraph_sdk.schema.Thread``, where ``created_at`` / ``updated_at`` | ||
| are ``datetime`` and JSON-encode to ISO 8601). All timestamp generation | ||
| should funnel through :func:`now_iso` so the wire format stays | ||
| consistent across endpoints, the embedded ``RunManager``, and the | ||
| checkpoint metadata written by the Gateway. | ||
|
|
||
| :func:`coerce_iso` provides a forward-compatible read path for legacy | ||
| records that historically stored ``str(time.time())`` floats. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from datetime import UTC, datetime | ||
|
|
||
| __all__ = ["coerce_iso", "now_iso"] | ||
|
|
||
| _UNIX_TIMESTAMP_PATTERN = re.compile(r"^\d{10}(?:\.\d+)?$") | ||
| """Matches the unix-timestamp string shape historically written by | ||
| ``str(time.time())`` (10-digit seconds with optional fractional part). | ||
| The 10-digit anchor avoids accidentally rewriting ISO years like | ||
| ``"2026"`` and stays valid until the year 2286. | ||
| """ | ||
|
|
||
|
|
||
| def now_iso() -> str: | ||
| """Return the current UTC time as an ISO 8601 string. | ||
|
|
||
| Example: ``"2026-04-27T03:19:46.511479+00:00"``. | ||
| """ | ||
| return datetime.now(UTC).isoformat() | ||
|
|
||
|
|
||
| def coerce_iso(value: object) -> str: | ||
| """Best-effort coerce a stored timestamp to an ISO 8601 string. | ||
|
|
||
| Translates legacy unix-timestamp floats / strings written by older | ||
| DeerFlow versions into ISO without a one-shot migration. ISO strings | ||
| pass through unchanged; ``datetime`` instances are normalised to UTC | ||
| (tz-naive values are assumed to be UTC) and emitted via | ||
| ``isoformat()`` so the wire format always uses the ``T`` separator; | ||
| empty values become ``""``; unrecognised values are stringified as a | ||
| last resort. | ||
| """ | ||
| if value is None or value == "": | ||
| return "" | ||
| if isinstance(value, bool): | ||
| # ``bool`` is a subclass of ``int`` — treat as garbage, not 0/1. | ||
| return str(value) | ||
| if isinstance(value, datetime): | ||
| # ``datetime`` must be handled before the ``int``/``float`` check; | ||
| # str(datetime) would produce ``"YYYY-MM-DD HH:MM:SS+00:00"`` | ||
| # (space separator), which breaks strict ISO 8601 consumers. | ||
| if value.tzinfo is None: | ||
| value = value.replace(tzinfo=UTC) | ||
| else: | ||
| value = value.astimezone(UTC) | ||
| return value.isoformat() | ||
| if isinstance(value, (int, float)): | ||
| try: | ||
| return datetime.fromtimestamp(float(value), UTC).isoformat() | ||
| except (ValueError, OverflowError, OSError): | ||
| return str(value) | ||
| if isinstance(value, str): | ||
| if _UNIX_TIMESTAMP_PATTERN.match(value): | ||
| try: | ||
| return datetime.fromtimestamp(float(value), UTC).isoformat() | ||
| except (ValueError, OverflowError, OSError): | ||
| return value | ||
| return value | ||
| return str(value) | ||
Oops, something went wrong.
Oops, something went wrong.
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.
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.
coerce_iso()currently stringifies non-str/non-numeric inputs (includingdatetimeobjects). Ifcreated_at/updated_atever come through asdatetime(e.g., from LangGraph internals or in-memory stores),str(datetime)produces a space-separated format (YYYY-MM-DD HH:MM:SS+00:00) rather than ISO-8601 withT, which can break consumers expecting strict ISO. Consider handlingdatetimeexplicitly (and normalizing to UTC if tz-naive) by returningvalue.astimezone(UTC).isoformat()/value.replace(tzinfo=UTC).isoformat()as appropriate.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.
Addressed in ed9026f.
coerce_isonow branches ondatetimebefore theint/floatcheck and routes throughastimezone(UTC).isoformat()(orreplace(tzinfo=UTC)when tz-naive), so the output always uses theTseparator regardless of how an upstream component handed us the value.Three new test cases cover the contract:
test_coerce_iso_handles_tz_aware_datetime— explicit assertion thatTis in the output and a space is not.test_coerce_iso_handles_tz_naive_datetime_as_utc— tz-naive input is treated as UTC.test_coerce_iso_normalises_non_utc_datetime_to_utc—+08:00value gets normalised so the wire format stays UTC.Verification:
uv run pytest tests/test_utils_time.py -v→ 12 passed (3 new).