Skip to content

Make Bedrock tool use validation more permissive? #3209

@davidsickmiller

Description

@davidsickmiller

Describe the feature

Hi! I recent wrote some code like this:

$statusResult = $this->route53DomainsClient->getOperationDetail([
    'OperationId' => $toolUse['input']['operation_id']
]);

return [
    'toolResult' => [
        'toolUseId' => $toolUse['toolUseId'],
        'content' => [
            ['json' => $statusResult->toArray()],
        ],
    ],
];

When the tool result subsequently got sent to BedrockRuntimeClient::converse(), it failed with this error:

InvalidArgumentException: Found 1 error while validating the input provided for the Converse operation:\n[messages][3][content][0][toolResult][content][0][json] is not a valid document type in /var/task/vendor/aws/aws-sdk-php/src/Api/Validator.php:65

This caused me to spend some time scratching my head. I added an echo json_encode($statusResult->toArray()); call then read through Validator.php without understanding why it would fail validation.

Eventually I came to understand that the AwsResult object returned by getOperationDetail() contains some values that are DateTime or DateTimeImmutable or something like that. When passed through json_encode(), these values are converted to strings. But converse() doesn't convert them into strings prior to validation and, as such, this code in Validator::checkDocumentType() rejects the input:

return is_null($value)
            || is_numeric($value)
            || is_string($value)
            || is_bool($value);

For now I've worked around the problem by changing one line of code to this:

            ['json' => json_decode(json_encode($statusResult->toArray()), true)],

The experience was sufficiently annoying that I think I'll remember to do this going forward, but it seems likely to trip up future engineers.

Use Case

See above

Proposed Solution

Can we remove or relax the validation so that ['json' => $data] will work for any input that json_encode() would accept?

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

3.356.28

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

PHP 8.2.22 (cli) (built: Jul 30 2024 11:47:12) (NTS)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions