{"record":{"id":"4f02f5f39446d58e","repo":"remotion-dev/remotion","slug":"invalid-json-type-asstring","errorCode":null,"errorMessage":"Invalid JSON (${type}): ${asString}","messagePattern":"Invalid JSON \\((.+?)\\): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/lambda-client/src/call-lambda-streaming.ts","lineNumber":31,"sourceCode":"\tStreamingMessage,\n} from '@remotion/serverless-client';\nimport {\n\tformatMap,\n\tmakeStreamer,\n\tmessageTypeIdToMessageType,\n} from '@remotion/serverless-client';\nimport {getLambdaClient} from './aws-clients';\nimport type {AwsRegion} from './regions';\n\nconst STREAM_STALL_TIMEOUT = 30000;\nconst LAMBDA_STREAM_STALL = `AWS did not invoke Lambda in ${STREAM_STALL_TIMEOUT}ms`;\n\nexport const parseJsonOrThrowSource = (data: Uint8Array, type: string) => {\n\tconst asString = new TextDecoder('utf-8').decode(data);\n\ttry {\n\t\treturn JSON.parse(asString);\n\t} catch {\n\t\tthrow new Error(`Invalid JSON (${type}): ${asString}`);\n\t}\n};\n\nconst invokeStreamOrTimeout = async <Provider extends CloudProvider>({\n\tregion,\n\ttimeoutInTest,\n\tfunctionName,\n\ttype,\n\tpayload,\n}: {\n\tregion: Provider['region'];\n\ttimeoutInTest: number;\n\tfunctionName: string;\n\ttype: string;\n\tpayload: Record<string, unknown>;\n}) => {\n\tconst resProm = getLambdaClient(\n\t\tregion as AwsRegion,","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/remotion-dev/remotion/blob/78fe4bb3fdb5a2cd68724393d63cb223db333fa7/packages/lambda-client/src/call-lambda-streaming.ts#L13-L49","documentation":"Thrown by parseJsonOrThrowSource() in @remotion/lambda-client while parsing a streaming Lambda response chunk that could not be JSON.parsed. The function decodes the raw Uint8Array chunk to UTF-8 and attempts JSON.parse; on failure it throws with the offending decoded string and the message type. Lambda streaming (InvokeWithResponseStream) is known to occasionally drop part of the JSON payload; the surrounding code intends to retry on that, but note: the retry trigger compares against 'Cannot parse Lambda response as JSON' (INVALID_JSON_MESSAGE) which does not match this thrown message, so the automatic retry does NOT catch this specific error in the current source.","triggerScenarios":"A streamed response chunk arrives truncated or with stray bytes (AWS streaming bug), the function returns non-JSON output (e.g. an unexpected error page or partial frame), or a network issue corrupts the stream mid-chunk.","commonSituations":"High concurrency / throttling causing AWS to truncate the stream; an older Remotion Lambda runtime that emits a different wire format; intermittent AWS networking issues; the renderer function crashing mid-stream and emitting non-JSON.","solutions":["Retry the render/still at the caller level (wrap renderMediaOnLambda in your own retry).","Upgrade @remotion/lambda and re-deploy the function so client and runtime wire formats match.","Reduce concurrency or stagger renders if you see this alongside throttling.","Inspect the decoded payload snippet in the error message to identify whether it is a partial JSON or an entirely different (error) body."],"exampleFix":"// before\nconst result = await renderMediaOnLambda({ ... }); // throws Invalid JSON on transient stream drop\n\n// after\nasync function withRetry<T>(fn: () => Promise<T>, retries = 3) {\n  for (let i = 0; i <= retries; i++) {\n    try { return await fn(); }\n    catch (e) { if (i === retries || !String((e as Error).message).includes('Invalid JSON')) throw e; }\n  }\n  throw new Error('unreachable');\n}\nconst result = await withRetry(() => renderMediaOnLambda({ ... }));","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"async function retryOnBadJson<T>(fn: () => Promise<T>, retries = 3): Promise<T> {\n  for (let i = 0; i <= retries; i++) {\n    try { return await fn(); }\n    catch (e) {\n      const msg = String((e as Error).message);\n      if (i === retries || !msg.includes('Invalid JSON')) throw e;\n    }\n  }\n  throw new Error('unreachable');\n}","preventionTips":["Wrap render/still calls in a small retry loop because AWS streaming can drop chunks.","Keep @remotion/lambda client and deployed function on the same version.","Reduce concurrency if Invalid JSON errors cluster with throttling."],"tags":["lambda-client","streaming","json","aws-lambda","transient","retry"],"backgroundTag":null,"analyzedSha":"78fe4bb3fdb5a2cd68724393d63cb223db333fa7","analyzedAt":"2026-08-12T17:18:50.444Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}