fix: surface xAI API errors instead of silently returning empty results#155
Open
km-git007 wants to merge 3 commits intomvanhorn:mainfrom
Open
fix: surface xAI API errors instead of silently returning empty results#155km-git007 wants to merge 3 commits intomvanhorn:mainfrom
km-git007 wants to merge 3 commits intomvanhorn:mainfrom
Conversation
added 3 commits
April 3, 2026 19:23
…ults When Codex API returns an empty or malformed SSE response, the parsing function was silently returning an empty dict, which then resulted in zero Reddit search results being delivered to the user with no error indication. This is a silent failure scenario where: - User runs /last30days with Codex auth configured - API returns empty/malformed response (network error, invalid format) - Code silently treats it as 'search found zero results' - User receives degraded research output with no error message The fix adds explicit error handling: - Raise HTTPError if SSE stream is completely empty (no events parsed) - Raise HTTPError if stream has events but produces no output text - These exceptions propagate through search_reddit() which already has proper exception handling that sets reddit_error and surfaces it to UI The exceptions are caught by the existing error handlers at: - search_reddit() line 312-320 (Codex auth path) - search_reddit() line 341-350 (API key fallback path) Both paths set reddit_error which bubbles up to the orchestrator and is displayed to the user via progress.show_error(). Tests updated: - test_codex_auth.py: Updated test_empty_stream to expect HTTPError - test_codex_auth.py: Added test_malformed_stream_no_output to cover the case where SSE events exist but produce no output text All 23 existing Codex auth tests continue to pass.
When xAI API returns an empty or malformed response (missing output text, invalid JSON structure, malformed JSON syntax), the parse_x_response() function was silently returning an empty items list, resulting in zero X search results delivered to the user with no error indication. This is a silent failure scenario where: - User runs /last30days with xAI_API_KEY configured - API returns empty/malformed response (network error, invalid format) - Code silently treats it as 'search found zero results' - User receives degraded research output with no error message The fix adds explicit error handling: - Raise HTTPError if output text is empty (no results or API failure) - Raise HTTPError if JSON parsing fails (malformed structure) - Raise HTTPError if output text lacks items key (wrong response format) - These exceptions propagate through search_x() which already has proper exception handling that sets x_error and surfaces it to UI The exceptions are caught by the existing error handlers in last30days.py (lines 393-398) which set x_error that bubbles up to the orchestrator and is displayed to the user via progress.show_error(). This mirrors the fix applied to Codex auth path in commit 7c9fd96.
Move parse_x_response() call inside the try-except block so that HTTPError exceptions raised by the parsing function (due to malformed API responses) are properly caught and x_error is set. This ensures that parsing errors from empty/malformed xAI responses are surfaced to the user instead of propagating uncaught.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
The xAI X (Twitter) search pipeline was silently discarding malformed or empty API responses. This caused users to receive incomplete research outputs without any error notification.
When the xAI API returned a
200 OKresponse with invalid or incomplete JSON (e.g., missing output text, malformed structure, or a missing"items"key), the parser would return an empty items list. Consequently, the orchestrator would incorrectly treat this as "zero relevant results found" rather than an "API failure."Why
The root cause was that
parse_x_response()returned early on malformed responses without raising exceptions. This violated the error-propagation contract; callers could not distinguish between a successful search with no results and an API that returned garbage data.Note
This fix mirrors the approach taken in commit
7c9fd96for the Codex authentication path (Reddit), ensuring consistency across our search parsers.How
The fix implements the following changes:
xai_x.py: Modifiedparse_x_response()to explicitly raise anHTTPErrorwhen:"items"key.last30days.py: Wrapped theparse_x_response()call inside the existingtry-exceptblock. This ensures parsing errors propagate as anx_errorto the UI, informing the user of the failure.Testing
HTTPError.x_errorstate correctly.Notes for Reviewer
7c9fd96(Codex path fix) for architectural consistency.BirdandScrapeCreatorsX search backends share this "parsing-outside-try-except" pattern; however, they remain untouched in this PR to keep the scope focused on xAI.progress.show_error()channel. No new UI components were required.