Skip to content

fix(backend): read naive timestamps as UTC when creating a developer conversation from text - #22900

Merged
kodjima33 merged 1 commit into
BasedHardware:mainfrom
sauranshbhardwaj:fix/dev-conversation-naive-timestamps
Oct 21, 2026
Merged

kodjima33 merged 1 commit into
BasedHardware:mainfrom
sauranshbhardwaj:fix/dev-conversation-naive-timestamps

Conversation

@sauranshbhardwaj

@sauranshbhardwaj sauranshbhardwaj commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What's wrong

POST /v1/dev/user/conversations (create a conversation from text) checks the timestamps like this:

# backend/routers/developer.py
started_at = request.started_at if request.started_at is not None else datetime.now(timezone.utc)
finished_at = request.finished_at if request.finished_at is not None else started_at + timedelta(seconds=300)

if finished_at < started_at:

When one timestamp has a UTC offset and the other doesn't, or a finished_at without an offset meets the aware "now" default for started_at, Python can't compare them and raises TypeError. The request fails with HTTP 500 instead of a usable answer.

The CLI sends values without an offset for date-only and plain YYYY-MM-DDTHH:MM:SS flags, so omi conversation create --text ... --finished-at 2099-01-01 hits it.

The sibling route, from-segments, got a guard for the same comparison in #13511 (#13505). This route didn't.

body (besides text) before after
finished_at: "2099-01-01T00:10:00" 500 200, finish read as 00:10 UTC
started_at: "2026-01-01T00:00:00", finished_at: "2026-01-01T00:10:00Z" 500 200
started_at: "2026-01-01T00:00:00Z", finished_at: "2026-01-01T00:10:00" 500 200
started_at: "2026-01-01T00:10:00", finished_at: "2026-01-01T00:00:00Z" (finish before start) 500 422 finished_at must be after started_at
both without an offset 200 200 (unchanged)

Change

CreateConversationRequest gets a started_at/finished_at validator that reads a timestamp without an offset as UTC.

I didn't copy the from-segments 422. Two offset-less timestamps have always been accepted on this route, and the CLI sends them for date-only flags. Rejecting them would break requests that work today. Reading them as UTC matches how they were already stored.

The request's JSON schema is byte-for-byte the same before and after (validation and serialization modes), so the OpenAPI contract doesn't change.

Tests

backend/tests/unit/test_dev_api_conversations_poison.py (the existing developer-conversations harness) has 5 new cases through the real route with TestClient:

  • "reads a timestamp without an offset as UTC": the three mixed cases above. Each expects 200 and a UTC finished_at passed to processing.
  • "keeps accepting two naive timestamps as UTC".
  • "still rejects a finish before the start": expects 422, and nothing is processed.

All 5 fail on main (four return 500, and the naive pair reaches processing without an offset) and pass with the change.

backend/.venv/bin/python -m pytest -q tests/unit/test_dev_api_conversations_poison.py tests/unit/test_developer_from_segments_idempotency.py tests/unit/test_developer_read_routes.py tests/unit/test_developer_error_sanitization.py
92 passed

All 32 files picked by scripts/select_backend_unit_tests.py --changed-files for this diff pass, each run in its own process.

Failure-Class: none
Line-Count-Exception: backend/routers/developer.py | 2814 -> 2821 | the request-model validator belongs next to CreateConversationRequest, like its from-segments sibling
UI-Evidence: none -- backend-only request validation change, no UI.

View guided diff

…conversation from text

POST /v1/dev/user/conversations compared finished_at with started_at. A
timestamp without an offset next to one with an offset (or next to the
aware "now" default for started_at) made that comparison raise TypeError,
so the request failed with HTTP 500. The CLI sends naive values for
date-only flags, so `omi conversation create --finished-at 2026-10-20`
hit it.

Read a naive started_at/finished_at as UTC. Two naive timestamps were
already accepted and are still accepted; the request schema is unchanged.
@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution backend Backend Task (python) labels Oct 20, 2026

@Git-on-my-level Git-on-my-level left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict: correct fix, nicely scoped, no blocking issues.

  • backend/routers/developer.py:1157 — the read_naive_timestamp_as_utc validator fixes a real 500: finished_at < started_at at developer.py:1761 raises TypeError whenever one supplied timestamp is offset-naive and the other aware, or a naive finished_at meets the tz-aware datetime.now(timezone.utc) default at line 1757. Coercing naive to UTC at the model boundary resolves every mixed case, and both-naive bodies keep succeeding exactly as before (naive-vs-naive always compared fine).
  • Reading naive values as UTC instead of the 422 the from-segments routes use (require_timezone_offset, developer.py:1291) is the right call here — this route has always accepted two naive timestamps, so rejecting would break currently-working requests (e.g. the CLI's date-only flags). Worth knowing: naive values now persist as tz-aware UTC rather than naive datetimes; strictly safer downstream, but a small storage behavior change.
  • Blast radius confined: CreateConversationRequest is used only by POST /v1/dev/user/conversations (developer.py:1722), and the generated JSON schema is unchanged.
  • backend/tests/unit/test_dev_api_conversations_poison.py:286 — the parametrized test covers all three naive/aware combinations plus naive finished_at against the aware now-default; the companion tests pin both-naive acceptance and the inverted-range 422. Good coverage.

USER IMPACT: developers creating a conversation from text with a timestamp that lacks a timezone offset no longer get a 500 error — it is read as UTC and the request succeeds.

Non-blocking nit: the body claims the JSON schema is byte-for-byte unchanged — a tiny test asserting that would pin it.

Leaving for routine maintainer merge.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level

Copy link
Copy Markdown
Member

Verdict: the production change is correct and nicely scoped; leaving a comment rather than a formal approval, which is reserved for maintainer sign-off here.

  • backend/routers/developer.py:1157-1162 — the new read_naive_timestamp_as_utc validator attaches UTC to naive started_at/finished_at. Verified statically: field_validator and timezone are already imported (lines 11, 4), and the handler comparison at line 1761 against the aware datetime.now(timezone.utc) default at line 1757 is exactly where the naive-vs-aware TypeError (500) came from. Fix is correct.
  • Deliberate divergence from from-segments (whose model at line 1291 returns 422 for naive timestamps, from fix(backend): gate /v1/dev abuse, canonical invalid_patch visibility race, embeddings-400 degrade #13511) is the right call: two offset-less timestamps were always accepted on this route and the CLI sends them for date-only flags, so rejecting would break working requests.
  • backend/tests/unit/test_dev_api_conversations_poison.py — good coverage: all three mixed naive/aware parametrized cases, the two-naive backcompat case, and the finish-before-start 422, with process_conversation and resolve_geolocation cleanly patched (no fixture pollution). Backend unit suite and Hermetic Merge Gate green on this head.
  • Blast radius note: naive timestamps are now persisted as tz-aware UTC where they were previously stored naive. That is a normalization improvement, but any client string-comparing serialized timestamps may now see an offset/Z suffix appear.

USER IMPACT: developers creating a conversation from text with timezone-less timestamps (e.g. the CLI's date-only flags) now get a successful request with the timestamp read as UTC, instead of an HTTP 500.

Nothing blocking from my side. Optional nicety: a brief note in the endpoint docs that offset-less timestamps are interpreted as UTC. Leaving for human maintainer review before merge.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@sauranshbhardwaj

Copy link
Copy Markdown
Contributor Author

Thanks! On the storage note: Firestore already stores a naive datetime as UTC, so the saved timestamp is the same instant before and after (checked with google.api_core.datetime_helpers.to_microseconds). This route's response returns only the id and status flags, not the timestamps, so no client sees a new offset. I left the endpoint docs as they are so the generated schema and clients stay unchanged.

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Scoped bug fix (naive-timestamp TypeError -> 500 on /v1/dev/user/conversations), stated root cause, 5 new tests reproducing the crash and passing after the fix, CI green. Mirrors existing from-segments guard. Merging.

@kodjima33
kodjima33 merged commit 5378e99 into BasedHardware:main Oct 21, 2026
59 of 60 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants