Closed devd7c closed 2 weeks ago
Thanks for reaching out. As mentioned in the previous issue you referenced, I confirmed with the Bedrock team that this is currently the expected behavior. The InvokeAgent API only returns one chunk. I suggested that they update their documentation to better clarify that behavior. They are also tracking a feature request in their backlog to stream multiple chunks. Please refer to the news blog and CHANGELOG for future updates related to that.
This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.
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 samplesExpected 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.
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