From 891307f6c2b9fcc9bd96474ef075124f12acef64 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Fri, 23 Jan 2026 20:02:43 -0800 Subject: [PATCH] Fix xAI API: migrate to Agent Tools API xAI deprecated search_parameters - now requires Agent Tools API: - Changed endpoint from /v1/chat/completions to /v1/responses - Changed from messages array to input array format - Changed from search_parameters to tools: [{"type": "x_search"}] - Updated model selection to grok-4-1-fast (required for x_search tool) Co-Authored-By: Claude Opus 4.5 --- scripts/lib/models.py | 6 +++--- scripts/lib/xai_x.py | 23 ++++++++--------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/scripts/lib/models.py b/scripts/lib/models.py index 0a74fe1..78399c7 100644 --- a/scripts/lib/models.py +++ b/scripts/lib/models.py @@ -9,11 +9,11 @@ from . import cache, http OPENAI_MODELS_URL = "https://api.openai.com/v1/models" OPENAI_FALLBACK_MODELS = ["gpt-5.2", "gpt-5.1", "gpt-5", "gpt-4o"] -# xAI API +# xAI API - Agent Tools API requires grok-4 family XAI_MODELS_URL = "https://api.x.ai/v1/models" XAI_ALIASES = { - "latest": "grok-3", - "stable": "grok-3", + "latest": "grok-4-1-fast", # Required for x_search tool + "stable": "grok-4-1-fast", } diff --git a/scripts/lib/xai_x.py b/scripts/lib/xai_x.py index badd4d3..4193d58 100644 --- a/scripts/lib/xai_x.py +++ b/scripts/lib/xai_x.py @@ -13,8 +13,8 @@ def _log_error(msg: str): sys.stderr.write(f"[X ERROR] {msg}\n") sys.stderr.flush() -# xAI uses chat completions endpoint -XAI_CHAT_URL = "https://api.x.ai/v1/chat/completions" +# xAI uses responses endpoint with Agent Tools API +XAI_RESPONSES_URL = "https://api.x.ai/v1/responses" # Depth configurations: (min, max) posts to request DEPTH_CONFIG = { @@ -91,14 +91,13 @@ def search_x( # Adjust timeout based on depth timeout = 60 if depth == "quick" else 90 if depth == "default" else 120 - # Use chat completions format with search enabled + # Use Agent Tools API with x_search tool payload = { "model": model, - "messages": [ - { - "role": "system", - "content": "You are a research assistant with access to real-time X (Twitter) data. Search X and return structured results." - }, + "tools": [ + {"type": "x_search"} + ], + "input": [ { "role": "user", "content": X_SEARCH_PROMPT.format( @@ -110,15 +109,9 @@ def search_x( ), } ], - "search_parameters": { - "mode": "auto", - "sources": [{"type": "x"}], - "from_date": from_date, - "to_date": to_date, - }, } - return http.post(XAI_CHAT_URL, payload, headers=headers, timeout=timeout) + return http.post(XAI_RESPONSES_URL, payload, headers=headers, timeout=timeout) def parse_x_response(response: Dict[str, Any]) -> List[Dict[str, Any]]: