apify/crawlee · error · Error

The "value" parameter cannot be stringified to JSON: ${error

Error message

The "value" parameter cannot be stringified to JSON: ${error.message}

What it means

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.

Source

Thrown at packages/core/src/storages/key_value_store_codec.ts:59

            contentType: 'application/octet-stream',
        };
    }

    if (typeof value === 'string') {
        return { value, contentType: 'text/plain; charset=utf-8' };
    }

    let serialized: string;
    try {
        // Format JSON to simplify debugging, the overheads with compression is negligible
        serialized = jsonStringifyExtended(value as Dictionary, null, 2);
    } catch (e) {
        const error = e as Error;
        // Give more meaningful error message
        if (error.message?.includes('Invalid string length')) {
            error.message = 'Object is too large';
        }
        throw new Error(`The "value" parameter cannot be stringified to JSON: ${error.message}`);
    }

    if (serialized === undefined) {
        throw new Error(
            'The "value" parameter was stringified to JSON and returned undefined. ' +
                "Make sure you're not trying to stringify an undefined value.",
        );
    }

    return { value: serialized, contentType: 'application/json; charset=utf-8' };
}

/**
 * Parses a Buffer or ArrayBuffer using the provided content type header.
 *
 * - application/json is returned as a parsed object.
 * - application/*xml and text/* are returned as strings.
 * - everything else is returned as original body.

View on GitHub (pinned to dbe57fb09c)

Solutions

  1. Remove circular references before storing (e.g. JSON.parse(JSON.stringify()) with a replacer, or a library like flatted)
  2. Convert BigInt values to string/number before storing
  3. Split very large values into chunks or store them under multiple keys to avoid string-length limits
  4. If the cause says 'Object is too large', restructure the data rather than enlarging it

Example fix

// before
await store.setValue('stats', { count: 10n, self }); // BigInt / circular
// after
await store.setValue('stats', { count: Number(10n), parentName: self.parentName });
Defensive patterns

Strategy: type-guard

Validate before calling

function isJsonSafe(value, seen = new WeakSet()) {
  if (value === null || typeof value !== 'object') return typeof value !== 'bigint';
  if (seen.has(value)) return false;
  seen.add(value);
  return Object.values(value).every((v) => isJsonSafe(v, seen));
}

Type guard

const isJsonSafe = (value, seen = new WeakSet()) => {
  if (value === null || typeof value !== 'object') return typeof value !== 'bigint' && typeof value !== 'function' && typeof value !== 'symbol';
  if (seen.has(value)) return false; // circular
  seen.add(value);
  return Object.values(value).every((v) => isJsonSafe(v, seen));
};

Try / catch

try {
  await store.setValue(key, value);
} catch (e) {
  if (e.message.includes('cannot be stringified to JSON')) {
    console.error('Value is not JSON-serializable:', e.message);
    await store.setValue(key, JSON.stringify(value, safeReplacer())); // sanitize and retry
  } else throw e;
}
function 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; }; }

Prevention

When it happens

Trigger: 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).

Common situations: Storing crawler state objects that accidentally reference the Request object graph (circular); storing BigInt statistics counters; persisting enormous accumulated result arrays.

Related errors


AI-assisted analysis of apify/crawlee@dbe57fb09c (2026-08-30). Data as JSON: /api/errors/8b9dc9169780b431. Report an issue: GitHub.