fix(xai): surface API errors instead of silently returning empty results

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 <km.git007@gmail.com>
This commit is contained in:
Kaustav Mishra
2026-05-17 02:26:54 -07:00
committed by Trevin Chow
parent 170b570cbc
commit d51e91ea26
+15 -7
View File
@@ -175,16 +175,24 @@ def parse_x_response(response: Dict[str, Any]) -> List[Dict[str, Any]]:
break break
if not output_text: 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 # Extract JSON from the response
json_match = re.search(r'\{[\s\S]*"items"[\s\S]*\}', output_text) json_match = re.search(r'\{[\s\S]*"items"[\s\S]*\}', output_text)
if json_match: if not json_match:
try: raise http.HTTPError(
data = json.loads(json_match.group()) f"xAI API returned output without valid JSON items structure (output: {output_text[:200]})"
items = data.get("items", []) )
except json.JSONDecodeError: try:
_log(f"Failed to parse xAI response JSON: {output_text[:200]}") 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 # Validate and clean items
clean_items = [] clean_items = []