Invoke Bedrock Agent with boto3: Unable to generate multiple chunks for streaming response #4159
Labels
bedrock-agent-runtime
bug
This issue is a confirmed bug.
service-api
This issue is caused by the service API, not the SDK implementation.
Describe the bug
The boto3 library is not able to generate multiple chunks for performing a streaming response when using the
invoke_agent
method. The response intermittently includesNone
chunks and raises aJSONDecodeError
.Previous issue reference
Boto3 documentation for invoke agents
Amazon Bedrock using streaming message samples
Expected Behavior
The invoke_agent method should return a streaming response in multiple chunks without intermittent None chunks. Each chunk should contain valid JSON that can be decoded and processed as JSON.
Inside the Amazon Bedrock Playground the functionality it seems soo easy to change for stream response or not. And when is need to implement and invoke an agent with these functionality is not very easy to set.
![image_2024-06-07_174905335](https://private-user-images.githubusercontent.com/6331820/337804266-3e6c403c-db7a-4556-8efe-41704aa0aaa8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzOTI3NDgsIm5iZiI6MTczOTM5MjQ0OCwicGF0aCI6Ii82MzMxODIwLzMzNzgwNDI2Ni0zZTZjNDAzYy1kYjdhLTQ1NTYtOGVmZS00MTcwNGFhMGFhYTgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxMiUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTJUMjAzNDA4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NzJiMTc5NWEwNWZjOWM3ODM5YTVkYTJiN2Y0OTk5NjBmZWYzODMzN2QyMjE0MmMyOTYxYjEwYWVlMTFjZWEwMiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.IA96QbDlBff6CLHHkYeRpR0StvIco48PyktxMfCszuA)
Current Behavior
The response includes intermittent None chunks.
A JSONDecodeError is raised when attempting to decode None values
{'ResponseMetadata': {'HTTPHeaders': {'connection': 'keep-alive',
'content-type': 'application/json',
'date': 'Fri, 07 Jun 2024 20:59:27 GMT',
'transfer-encoding': 'chunked',
'x-amz-bedrock-agent-session-id': 'd2c6c96f-2510-11ef-a351-4c24ce79c99c',
'x-amzn-bedrock-agent-content-type': 'application/json',
'x-amzn-requestid': 'daf30b3d-86f8-4bc7-a8e8-84d6e9d9e50b'},
'HTTPStatusCode': 200,
'RequestId': 'daf30b3d-86f8-4bc7-a8e8-84d6e9d9e50b',
'RetryAttempts': 0},
'completion': <botocore.eventstream.EventStream object at 0x0000012B0F01A930>,
'contentType': 'application/json',
'sessionId': 'd2c6c96f-2510-11ef-a351-4c24ce79c99c'}
chunk: None
chunk: None
chunk: None
chunk: None
chunk: None
chunk: {'bytes': b"Hello!, I'm AI assistant. How can I assist you today?"}
bytes: Hello! I'm AI assistant. How can I assist you today?
Traceback (most recent call last):
File "C:\Users\marce\AppData\Local\Temp\ipykernel_86248\3204360913.py", line 29, in call_haiku
message = json.loads(decoded_bytes)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\marce\AppData\Local\Programs\Python\Python312\Lib\json_init_.py", line 346, in loads
return _default_decoder.decode(s)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\marce\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\marce\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Reproduction Steps
Steps to reproduce the behavior:
bedrock_agent_runtime_client.invoke_agent
.Code Snippet
The text was updated successfully, but these errors were encountered: