API / combine-collections-search-with-web-and-x-search

API

Combine collections search with web and X search

Combine collections search with web and X search

Give Grok your collection and live web / X tools in one chat. It pulls figures from your uploads first, then checks public sources, then synthesizes. Basic collection chat is in Search collections with the Grok API. This page is the hybrid tool set.

Create the collection and wait until each document is processed before you chat.

xAI SDK

import os
from xai_sdk import Client
from xai_sdk.chat import user
from xai_sdk.tools import (
    code_execution,
    collections_search,
    web_search,
    x_search,
)

client = Client(api_key=os.getenv("XAI_API_KEY"))
collection_id = "collection_your_id_here"

chat = client.chat.create(
    model="grok-4.6",
    tools=[
        collections_search(collection_ids=[collection_id]),
        web_search(),
        x_search(),
        code_execution(),
    ],
)

chat.append(
    user(
        "Based on the production figures in my collection, what is current "
        "analyst sentiment on 2024–2025 vehicle production?"
    )
)

for response, chunk in chat.stream():
    for tool_call in chunk.tool_calls:
        print(tool_call.function.name, tool_call.function.arguments)
    if chunk.content:
        print(chunk.content, end="", flush=True)

print("\nCitations:", response.citations)
print("Tool usage:", response.server_side_tool_usage)

Grok decides the order. Typical pattern: several collections_search calls, then web_search / x_search, then a final answer. Collection citations use collections://collection_id/files/file_id. Web hits use normal https:// URLs.

Responses API shape

On POST https://api.x.ai/v1/responses, enable file_search with your collection id in vector_store_ids, plus web and X tools your stack already uses for live search. Pair code_interpreter when you want arithmetic on the numbers it found.

Pitfalls

  • Empty or still-processing collections — poll until DOCUMENT_STATUS_PROCESSED.
  • Management key on the chat call — search tools use the regular inference XAI_API_KEY.
  • Reading only web citations and ignoring collections:// URIs when you asked about your own filings.