Skip to content

Bedrock Agent Action Group Not Passing Objects/Arrays as Valid JSON to Lambda

3

I'm encountering an issue with a Bedrock Agent where object and array parameters from an action group are not being passed to the backing Lambda function in a valid JSON format.

I have an action group defined with the following OpenAPI schema for the requestBody:

{
  "requestBody": {
    "required": true,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "userInfo": {
              "type": "object",
              "description": "User's personal information.",
              "properties": {
                "name": {
                  "type": "string",
                  "example": "test_name"
                },
                "title": {
                  "type": "string",
                  "example": "test_title"
                }
              },
              "required": ["name", "titlel"]
            },
            "tags": {
              "type": "array",
              "description": "Tags associated with the user.",
              "items": {
                "type": "string"
              },
              "example": ["customer", "vip"]
            }
          },
          "required": ["userInfo", "tags"]
        }
      }
    }
  }
}

When the Lambda function is invoked, the event contains the parameters as malformed strings, where keys and values are not enclosed in double quotes.

For the userInfo object, I receive a string like this: "{name:test_name, title:test_title}"

And for the tags array, I receive: "[customer, vip]"

My desired format, which is a valid JSON string that json.loads() can parse, is: {"name":"test_name", "title":"test_title"} ["customer", "vip"]

Interestingly, if the Lambda function fails due to a format error and is retried, the parameters are sometimes passed in the correct, parseable JSON format on the subsequent attempt.

I have tried modifying the prompt to instruct the model to "please enclose parameters in double quotes," but this has not resolved the issue.

Is there a way to ensure that the Bedrock Agent consistently passes objects and arrays as properly formatted JSON strings to the Lambda function on the first attempt? Are there any known workarounds for this behavior?

2 Answers
0

We ran into the same issue. This documentation indicates that non-OpenAPI-defined action groups do not support nested objects. I suspect that action groups defined using an OpenAPI schema are subject to the same limitations, even though this is not documented at all. Ideally, Bedrock would also provide along the raw content generated by the agent so we could parse it in the application.

answered 9 months ago

  • Is there any valid solution for this issue of aws bedrock agent not able to produce valid json which would work every time. Updating instruction in agent and orchestration also does not help. Currently I have to wait for agent to retry and hope that it somehow decides to create a valid json. If not then the whole flow breaks. What is the point of giving user the option to add object/array type body parametrs if it is not working

-3

This is a known issue with Bedrock Agents when passing complex data types like objects and arrays to Lambda functions. The agent sometimes formats these parameters as strings with improper JSON formatting, making them difficult to parse in your Lambda function.

Here are some workarounds you can implement:

  1. Add defensive parsing in your Lambda function: Create a helper function that attempts to clean and properly format the malformed JSON strings before parsing them. For example:
def clean_json_string(json_str):
if isinstance(json_str, str):
# Replace unquoted keys and values with properly quoted ones
import re
# Add quotes to keys
json_str = re.sub(r'(\w+):', r'"\1":', json_str)
# Add quotes to string values that aren't already quoted
json_str = re.sub(r': *(\w+)', r': "\1"', json_str)
return json.loads(json_str)
return json_str
  1. Use string parameters and parse them in your Lambda: Instead of defining complex object types in your OpenAPI schema, use string parameters and have your Lambda function parse them. This gives you more control over the parsing logic.

  2. Simplify your schema: Break down complex objects into individual string parameters where possible, which tend to be handled more reliably.

  3. Wrap arrays in objects: Instead of using top-level arrays, wrap them in objects, which sometimes helps with proper formatting.

  4. Add explicit formatting instructions in your action group description: While you mentioned this didn't fully resolve the issue, including specific formatting instructions in your action group description or parameter descriptions can sometimes help.

The issue appears to be related to how the Bedrock Agent formats the parameters when invoking the Lambda function, rather than an issue with your OpenAPI schema definition. The inconsistent behavior you're seeing (where retries sometimes work correctly) suggests this is likely a limitation in the current implementation of Bedrock Agents.

If these workarounds don't resolve your issue, consider reaching out to AWS Support for additional guidance, as they may have more specific recommendations or information about upcoming fixes for this behavior.
Sources
JSON Array in RequestBody not supported? | AWS re:Post
Amazon.Lambda.AspNetCoreServer.Hosting as Bedrock Agent Action Group Lambda | AWS re:Post
Is this a Bug? Not been able to edit action group function in JSON Editor. | 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.