JuliusBrussee/caveman · error · CaveRequestError
cave artifact was not valid JSON
Error message
cave artifact was not valid JSON
What it means
artifactGet parses the artifact body with JSON.parse(await response.text()) and wraps any parse failure in CaveRequestError(status, path, "cave artifact was not valid JSON"). Unlike the object-request helper, artifacts may legitimately be any JSON value, but they must still be valid JSON — a non-JSON or truncated body triggers this error.
Solutions
- Re-upload the artifact and retry the get to rule out transient truncation
- Fetch the artifact URL manually (curl) and inspect the raw bytes to find where the JSON breaks
- Verify no proxy/gateway is rewriting, compressing, or chunk-corrupting response bodies
- If artifacts are written by a custom pipeline, validate JSON.parse(store) at write time
Example fix
// before
await fetch(uploadUrl, { method: 'PUT', body: maybeStringified }); // partial body persisted
// after
const payload = JSON.stringify(data);
JSON.parse(payload); // validate before storing
await fetch(uploadUrl, { method: 'PUT', body: payload }); Defensive patterns
Strategy: retry
Validate before calling
// at write time: const payload = JSON.stringify(data); JSON.parse(payload); // throws before upload if data is not serializable
Try / catch
try { return await sdk.getArtifact(id, wf); } catch (e) { if (e instanceof CaveRequestError && e.message.includes('artifact was not valid JSON')) { await reupload(id, wf); return sdk.getArtifact(id, wf); } throw e; } Prevention
- Validate artifacts with JSON.parse immediately after upload (read-back check)
- Detect interrupted uploads server-side (checksums/content-length)
- Bypass intermediaries for large artifact bodies or verify no rewriting occurs
- Seed dev/test artifacts only via the SDK upload path, never raw files
When it happens
Trigger: Artifact upload was corrupted or truncated (partial write, connection drop mid-upload); storage layer serving raw binary or an empty string; an intermediary returning an error page with 200 kept alive; non-JSON artifact bytes stored through another tool.
Common situations: Network interruption during artifact upload leading to partial body on disk; proxy compressing/encoding the body in a way JSON.parse cannot read; hand-seeded artifact data in a local dev Cave that was never valid JSON.
Understand the failure class
Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.
Related errors
- cave response was not valid JSON
- local learn snapshot is unreadable at
- AbortError
- array not closed
- artifact_id is required
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/b515fb36e7757e58.
Report an issue: GitHub.
Appendix: source
Thrown at packages/sdk/typescript/src/index.ts:2157
}
return decoded as Record<string, unknown>;
}
async function artifactGet(cave: Cave, artifactId: string, workflow: string, trace: TraceContext): Promise<unknown> {
if (typeof artifactId !== "string" || artifactId.trim() === "") throw new Error("artifactId is required");
const path = `/sdk/v1/artifacts/${encodeURIComponent(artifactId)}`;
const response = await caveFetch(cave, `${cave.options.baseURL}${path}`, {
method: "GET",
headers: headers(cave, workflow, undefined, undefined, trace)
});
if (!response.ok) {
discardResponse(response);
throw new CaveRequestError(response.status, path, `cave request failed (${response.status})`);
}
try {
return JSON.parse(await response.text());
} catch {
throw new CaveRequestError(response.status, path, "cave artifact was not valid JSON");
}
}
function headers(cave: Cave, workflow: string, upstreamKey?: string, hint?: Record<string, unknown>, trace?: TraceContext) {
return {
"content-type": "application/json",
authorization: `Bearer ${cave.options.apiKey}`,
"x-cave-agent": cave.options.agent,
"x-cave-workflow": workflow,
"x-cave-retention": cave.options.retention ?? "metadata",
...(cave.options.user ? { "x-cave-user-hash": cave.options.user } : {}),
...(upstreamKey ? { "x-cave-upstream-key": upstreamKey } : {}),
...(hint?.["latencyClass"] !== undefined ? { "x-cave-async": String(hint["latencyClass"] !== "interactive") } : {}),
...(typeof hint?.["toolSessionId"] === "string" ? { "x-cave-tool-session": hint["toolSessionId"] as string } : {}),
...(typeof hint?.["assemblyHeader"] === "string" ? { "x-cave-assembly": hint["assemblyHeader"] as string } : {}),
// Trace continuity: only requests made through a CaveTrace carry these, so
// the gateway row's span parents onto the SDK's root span.
...(trace ? { "x-cave-trace-id": trace.traceId, "x-cave-parent-span-id": trace.parentSpanId } : {})View on GitHub (pinned to 3ee70a1026)