Repository navigation
[fix] Normalize offset-less ISO strings to UTC in to_utc_datetime - #10301
Lesereingrape wants to merge 1 commit into
Conversation
|
Real CI has not run on this PR yet - it needs one click from someone with write access. The three workflow runs on head The verification I do control - the The other six PRs from this account are in the same state (#10281, #10287, #10297, #10320, #10323, #10325), each with 2-3 runs awaiting approval. The repo-wide queue is large, so I realise this is not a trivial ask; say the word and I will narrow the set down. Disclosure: this account is being operated by an AI coding assistant on the owner's instruction, and the PR bodies say so as well. |
Summary
to_utc_datetime()promises a UTC datetime, and three of its four branches enforce that — thedatetimebranchreplaces a missing tzinfo, the
datebranch combines withtzinfo=timezone.utc, the numeric branch usesfromtimestamp(..., tz=utc). The ISO-string branch returneddatetime.fromisoformat(value)directly, so anoffset-less string came back naive:
to_utc_datetime(date(2024, 1, 1))is aware whileto_utc_datetime("2024-01-01")— the same instant, the spelling the JSON-ish backends persist — is naive, andcomparing the two raises
TypeError: can't compare offset-naive and offset-aware datetimes.Through the shipped model that means
GET /metrics/dailyrenders"date": "2026-09-18T00:00:00"next to"created_at": "2026-09-18T14:56:05Z": one payload, two offset conventions. A client parsing the offset-lessdate-time reads it as local time, so the same daily bucket shifts a calendar day across viewer timezones.
The fix normalizes the parsed value exactly as the
datetimebranch already does, so an explicit offset(
...Z,...+05:30) is still preserved rather than flattened.Closes #10300
Type of change
Checklist
./scripts/format.shand./scripts/validate.sh)Duplicate and AI-Generated PR Check
Additional Notes
How this was verified (Python 3.13 on Windows; no DB, server, network or API key):
main(85b6d1d):date(2024,1,1)→tzinfo=UTC,"2024-01-01"→tzinfo=None,"2024-01-01T12:00:00"→tzinfo=None,"…Z"→tzinfo=UTC, andto_utc_datetime(date(2024,1,1)) > to_utc_datetime("2024-01-01")→TypeError.DayAggregatedMetrics.from_dict({"date": "2026-09-18", …})yieldeddate.tzinfo=None,created_at.tzinfo=UTCand a JSON body of2026-09-18T00:00:00vs2026-09-18T14:56:05Z;after the change both render with
Z. The persisted value really is an offset-less string on these backends —db/in_memory/utils.py:117date_iso = date_to_process.isoformat()→:132"date": date_iso(same indb/json/utils.py:132,db/gcs_json/utils.py:132), which their own readers treat as a string viadatetime.strptime(metric.get("date", ""), "%Y-%m-%d")(in_memory_db.py:1063,json_db.py:1271,gcs_json_db.py:1253).libs/agno/tests/unit/os/test_utils.py: 176 tests (173 existing + 3 new). With the change 175 pass; the onefailure is
test_handles_negative_timestamp, which fails identically on unmodifiedmainon this box becauseWindows
datetime.fromtimestamp()rejects a negative epoch. Nothing about that branch is touched here.test_adds_utc_to_offsetless_iso_stringandtest_iso_date_string_matches_date_object, so the new tests cover the bug rather than the harness.tests/unit/os(run with--continue-on-collection-errors): baseline103 failed / 1368 passed / 126 collection errors→ patched101 failed / 1370 passed / 126 collection errors;diffing the two
FAILEDlists shows the only change is my two new tests flipping, 0 regressions. The residualfailures/errors are pre-existing and come from optional extras missing in this local env.
ruff check,ruff format --checkandruff check --select Iare clean on both changed files;mypyreports noerror in
agno/os/utils.py(only the pre-existing missing-optional-stub andteam/_run.pyerrors elsewhere).Behaviour intentionally preserved: an ISO string that carries an explicit non-UTC offset keeps it
(
test_preserves_non_utc_offset_in_iso_string), matchingtest_preserves_non_utc_timezonefordatetimeobjects.The only value that changes is an offset-less string, which previously produced a naive datetime.
Collision check — searched open issues/PRs for
to_utc_datetime/fromisoformat/naive, then scanned thechanged-file lists of all 767 open PRs. 22 touch
libs/agno/agno/os/utils.py, but none of their patches containsfromisoformat, so no one edits this branch. The closest hunk is #8540 (open since 2026-06-23) at@@ -52,7 +52,7 @@, which replaces the numeric fallback (fromtimestamp→ epoch +timedelta) three linesbelow where my hunk ends — a different branch of the same function, no overlapping lines. Two PRs touch
tests/unit/os/test_utils.py(#8945, #8540) and both append at the end of the file (@@ -213,3 +…, forformat_sse_eventand media-normalization tests); this PR inserts aftertest_preserves_non_utc_timezonenear line79, so the additions are independent.
AI-generated disclosure: the code and tests in this PR were written by an AI coding agent, and every line of the diff was read by that same agent, which also ran every check listed above. No human has reviewed this diff, so the self-review box above is the agent's own review. (This sentence replaced the wording “reviewed line by line by the author” on 2026-09-20; a body edit leaves no public trace, so the correction is stated here.)
AI assistance (per
CONTRIBUTING.md§5): the investigation, the diff and this description were produced by an AI coding agent working under the account owner's standing instruction to find and fix defects of this kind and open PRs for them; no human read the diff before it was opened. Everything quoted in the validation section was run by that agent on a local checkout and is reproducible on a fresh clone of this branch. The commit carries no AI co-author trailer.