JuliusBrussee/caveman · error

tool search response tools must be an array

Error message

tool search response tools must be an array

What it means

The client validates the tool search response shape: data.tools must be an array. If the server returned a malformed payload (missing tools field or non-array), this error is thrown instead of failing obscurely later.

Solutions

  1. Verify SDK and server API versions are compatible
  2. Confirm the tool-search endpoint URL is correct
  3. Check the raw response body from the server for an error payload
  4. Rule out proxies/interceptors rewriting the response

Example fix

// before
const data = await response.json(); // { error: "quota exceeded" } with status 200
// after
const data = await response.json();
if (!Array.isArray(data?.tools)) throw new Error(`unexpected tool search response: ${JSON.stringify(data).slice(0, 200)}`);
Defensive patterns

Strategy: type-guard

Validate before calling

const res = await fetch(searchUrl, { method: "POST", body });
const text = await res.text();
let data: unknown;
try { data = JSON.parse(text); } catch { throw new Error(`tool search returned non-JSON: ${text.slice(0,100)}`); }
if (!(data !== null && typeof data === "object" && Array.isArray((data as any).tools))) throw new Error("server response missing tools array");

Type guard

function hasToolsArray(v: unknown): v is { tools: unknown[] } & Record<string, unknown> {
  return typeof v === "object" && v !== null && Array.isArray((v as Record<string, unknown>).tools);
}

Try / catch

try {
  const result = await searchTools(cave, catalog, query);
} catch (e) {
  if (/tools must be an array/.test(e.message)) failoverToLocalRanker(catalog, query);
  else throw e;
}

Prevention

When it happens

Trigger: Tool-search endpoint returns JSON without a tools array — e.g. an error object, a null, or a differently-versioned API response.

Common situations: Hitting a wrong/legacy endpoint URL; API version mismatch between SDK and server; a proxy returning an HTML/JSON error body with 200 status; response.json() parsing an unexpected payload.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/0f12800162e285b7. Report an issue: GitHub.

Appendix: source

Thrown at packages/sdk/typescript/src/index.ts:1630

  const data = await response.json();

  // Map snake_case response to camelCase
  const rawSent = strictNonNegativeInt(data.sent_schema_tokens);
  const rawFull = strictNonNegativeInt(data.full_schema_tokens);
  const validCounts = rawSent !== null && rawFull !== null && rawSent <= rawFull;
  const sentSchemaTokens = validCounts ? rawSent : 0;
  const fullSchemaTokens = validCounts ? rawFull : 0;
  const savedTokens = fullSchemaTokens - sentSchemaTokens;
  const reductionPct = fullSchemaTokens === 0 ? 0 : Math.round((savedTokens / fullSchemaTokens) * 1000) / 10;

  const catalogByName = new Map<string, CaveTool>();
  for (const tool of catalog) {
    if (typeof tool.name !== "string" || tool.name.trim() === "" || catalogByName.has(tool.name)) {
      throw new Error("tool catalog names must be non-empty and unique");
    }
    catalogByName.set(tool.name, tool);
  }
  if (!Array.isArray(data.tools)) throw new Error("tool search response tools must be an array");
  const returnedNames = new Set<string>();
  const selectedTools = data.tools.map((wire: unknown) => {
    const name = wire !== null && typeof wire === "object" && typeof (wire as Record<string, unknown>)["name"] === "string"
      ? (wire as Record<string, unknown>)["name"] as string
      : "";
    const local = catalogByName.get(name);
    if (!local || returnedNames.has(name)) throw new Error("tool search response contained an unknown or duplicate tool");
    returnedNames.add(name);
    return local;
  });

  return {
    sessionId: typeof data.session_id === "string" ? data.session_id : undefined,
    tools: selectedTools,
    sentSchemaTokens,
    fullSchemaTokens,
    deferredCount: strictNonNegativeInt(data.deferred_count) ?? 0,
    method: data.method ?? "unknown",

View on GitHub (pinned to 3ee70a1026)