aws-samples / amazon-bedrock-samples

This repository contains examples for customers to get started using the Amazon Bedrock Service. This contains examples for all available foundational models
https://aws.amazon.com/bedrock/
MIT No Attribution
474 stars 251 forks source link

KeyError: 'requestBody' : File "/var/task/lambda_function.py", line 15, in get_named_property event['requestBody']['content']['application/json']['properties'] #226

Open sabeelms opened 2 months ago

sabeelms commented 2 months ago

Hi,

I'm testing "create an action group" for a bedrock agent that can call a lambda function. Creating this action group calls for having an OpenAPI schema which defines the function endpoints, required arguments, and the returns.

However, I'm seeing the following error

LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html

[ERROR] KeyError: 'requestBody'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 97, in lambda_handler
    internal_id = get_named_property(event, "internalId")
  File "/var/task/lambda_function.py", line 15, in get_named_property
    event['requestBody']['content']['application/json']['properties']

Can someone please look into this?

mttanke commented 1 month ago

is internalId defined as a POST or a GET parameter in your OpenAPI schema? you should use get_parameter for GET parameters and get_named_property for POST ones. See readme: https://github.com/aws-samples/amazon-bedrock-samples/tree/main/agents-for-bedrock/features-examples/02-create-agent-with-api-schema

    # getting parameters according to the http method
    if http_method == "get":
        claim_id = get_named_parameter(event, "claim_id")
    elif http_method == "post":
        claim_id = get_named_property(event, "claim_id")