{"record":{"id":"0f12800162e285b7","repo":"JuliusBrussee/caveman","slug":"tool-search-response-tools-must-be-an-array","errorCode":null,"errorMessage":"tool search response tools must be an array","messagePattern":"tool search response tools must be an array","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/sdk/typescript/src/index.ts","lineNumber":1630,"sourceCode":"  const data = await response.json();\n\n  // Map snake_case response to camelCase\n  const rawSent = strictNonNegativeInt(data.sent_schema_tokens);\n  const rawFull = strictNonNegativeInt(data.full_schema_tokens);\n  const validCounts = rawSent !== null && rawFull !== null && rawSent <= rawFull;\n  const sentSchemaTokens = validCounts ? rawSent : 0;\n  const fullSchemaTokens = validCounts ? rawFull : 0;\n  const savedTokens = fullSchemaTokens - sentSchemaTokens;\n  const reductionPct = fullSchemaTokens === 0 ? 0 : Math.round((savedTokens / fullSchemaTokens) * 1000) / 10;\n\n  const catalogByName = new Map<string, CaveTool>();\n  for (const tool of catalog) {\n    if (typeof tool.name !== \"string\" || tool.name.trim() === \"\" || catalogByName.has(tool.name)) {\n      throw new Error(\"tool catalog names must be non-empty and unique\");\n    }\n    catalogByName.set(tool.name, tool);\n  }\n  if (!Array.isArray(data.tools)) throw new Error(\"tool search response tools must be an array\");\n  const returnedNames = new Set<string>();\n  const selectedTools = data.tools.map((wire: unknown) => {\n    const name = wire !== null && typeof wire === \"object\" && typeof (wire as Record<string, unknown>)[\"name\"] === \"string\"\n      ? (wire as Record<string, unknown>)[\"name\"] as string\n      : \"\";\n    const local = catalogByName.get(name);\n    if (!local || returnedNames.has(name)) throw new Error(\"tool search response contained an unknown or duplicate tool\");\n    returnedNames.add(name);\n    return local;\n  });\n\n  return {\n    sessionId: typeof data.session_id === \"string\" ? data.session_id : undefined,\n    tools: selectedTools,\n    sentSchemaTokens,\n    fullSchemaTokens,\n    deferredCount: strictNonNegativeInt(data.deferred_count) ?? 0,\n    method: data.method ?? \"unknown\",","sourceCodeStart":1612,"sourceCodeEnd":1648,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/sdk/typescript/src/index.ts#L1612-L1648","documentation":"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.","triggerScenarios":"Tool-search endpoint returns JSON without a tools array — e.g. an error object, a null, or a differently-versioned API response.","commonSituations":"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.","solutions":["Verify SDK and server API versions are compatible","Confirm the tool-search endpoint URL is correct","Check the raw response body from the server for an error payload","Rule out proxies/interceptors rewriting the response"],"exampleFix":"// before\nconst data = await response.json(); // { error: \"quota exceeded\" } with status 200\n// after\nconst data = await response.json();\nif (!Array.isArray(data?.tools)) throw new Error(`unexpected tool search response: ${JSON.stringify(data).slice(0, 200)}`);","handlingStrategy":"type-guard","validationCode":"const res = await fetch(searchUrl, { method: \"POST\", body });\nconst text = await res.text();\nlet data: unknown;\ntry { data = JSON.parse(text); } catch { throw new Error(`tool search returned non-JSON: ${text.slice(0,100)}`); }\nif (!(data !== null && typeof data === \"object\" && Array.isArray((data as any).tools))) throw new Error(\"server response missing tools array\");","typeGuard":"function hasToolsArray(v: unknown): v is { tools: unknown[] } & Record<string, unknown> {\n  return typeof v === \"object\" && v !== null && Array.isArray((v as Record<string, unknown>).tools);\n}","tryCatchPattern":"try {\n  const result = await searchTools(cave, catalog, query);\n} catch (e) {\n  if (/tools must be an array/.test(e.message)) failoverToLocalRanker(catalog, query);\n  else throw e;\n}","preventionTips":["Pin compatible SDK and server versions","Confirm the endpoint URL is the tool-search route","Fail over to a local ranking strategy when the response is malformed"],"tags":["api","response-validation","tool-search","unexpected-response"],"backgroundTag":"unexpected-response-shape","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}