{"record":{"id":"8b9dc9169780b431","repo":"apify/crawlee","slug":"the-value-parameter-cannot-be-stringified-to-jso","errorCode":null,"errorMessage":"The \"value\" parameter cannot be stringified to JSON: ${error.message}","messagePattern":"The \"value\" parameter cannot be stringified to JSON: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storages/key_value_store_codec.ts","lineNumber":59,"sourceCode":"            contentType: 'application/octet-stream',\n        };\n    }\n\n    if (typeof value === 'string') {\n        return { value, contentType: 'text/plain; charset=utf-8' };\n    }\n\n    let serialized: string;\n    try {\n        // Format JSON to simplify debugging, the overheads with compression is negligible\n        serialized = jsonStringifyExtended(value as Dictionary, null, 2);\n    } catch (e) {\n        const error = e as Error;\n        // Give more meaningful error message\n        if (error.message?.includes('Invalid string length')) {\n            error.message = 'Object is too large';\n        }\n        throw new Error(`The \"value\" parameter cannot be stringified to JSON: ${error.message}`);\n    }\n\n    if (serialized === undefined) {\n        throw new Error(\n            'The \"value\" parameter was stringified to JSON and returned undefined. ' +\n                \"Make sure you're not trying to stringify an undefined value.\",\n        );\n    }\n\n    return { value: serialized, contentType: 'application/json; charset=utf-8' };\n}\n\n/**\n * Parses a Buffer or ArrayBuffer using the provided content type header.\n *\n * - application/json is returned as a parsed object.\n * - application/*xml and text/* are returned as strings.\n * - everything else is returned as original body.","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/storages/key_value_store_codec.ts#L41-L77","documentation":"Before storing, serializeValue runs JSON.stringify on the value. If stringify throws (circular structures, BigInt, 'Invalid string length' for huge objects), the library rewraps the failure in this clearer error while preserving the cause message. It signals the value simply cannot be represented as JSON.","triggerScenarios":"setValue() with an object containing circular references, BigInt values, functions in strict positions that throw, or an object so deeply nested/huge that JSON.stringify exceeds the maximum string length (that case is remapped to 'Object is too large' inside this message).","commonSituations":"Storing crawler state objects that accidentally reference the Request object graph (circular); storing BigInt statistics counters; persisting enormous accumulated result arrays.","solutions":["Remove circular references before storing (e.g. JSON.parse(JSON.stringify()) with a replacer, or a library like flatted)","Convert BigInt values to string/number before storing","Split very large values into chunks or store them under multiple keys to avoid string-length limits","If the cause says 'Object is too large', restructure the data rather than enlarging it"],"exampleFix":"// before\nawait store.setValue('stats', { count: 10n, self }); // BigInt / circular\n// after\nawait store.setValue('stats', { count: Number(10n), parentName: self.parentName });","handlingStrategy":"type-guard","validationCode":"function isJsonSafe(value, seen = new WeakSet()) {\n  if (value === null || typeof value !== 'object') return typeof value !== 'bigint';\n  if (seen.has(value)) return false;\n  seen.add(value);\n  return Object.values(value).every((v) => isJsonSafe(v, seen));\n}","typeGuard":"const isJsonSafe = (value, seen = new WeakSet()) => {\n  if (value === null || typeof value !== 'object') return typeof value !== 'bigint' && typeof value !== 'function' && typeof value !== 'symbol';\n  if (seen.has(value)) return false; // circular\n  seen.add(value);\n  return Object.values(value).every((v) => isJsonSafe(v, seen));\n};","tryCatchPattern":"try {\n  await store.setValue(key, value);\n} catch (e) {\n  if (e.message.includes('cannot be stringified to JSON')) {\n    console.error('Value is not JSON-serializable:', e.message);\n    await store.setValue(key, JSON.stringify(value, safeReplacer())); // sanitize and retry\n  } else throw e;\n}\nfunction safeReplacer() { const seen = new WeakSet(); return (k, v) => { if (typeof v === 'bigint') return v.toString(); if (typeof v === 'object' && v !== null) { if (seen.has(v)) return '[Circular]'; seen.add(v); } return v; }; }","preventionTips":["Keep BigInt out of stored state (convert to string/number)","Avoid storing objects that reference crawler/request internals (circular graphs)","Store large datasets incrementally under multiple keys instead of one giant value"],"tags":["serialization","json","key-value-store"],"backgroundTag":"json-serialization-failed","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}