- Newest
- Most votes
- Most comments
The issue you're experiencing is likely related to how you're handling the 'completion' field in the response from the Bedrock Agent. The 'completion' field is actually an EventStream object, not a JSON structure, which is why you're having trouble reading it directly.
To properly handle the response, you should iterate over the EventStream object and process each chunk of data. Here's how you can modify your code to correctly read the completion:
- First, check if 'completion' is in the response.
- If it is, iterate over the EventStream object.
- For each event, look for a 'chunk' key which contains the actual response data.
- Decode the bytes in the chunk and append it to your response.
Here's an example of how you could modify your code:
if 'completion' in response: completion = "" for event in response['completion']: if 'chunk' in event: chunk = event['chunk'] if 'bytes' in chunk: completion += chunk['bytes'].decode('utf-8') logging.debug(f"Response from COMPLETION: {completion}") return jsonify({"result": completion})
This approach should allow you to properly read and process the response from the Bedrock Agent. Remember to handle potential exceptions and edge cases, such as empty responses or unexpected data formats.
Also, make sure you have the latest version of boto3 installed, as older versions might have issues with certain Bedrock operations. If you continue to experience problems, you may want to enable more detailed logging or tracing to get a better understanding of the exact response structure you're receiving from the Bedrock Agent.
Sources
Error raised while calling invoke agent operation | AWS re:Post
Boto3 dependencyFailedException error when calling the InvokeAgent operation: Access denied when calling Bedrock | AWS re:Post
Bedrock agent invokation does not return "returnControl" element | AWS re:Post
Relevant content
- asked 2 years ago
- asked a year ago
