事象
Amazon Bedrockエージェントで、アクショングループに外部APIを叩いてレスポンスを受け取るLambda関数を選択し、テストエージェントでメッセージを送って動作確認をしようとしたところ、以下のエラーが発生しました。
The server encountered an error processing the Lambda response. Check the Lambda response and retry the request
訳: 「サーバーで Lambda レスポンスの処理中にエラーが発生しました。Lambda レスポンスを確認してリクエストを再試行してください。」
解決法
Lambdaのレスポンスを公式ドキュメントの形式に合わせることで解決しました。
Lambda関数例
import json
import urllib.request
def lambda_handler(event, context):
api_url = "https://example.com/api"
request_data = json.dumps(event.get('requestData', {})).encode('utf-8')
request = urllib.request.Request(api_url, data=request_data, headers={'Content-Type': 'application/json'}, method='POST')
with urllib.request.urlopen(request) as response:
response_data = response.read().decode('utf-8')
response_body = {
'application/json': {
'body': json.loads(response_data)
}
}
action_response = {
'actionGroup': event.get('actionGroup'),
'apiPath': event.get('apiPath'),
'httpMethod': event.get('httpMethod'),
'httpStatusCode': response.getcode(),
'responseBody': response_body
}
api_response = {
'messageVersion': '1.0',
'response': action_response,
'sessionAttributes': event.get('sessionAttributes', {}),
'promptSessionAttributes': event.get('promptSessionAttributes', {})
}
return api_response
終わりに
APIのレスポンスをそのままエージェントに渡せば良いと思っていたため、Lambdaからのレスポンス形式に細かい指定があるのは盲点でした。
参考になれば幸いです。