Skip to content

Bedrock Invoke Agent Stream - Message checksum mismatch. #3110

@adrianpaiva1

Description

@adrianpaiva1

Describe the bug

I randomly get this Exception while streaming responses with the BedrockAgentRuntimeClient. This is doesn't happen consistently, maybe once every 10 times.

Aws\Api\Parser\Exception\ParserException Message checksum mismatch. at vendor/aws/aws-sdk-php/src/Api/Parser/NonSeekableStreamDecodingEventStreamIterator.php:55

Image

Sample code to reproduce the issue

<?php

namespace App\Console\Commands;

use Aws\BedrockAgentRuntime\BedrockAgentRuntimeClient;
use Aws\Result;
use Illuminate\Console\Command;

class Chat extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'chat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @throws \Throwable
     */
    public function handle()
    {
        $client = new BedrockAgentRuntimeClient(
            [
                'version' => config('services.bedrock.version'),
                'region' => config('services.bedrock.region'),
                'credentials' => [
                    'key' => config('services.bedrock.key'),
                    'secret' => config('services.bedrock.secret'),
                ],
                'http' => [
                    'stream' => true,
                ],
            ]
        );

        $data = [
            'agentId' => 'IDHERE',
            'agentAliasId' => 'TSTALIASID',
            'sessionId' => uniqid(),
            'inputText' => 'tell me everything about tylenol',
            'memoryId' => uniqid(),
            'streamingConfigurations' => [
                'streamFinalResponse' => true,
            ],
        ];

        $result = $client->invokeAgent($data);

        foreach ($this->processResult($result) as $chunk) {
            echo  $chunk['data']['bytes'];
        }
    }

    public function processResult(Result $result): \Generator
    {
        $base_data = [
            'session_id' => $result['sessionId'],
            'memory_id' => $result['memoryId'],
        ];

        // Process all events in the completion stream
        foreach ($result['completion'] as $event) {
            $data = $base_data;

            if (isset($event['chunk'])) {
                $data['data'] = $event['chunk'];
            } elseif (isset($event['files'])) {
                $data['files'] = $event['files']['files'];
            } elseif (isset($event['returnControl'])) {
                $data['return_control'] = $event['returnControl'];
            } elseif (isset($event['trace'])) {
                $data['trace'] = $event['trace'];
            } else {
                // Everything else is treated as an exception
                $exceptionType = array_key_first($event);
                $exceptionData = $event[$exceptionType];
                throw new \Exception($exceptionType);
            }

            yield $data;
        }
    }
}

This happens for me in two different projects, using the latest SDK version.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

I should be able to consistently stream invoke agent responses.

Current Behavior

Image

Reproduction Steps

See the included sample code, try it multiple times.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

3.342.32 - tried earlier versions as well same issue

Environment details (Version of PHP (php -v)? OS name and version, etc.)

Happens with both php 8.3 and 8.4

Metadata

Metadata

Labels

bugThis issue is a bug.investigatingThis issue is being investigated and/or work is in progress to resolve the issue.p2This is a standard priority issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions