Skip to content

fix: slackbot error#5430

Merged
Weves merged 4 commits intomainfrom
slackbot-fix
Sep 17, 2025
Merged

fix: slackbot error#5430
Weves merged 4 commits intomainfrom
slackbot-fix

Conversation

@Weves
Copy link
Contributor

@Weves Weves commented Sep 16, 2025

Description

Fixes:

2025-09-16 14:50:21.868TypeError: can't subtract offset-naive and offset-aware datetimes 2025-09-16 14:50:21.868           ~~~~^~~~~~ 2025-09-16 14:50:21.868    date = now - date 2025-09-16 14:50:21.868  File "/usr/local/lib/python3.11/site-packages/timeago/__init__.py", line 52, in format 2025-09-16 14:50:21.868    timeago.format(d.updated_at, datetime.now(pytz.utc)) 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/blocks.py", line 336, in _build_sources_blocks 2025-09-16 14:50:21.868                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-09-16 14:50:21.868    citations_block = _build_sources_blocks(cited_documents=cited_docs) 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/blocks.py", line 409, in _build_citations_blocks 2025-09-16 14:50:21.868                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-09-16 14:50:21.868    citations_blocks = _build_citations_blocks(answer) 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/blocks.py", line 639, in build_slack_response_blocks 2025-09-16 14:50:21.868                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-09-16 14:50:21.868    all_blocks = build_slack_response_blocks( 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/handlers/handle_regular_answer.py", line 385, in handle_regular_answer 2025-09-16 14:50:21.868                                ^^^^^^^^^^^^^^^^^^^^^^ 2025-09-16 14:50:21.868    issue_with_regular_answer = handle_regular_answer( 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/handlers/handle_message.py", line 231, in handle_message 2025-09-16 14:50:21.868             ^^^^^^^^^^^^^^^ 2025-09-16 14:50:21.868    failed = handle_message( 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/listener.py", line 991, in process_message 2025-09-16 14:50:21.868           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2025-09-16 14:50:21.868    return process_message(req, client) 2025-09-16 14:50:21.868  File "/app/onyx/onyxbot/slack/listener.py", line 1070, in process_slack_event 2025-09-16 14:50:21.868Traceback (most recent call last): | 2025-09-16 14:50:21.868 | TypeError: can't subtract offset-naive and offset-aware datetimes |   |   |   | 2025-09-16 14:50:21.868 | ~~~~^~~~~~ |   |   |   | 2025-09-16 14:50:21.868 | date = now - date |   |   |   | 2025-09-16 14:50:21.868 | File "/usr/local/lib/python3.11/site-packages/timeago/__init__.py", line 52, in format |   |   |   | 2025-09-16 14:50:21.868 | timeago.format(d.updated_at, datetime.now(pytz.utc)) |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/blocks.py", line 336, in _build_sources_blocks |   |   |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |   |   |   | 2025-09-16 14:50:21.868 | citations_block = _build_sources_blocks(cited_documents=cited_docs) |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/blocks.py", line 409, in _build_citations_blocks |   |   |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |   |   |   | 2025-09-16 14:50:21.868 | citations_blocks = _build_citations_blocks(answer) |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/blocks.py", line 639, in build_slack_response_blocks |   |   |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |   |   |   | 2025-09-16 14:50:21.868 | all_blocks = build_slack_response_blocks( |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/handlers/handle_regular_answer.py", line 385, in handle_regular_answer |   |   |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^ |   |   |   | 2025-09-16 14:50:21.868 | issue_with_regular_answer = handle_regular_answer( |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/handlers/handle_message.py", line 231, in handle_message |   |   |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^ |   |   |   | 2025-09-16 14:50:21.868 | failed = handle_message( |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/listener.py", line 991, in process_message |   |   |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |   |   |   | 2025-09-16 14:50:21.868 | return process_message(req, client) |   |   |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/listener.py", line 1070, in process_slack_event |   |   |   | 2025-09-16 14:50:21.868 | Traceback (most recent call last): |  
-- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | -- | --
2025-09-16 14:50:21.868 | TypeError: can't subtract offset-naive and offset-aware datetimes |  
  |   | 2025-09-16 14:50:21.868 | ~~~~^~~~~~ |  
  |   | 2025-09-16 14:50:21.868 | date = now - date |  
  |   | 2025-09-16 14:50:21.868 | File "/usr/local/lib/python3.11/site-packages/timeago/__init__.py", line 52, in format |  
  |   | 2025-09-16 14:50:21.868 | timeago.format(d.updated_at, datetime.now(pytz.utc)) |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/blocks.py", line 336, in _build_sources_blocks |  
  |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |  
  |   | 2025-09-16 14:50:21.868 | citations_block = _build_sources_blocks(cited_documents=cited_docs) |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/blocks.py", line 409, in _build_citations_blocks |  
  |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |  
  |   | 2025-09-16 14:50:21.868 | citations_blocks = _build_citations_blocks(answer) |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/blocks.py", line 639, in build_slack_response_blocks |  
  |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |  
  |   | 2025-09-16 14:50:21.868 | all_blocks = build_slack_response_blocks( |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/handlers/handle_regular_answer.py", line 385, in handle_regular_answer |  
  |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^ |  
  |   | 2025-09-16 14:50:21.868 | issue_with_regular_answer = handle_regular_answer( |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/handlers/handle_message.py", line 231, in handle_message |  
  |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^ |  
  |   | 2025-09-16 14:50:21.868 | failed = handle_message( |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/listener.py", line 991, in process_message |  
  |   | 2025-09-16 14:50:21.868 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |  
  |   | 2025-09-16 14:50:21.868 | return process_message(req, client) |  
  |   | 2025-09-16 14:50:21.868 | File "/app/onyx/onyxbot/slack/listener.py", line 1070, in process_slack_event |  
  |   | 2025-09-16 14:50:21.868 | Traceback (most recent call last): |  

2025-09-16 14:50:21.868ERROR:    09/16/2025 09:50:21 PM                    listener.py 1072: [t:b0e1892a] Failed to process slack event |   |   | 2025-09-16 14:50:21.868 | ERROR:    09/16/2025 09:50:21 PM                    listener.py 1072: [t:b0e1892a] Failed to process slack event
  |   | 2025-09-16 14:50:21.868 | ERROR:    09/16/2025 09:50:21 PM                    listener.py 1072: [t:b0e1892a] Failed to process slack event

How Has This Been Tested?

test

Backporting (check the box to trigger backport action)

Note: You have to check that the action passes, otherwise resolve the conflicts manually and tag the patches.

  • This PR should be backported (make sure to check that the backport attempt succeeds)
  • [Optional] Override Linear Check

@Weves Weves requested a review from a team as a code owner September 16, 2025 23:16
@vercel
Copy link

vercel bot commented Sep 16, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
internal-search Ready Ready Preview Comment Sep 17, 2025 6:02am

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR fixes a critical timezone handling bug in the Slack bot that was causing TypeError: can't subtract offset-naive and offset-aware datetimes when formatting document timestamps for display in Slack messages. The error occurred in the timeago.format() function when it tried to subtract a timezone-naive d.updated_at datetime from a timezone-aware datetime.now(pytz.utc) datetime.

The fix introduces a new helper function _format_doc_updated_at() that centralizes timezone handling logic. This function checks if a datetime is timezone-naive (no timezone info) and converts it to UTC if needed, or converts timezone-aware datetimes to UTC before passing them to timeago.format(). The helper function is now used in two places where the error was occurring: _build_documents_blocks() and _build_sources_blocks().

The solution follows the codebase's principles by:

  • Creating a reusable function to avoid code duplication
  • Ensuring consistent timezone handling across the application
  • Following the "fail loudly" principle by properly handling timezone conversion rather than silently failing
  • Using explicit type annotations and clear function documentation

Comprehensive unit tests were added to verify the fix works correctly with both timezone-naive and timezone-aware datetime objects, ensuring the bug won't reoccur.

Confidence score: 5/5

  • This PR is safe to merge with minimal risk as it fixes a critical runtime error without changing core functionality
  • Score reflects a straightforward bug fix with proper testing and defensive programming practices
  • No files require special attention as the changes are well-contained and thoroughly tested

3 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

Copy link
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.

2 issues found across 3 files

Prompt for AI agents (all 2 issues)

Understand the root cause of the following 2 issues and fix them.


<file name="backend/tests/unit/onyx/onyxbot/test_slack_blocks.py">

<violation number="1" location="backend/tests/unit/onyx/onyxbot/test_slack_blocks.py:61">
Hard-coded relative time in test makes it time-dependent and flaky; compute expected text dynamically using timeago.format and current UTC time.</violation>

<violation number="2" location="backend/tests/unit/onyx/onyxbot/test_slack_blocks.py:62">
Brittle, time-dependent assertion: exact match on relative time string will fail as time passes. Make the assertion robust (e.g., prefix/suffix check) or freeze now.</violation>
</file>

React with 👍 or 👎 to teach cubic. Mention @cubic-dev-ai to give feedback, ask questions, or re-run the review.

assert len(blocks) >= 2
section_block = blocks[1].to_dict()
# NOTE: will need to be updated after another year.
expected_text = "<https://example.com|Example Doc>\n_Updated 1 year ago_\n>"
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Sep 16, 2025

Choose a reason for hiding this comment

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

Hard-coded relative time in test makes it time-dependent and flaky; compute expected text dynamically using timeago.format and current UTC time.

Prompt for AI agents
Address the following comment on backend/tests/unit/onyx/onyxbot/test_slack_blocks.py at line 61:

<comment>Hard-coded relative time in test makes it time-dependent and flaky; compute expected text dynamically using timeago.format and current UTC time.</comment>

<file context>
@@ -0,0 +1,67 @@
+    assert len(blocks) &gt;= 2
+    section_block = blocks[1].to_dict()
+    # NOTE: will need to be updated after another year.
+    expected_text = &quot;&lt;https://example.com|Example Doc&gt;\n_Updated 1 year ago_\n&gt;&quot;
+    assert section_block[&quot;text&quot;][&quot;text&quot;] == expected_text
+
</file context>

✅ Addressed in f836312

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I agree with Cubic, might be better if we make it dynamic instead of hard coding the time to 1 year?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks! Agreed—I'll update the test to compute the expected relative time dynamically rather than hard-coding “1 year.”

Copy link
Contributor

@jessicasingh7 jessicasingh7 left a comment

Choose a reason for hiding this comment

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

LGTM! Includes some of the change I also had it in my Slack PR so makes sense!

assert len(blocks) >= 2
section_block = blocks[1].to_dict()
# NOTE: will need to be updated after another year.
expected_text = "<https://example.com|Example Doc>\n_Updated 1 year ago_\n>"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I agree with Cubic, might be better if we make it dynamic instead of hard coding the time to 1 year?

@Weves Weves merged commit 339ad91 into main Sep 17, 2025
51 of 53 checks passed
@Weves Weves deleted the slackbot-fix branch September 17, 2025 06:25
justin-tahara pushed a commit that referenced this pull request Sep 17, 2025
razvanMiu pushed a commit to eea/danswer that referenced this pull request Oct 16, 2025
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.

2 participants