From d51e91ea2696b67ce8f54d0e270f45479200aed7 Mon Sep 17 00:00:00 2001 From: Kaustav Mishra Date: Sun, 17 May 2026 02:26:54 -0700 Subject: [PATCH] fix(xai): surface API errors instead of silently returning empty results MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parse_x_response was returning an empty items list whenever xAI returned a 200 OK with a malformed payload — empty output text, missing "items" key, or invalid JSON. The pipeline saw "successful response with zero items" and quietly handed the user a degraded report with no indication the API had failed. Now raise http.HTTPError on each of those branches so _retrieve_stream's caller catches it and surfaces the failure in errors_by_source, giving the user a visible signal that X didn't work. Closes #155. Co-authored-by: Kaustav Mishra --- skills/last30days/scripts/lib/xai_x.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/skills/last30days/scripts/lib/xai_x.py b/skills/last30days/scripts/lib/xai_x.py index fdb044b..865698b 100644 --- a/skills/last30days/scripts/lib/xai_x.py +++ b/skills/last30days/scripts/lib/xai_x.py @@ -175,16 +175,24 @@ def parse_x_response(response: Dict[str, Any]) -> List[Dict[str, Any]]: break if not output_text: - return items + response_preview = str(response)[:200] if response else "(empty)" + raise http.HTTPError( + f"xAI API returned empty response (no output text found; response preview: {response_preview})" + ) # Extract JSON from the response json_match = re.search(r'\{[\s\S]*"items"[\s\S]*\}', output_text) - if json_match: - try: - data = json.loads(json_match.group()) - items = data.get("items", []) - except json.JSONDecodeError: - _log(f"Failed to parse xAI response JSON: {output_text[:200]}") + if not json_match: + raise http.HTTPError( + f"xAI API returned output without valid JSON items structure (output: {output_text[:200]})" + ) + try: + data = json.loads(json_match.group()) + items = data.get("items", []) + except json.JSONDecodeError: + raise http.HTTPError( + f"xAI API returned valid output but invalid JSON structure (output: {output_text[:200]})" + ) # Validate and clean items clean_items = []