Boto3 dependencyFailedException error when calling the InvokeAgent operation: Access denied when calling Bedrock

0

Hi,

I'm writing a simple Python script to send a question to a Bedrock agent and then print the response.

My code is as follows:

import boto3

agent_id = agent_id
region_name = region_name
agent_alias_id = agent_alias_id

bedrock_agent_client = boto3.client("bedrock-agent-runtime", region_name = region_name)

try:
  response = bedrock_agent_client.invoke_agent(
     agentId=agent_id,
     agentAliasId=agent_alias_id,
     sessionId=sessionID,
     endSession=False,
     enableTrace=False,
     inputText= "Explain black holes to a fifth grader"
  ) 
  completion = ""

  for event in response.get("completion"):
        chunk = event["chunk"]
        completion = completion + chunk["bytes"].decode()
        print(completion)

except Exception as e:
    raise Exception("unexpected event.",e)

No matter how I modify this code, though, I receive the following error:

botocore\eventstream.py", line 619, in _parse_event raise EventStreamError(parsed_response, self._operation_name) botocore.exceptions.EventStreamError: An error occurred (dependencyFailedException) when calling the InvokeAgent operation: Access denied when calling Bedrock. Check your request permissions and retry the request.

The script crashes on this line: event in response.get("completion"):

My IAM User account has full permissions to Bedrock (via AmazonBedrockFullAccess). I've been searching for a fix to the permission error, but I don't understand where my user account is lacking permission. When using invoke_model in a similar Python script, I'm able to send a question to an LLM and print the response with no issues. I'm also able to use invoke_model_with_response_stream and have no issues printing the chunked response.

Thanks in advance for any guidance you can give me.

asked a year ago2.2K views
1 Answer
0
Accepted Answer

I managed to fix the problem myself. I created a new agent, giving it a name and brief description, as I did before. Then I replaced the agent_ID and agent_alias_ID in my code with the new agent's IDs. Now, suddenly, the code works.

answered a year 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.

Guidelines for Answering Questions