{"record":{"id":"680de9213769f9e1","repo":"remotion-dev/remotion","slug":"failed-to-parse-lambda-response-stream-e","errorCode":null,"errorMessage":"Failed to parse Lambda response stream: {e}","messagePattern":"Failed to parse Lambda response stream: (.+?)","errorType":"exception","errorClass":"RemotionException","httpStatus":null,"severity":"error","filePath":"packages/lambda-python/remotion_lambda/remotionclient.py","lineNumber":465,"sourceCode":"                if depth == 0:\n                    start_index = i\n                depth += 1\n            elif char == '}':\n                depth -= 1\n                if depth == 0:\n                    objects.append(input_string[start_index : i + 1])\n        return objects\n\n    def _parse_stream(self, stream: str) -> List[Dict[str, Any]]: # Added type hints\n        \"\"\"Parses a stream of concatenated JSON objects.\"\"\"\n        json_objects = self._find_json_objects(stream)\n        parsed_objects: List[Dict[str, Any]] = []\n        for obj_str in json_objects: # Renamed obj to obj_str to avoid confusion with parsed obj\n            try:\n                parsed_objects.append(json.loads(obj_str))\n            except json.JSONDecodeError as e:\n                logger.error(\"Failed to decode JSON object from stream: %s\", obj_str)\n                raise RemotionException(\n                    f\"Failed to parse Lambda response stream: {e}\"\n                ) from e\n        return parsed_objects\n\n    def _invoke_lambda(self, function_name: str, payload: str) -> Dict[str, Any]: # Added type hints\n        \"\"\"Invokes the Remotion Lambda function and parses its response.\"\"\"\n        client = self._create_lambda_client()\n        result_raw: Optional[str] = None # Renamed to avoid confusion with `decoded_result`\n        decoded_result: Dict[str, Any] = {}\n\n        try:\n            # boto3.client('lambda').invoke returns a dictionary.\n            # 'Payload' is a StreamingBody object.\n            response: Dict[str, Any] = client.invoke(FunctionName=function_name, Payload=payload)\n            streaming_body: StreamingBody = response['Payload']\n            result_raw = streaming_body.read().decode('utf-8')\n            parsed_results = self._parse_stream(result_raw)\n            decoded_result = parsed_results[-1] if parsed_results else {}","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/remotion-dev/remotion/blob/78fe4bb3fdb5a2cd68724393d63cb223db333fa7/packages/lambda-python/remotion_lambda/remotionclient.py#L447-L483","documentation":"Raised inside _parse_stream when one of the JSON objects extracted from the Lambda response stream cannot be decoded. The Lambda returns concatenated JSON chunks (progress + final result); each chunk must be individually valid JSON. The offending raw chunk is logged at error level before the raise.","triggerScenarios":"Lambda returns a partial/truncated chunk due to a network interruption or a function timeout that cut the response mid-object; an unexpected non-JSON error page from an API Gateway/proxy; a malformed chunk produced by a Lambda runtime crash.","commonSituations":"Lambda hits the 15-minute timeout and the response is truncated; an upstream proxy injects HTML/JSON error text; cold-start crashes emit stack traces mixed with JSON; mismatched Remotion Lambda version emitting an unexpected schema.","solutions":["Check CloudWatch Logs for the Lambda function to see the raw response and any crash.","Increase Lambda memory/timeout if the render is hitting resource limits and truncating output.","Ensure you are calling the function directly (not via API Gateway) so the raw streamed response is preserved.","Update the Python client and the Lambda function to matching Remotion versions."],"exampleFix":"// before - lambda function times out mid-render\nclient.render_media(comp, input_props={'huge': 'data'})  # truncates response\n\n# after\n# bump Lambda memory/timeout\naws lambda update-function-configuration --function-name remotion-render-... --memory-size 2048 --timeout 900\nclient.render_media(comp, input_props={'huge': 'data'})","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from time import sleep\nfor attempt in range(3):\n    try:\n        result = client.render_media(...)\n        break\n    except RemotionException as e:\n        if 'Failed to parse Lambda response stream' in str(e) and attempt < 2:\n            sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Use sufficient Lambda memory and timeout so the response is not truncated.","Invoke the Lambda directly, not via API Gateway/proxy.","Keep Python client and Lambda function on matching Remotion versions."],"tags":["python","lambda","json","response-parsing","aws"],"backgroundTag":null,"analyzedSha":"78fe4bb3fdb5a2cd68724393d63cb223db333fa7","analyzedAt":"2026-08-12T17:18:50.444Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}