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:
committed by
Trevin Chow
parent
170b570cbc
commit
d51e91ea26
@@ -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 = []
|
||||
|
||||
Reference in New Issue
Block a user