import boto3
import json
import uuid
REGION = "us-west-2"
AGENT_RUNTIME_ARN = "arn:aws:bedrock-agentcore:...:agent-runtime/..." # <- your runtime ARN
client = boto3.client("bedrock-agentcore", region_name=REGION)
session_id = str(uuid.uuid4()) # reuse this for the SAME chat thread
prompt = "Hello! Summarize what you can do in one sentence."
payload = json.dumps({"prompt": prompt}).encode("utf-8")
resp = client.invoke_agent_runtime(
agentRuntimeArn=AGENT_RUNTIME_ARN,
runtimeSessionId=session_id,
contentType="application/json",
accept="text/event-stream", # streaming
payload=payload,
)
print("runtimeSessionId:", resp.get("runtimeSessionId"))
print("statusCode:", resp.get("statusCode"))
# AgentCore returns a streaming body; docs show parsing SSE lines via iter_lines
# (Look for lines starting with "data: ")
for line in resp["response"].iter_lines(chunk_size=10):
if not line:
continue
text = line.decode("utf-8", errors="replace")
if text.startswith("data: "):
print(text[6:]) # JSON or text chunks depending on your agent