Skip to content

fix(examples): replace deprecated datetime.utcnow() in web_search_gpt_oss_helper#669

Open
Ghraven wants to merge 1 commit into
ollama:mainfrom
Ghraven:fix/replace-deprecated-datetime-utcnow
Open

fix(examples): replace deprecated datetime.utcnow() in web_search_gpt_oss_helper#669
Ghraven wants to merge 1 commit into
ollama:mainfrom
Ghraven:fix/replace-deprecated-datetime-utcnow

Conversation

@Ghraven
Copy link
Copy Markdown

@Ghraven Ghraven commented May 19, 2026

What

datetime.utcnow() is deprecated since Python 3.12 and emits a DeprecationWarning whenever the helper is imported under a strict warnings config. The recommended replacement is datetime.now(timezone.utc), which also returns a timezone-aware UTC datetime instead of the naive one utcnow() produces — a small correctness win, since naive UTC datetimes can silently misbehave when compared against aware ones elsewhere in user code.

Why

  • Removes Python 3.12+ DeprecationWarning from the example.
  • Returns aware UTC datetimes, which is the modern Python recommendation.
  • Behaviour-equivalent for the example's downstream use: fetched_at: datetime accepts both naive and aware values.

Changes

examples/web_search_gpt_oss_helper.py:

  • Add timezone to the existing from datetime import datetime import.
  • Replace 5 occurrences of datetime.utcnow() with datetime.now(timezone.utc).

Total: +6/-6 lines, 1 file.

Testing

Pure call-site swap with no logic change. The file parses cleanly (python -m py_compile). The example still produces the same wall-clock instant for fetched_at, just with a tzinfo=UTC attached instead of None.

datetime.utcnow() is deprecated since Python 3.12 and emits a
DeprecationWarning when imported in environments configured to surface
deprecation warnings. The recommended replacement returns an
explicitly timezone-aware UTC datetime instead of a naive one, which
also avoids the subtle bug of naive datetimes being compared against
aware datetimes elsewhere in user code.

Replace 5 occurrences of datetime.utcnow() with
datetime.now(timezone.utc) in examples/web_search_gpt_oss_helper.py
and add timezone to the datetime import.

No behaviour change for the example's downstream use of fetched_at
(typed as datetime, accepts both naive and aware values).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant