0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Amazon BedrockエージェントでThe server encountered an error processing the Lambda response.のエラーが出た時の対処法

Last updated at Posted at 2025-03-18

事象

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からのレスポンス形式に細かい指定があるのは盲点でした。
参考になれば幸いです。

参考
https://note.com/yutaito_opst/n/nb10b685073ec

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?