Skip to content

Lambda isnt returning my body?

0

In android studio i am passing two prompts over to my aws lambda function in order to do multi shot with nova reels. Everything seems right but i get the error below.

2025-05-21 13:56:51.261 26010-26233 LambdaInvokeNova com.example.myroots8 D Received response from API 2025-05-21 13:56:51.262 26010-26233 LambdaInvokeNova com.example.myroots8 D Response body: {"invocationArn": "arn:aws:bedrock:us-east-1:145023101762:async-invoke/4mjga2zmc83p", "requestId": null, "futureObjectKey": "4mjga2zmc83p/output.mp4"} 2025-05-21 13:56:51.263 26010-26233 LambdaInvokeNova com.example.myroots8 D Success: {"invocationArn": "arn:aws:bedrock:us-east-1:145023101762:async-invoke/4mjga2zmc83p", "requestId": null, "futureObjectKey": "4mjga2zmc83p/output.mp4"} 2025-05-21 13:56:51.265 26010-26233 LambdaInvokeNova com.example.myroots8 E Error parsing JSON: No value for body (Ask Gemini) org.json.JSONException: No value for body at org.json.JSONObject.get(JSONObject.java:398) at org.json.JSONObject.getString(JSONObject.java:559) at com.example.myroots8.VcActivity$onCreate$3$1$1.onResponse(VcActivity.kt:205) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:529) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) at java.lang.Thread.run(Thread.java:1012)

Here is my Android studio code in kotlin:

if (response.isSuccessful) { Log.d("LambdaInvokeNova", "Success: $responseBody")

                            try {

                                // New and improved. WORKS for futureObjectKey
                                // Parse the JSON response
                                val jsonResponse = JSONObject(responseBody)


                                
                                // Extract the body string and parse it as JSON
                                val bodyString = jsonResponse.getString("body")
                                

                                val bodyJson = JSONObject(bodyString)

                                // Now extract the futureObjectKey from the body JSON
                                if (bodyJson.has("futureObjectKey")) {
                                    val futureObjectKey = bodyJson.getString("futureObjectKey")
                                   // Log.d(
                                     //   "LambdaInvokeNova",
                                       // "futureObjectKey: $futureObjectKey"
                                    //)

                                    // below is letting our video get created
                                    try {
                                        //Log.d(
                                          //  "LambdaInvokeNova",
                                            //"Zzzz... started sleeping. Will wake up in 2 1/2 minutes when video is ready..Zzz"
                                        //)
                                        //Thread.sleep(240000) // Sleep (block) the current thread for 4 minutes.
                                        Thread.sleep(150000) // Sleep (block) the current thread for 2 minutes 30 seconds.
                                    } catch (e: InterruptedException) {
                                        e.printStackTrace() // Handle potential interruption
                                    }

Here is my AWS Lambda function code in python 3.3:

import json import boto3

bedrock_runtime = boto3.client("bedrock-runtime")

def lambda_handler(event, context):

body = json.loads(event['body'])
prompt = body['prompt']
prompt2 = body['prompt2']

model_input = {
"taskType": "MULTI_SHOT_MANUAL",
"multiShotManualParams": {
    "shots": [
        {"text": prompt},
        {"text": prompt2}
        
    ]
},
"videoGenerationConfig": {
    "seed": 33,
    "fps": 24,  # Must be 24
    "dimension": "1280x720",  # Must be "1280x720"
},

}

try:
    # Start the asynchronous video generation job
    invocation = bedrock_runtime.start_async_invoke(
        modelId="amazon.nova-reel-v1:1",
        modelInput=model_input,
        outputDataConfig={
            "s3OutputDataConfig": {
                "s3Uri": "s3://novabucket"
            }
        }
    )

    # Extract the invocation ID and construct the future object key
    invocation_id = invocation['invocationArn'].split('/')[-1]
    future_object_key = f"{invocation_id}/output.mp4"

    return {
        "statusCode": 200,
        "body": json.dumps({
            "invocationArn": invocation.get('invocationArn'),
            "requestId": invocation.get('requestId'),
            "futureObjectKey": f"{invocation_id}/output.mp4"
        })
    }

except Exception as e:
    message = str(e)
    return {
        "statusCode": 500,
        "body": json.dumps({"error": message})
    }

asked a year ago176 views

1 Answer
0

The issue you're experiencing is due to a mismatch between how the Lambda function is returning the response and how your Android code is trying to parse it. Your Lambda function is correctly returning a JSON object with a "body" field, but your Android code is trying to parse the entire response as if it were the body itself.

To fix this, you should modify your Android code to directly use the response body without trying to extract a nested "body" field. Here's how you can adjust your Kotlin code:

  1. Remove this line: val bodyString = jsonResponse.getString("body")

  2. Replace it with: val bodyJson = jsonResponse

  3. Then proceed with extracting the futureObjectKey as you were doing before: if (bodyJson.has("futureObjectKey")) { val futureObjectKey = bodyJson.getString("futureObjectKey") // Use futureObjectKey as needed }

This change should resolve the "No value for body" error you're seeing. The Lambda function is already returning the data you need in the top-level JSON object, so there's no need to look for a nested "body" field.

Also, make sure your OkHttp client is configured to parse the response as a JSON object. If you're using Gson or another JSON parsing library, you might need to adjust how you're converting the response to a JSONObject.

Remember to handle potential exceptions when parsing the JSON, as the response might not always be in the expected format.
Sources
Changing AWS Lambda Function prompt value with an event instead of changing environment variable. | AWS re:Post
Lambda Function not returning all properties in a response via function URL. | AWS re:Post

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.