Skip to content

Error reading COMPLETION field as JSON (botocore type) response from BEDROCK Agent

0

Hi All, I have configured an agent in bedrock that invokes a LAMBDA function. Everything works fine.

The problem I'm having is that I want to publish the agent to an EC2 + IIS + Flask + Python.

The service manages to consume and respond, however, when I want to read the COMPLETION field where the Model's response is, I cannot read it correctly.

Here is the python code, if you can help me I would greatly appreciate it.

from flask import Flask, request, jsonify import json import logging import uuid import boto3 import asyncio import botocore.session from botocore.exceptions import ClientError from botocore.eventstream import EventStream

   response = client.invoke_agent(
        agentId='AAAAAAAAAAAAAAAAA',
        agentAliasId=agent_alias_id,
        sessionId=session_id,
        inputText=prompt,
        endSession=True,
        enableTrace=True
    )
    
    
    if 'completion' in response:
        event_stream = response['completion']
        
         response_parts = []
        for event in event_stream:
            if 'data' in event:
                response_data = event['data'].decode('utf-8')
                
                print("Evento de respuesta:", response_data)

                response_parts.append(response_data)

        # Unir todos los fragmentos de respuesta en un solo texto
        final_response = ''.join(response_parts)
    logging.debug(f"Response from COMPLETION: {response.get("completion")}")

    return jsonify({"result": final_response})

Thanks

1 Answer
0

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:

  1. First, check if 'completion' is in the response.
  2. If it is, iterate over the EventStream object.
  3. For each event, look for a 'chunk' key which contains the actual response data.
  4. 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

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.