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.