Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

Add error handling for X/xAI API calls

Matt Van Horn9604d0ba4682
diff --git a/scripts/last30days.py b/scripts/last30days.py
index bc3ddc9a..ceae0267 100644
--- a/scripts/last30days.py
+++ b/scripts/last30days.py
@@ -133,14 +133,25 @@ def run_research(
if mock:
raw_xai = load_fixture("xai_sample.json")
else:
- raw_xai = xai_x.search_x(
- config["XAI_API_KEY"],
- selected_models["xai"],
- topic,
- from_date,
- to_date,
- depth=depth,
- )
+ try:
+ raw_xai = xai_x.search_x(
+ config["XAI_API_KEY"],
+ selected_models["xai"],
+ topic,
+ from_date,
+ to_date,
+ depth=depth,
+ )
+ except http.HTTPError as e:
+ if progress:
+ progress.show_error(f"X API failed: {e}")
+ raw_xai = {"error": str(e)}
+ x_error = f"API error: {e}"
+ except Exception as e:
+ if progress:
+ progress.show_error(f"X error: {e}")
+ raw_xai = {"error": str(e)}
+ x_error = f"{type(e).__name__}: {e}"
# Parse response
x_items = xai_x.parse_x_response(raw_xai)
diff --git a/scripts/lib/xai_x.py b/scripts/lib/xai_x.py
index a0e9dd29..2a0fe256 100644
--- a/scripts/lib/xai_x.py
+++ b/scripts/lib/xai_x.py
@@ -125,6 +125,13 @@ def parse_x_response(response: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
items = []
+ # Check for API errors first
+ if "error" in response and response["error"]:
+ error = response["error"]
+ err_msg = error.get("message", str(error)) if isinstance(error, dict) else str(error)
+ print(f"[X ERROR] xAI API error: {err_msg}", flush=True)
+ return items
+
# Try to find the output text
output_text = ""
if "output" in response: