Bedrock Runtime get_session does not return sessionAttributes

0

I have an agent where I would like to use sessionAttributes to keep track of context and share it with the front end. The agent has a tool to create a record in a database. My lambda function creates the record, creates an ID for it, and returns that as sessionAttributes.

However, after this, I call client.get_session() using the session ID, and the session does not contain the sessionMetadata key, or any session Attributes.

bedrock_agent_runtime = boto3.client('bedrock-agent-runtime')
response = bedrock_agent_runtime.create_session()
session_id = response['sessionId']
inv_response = bedrock_agent_runtime.invoke_agent(
    agentId=AGENT_ID,
    agentAliasId=AGENT_ALIAS_ID,
    sessionId=session_id,
    inputText=prompt,
    enableTrace=True, # Set to True for debugging
    endSession=False,
    sessionState={
        "sessionAttributes": {
            'some_key':'some_val'
        }
    }
)
sess_response = bedrock_agent_runtime.get_session(sessionIdentifier=session_id)

The value of sess_response is: {'ResponseMetadata': {'RequestId': '3c354a53-adca-469c-bc1f-9dd68223798a', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Thu, 10 Apr 2025 02:43:35 GMT', 'content-type': 'application/json', 'content-length': '270', 'connection': 'keep-alive', 'x-amzn-requestid': '3c354a53-adca-469c-bc1f-9dd68223798a'}, 'RetryAttempts': 0}, 'createdAt': datetime.datetime(2025, 4, 10, 2, 38, 41, 964420, tzinfo=tzutc()), 'lastUpdatedAt': datetime.datetime(2025, 4, 10, 2, 38, 41, 964420, tzinfo=tzutc()), 'sessionArn': 'arn:aws:bedrock:us-east-1:<account ID>:session/43c36cd6-fb9c-42d9-b95f-8caddfb91322', 'sessionId': '43c36cd6-fb9c-42d9-b95f-8caddfb91322', 'sessionStatus': 'ACTIVE'}

I would have expected to see both the values passed to invoke_agent, and the ones that my lambda function added, however, I see neither.

asked a month ago15 views
1 Answer
0

Set sessionAttributes in sessionState during invoke_agent(): Ensure that you're correctly passing sessionState in the invoke_agent() call, as you're doing. However, the session state should be set correctly in the request. You’re doing it right, but ensure that all values are properly formatted.

Check for Correct Call Order: Make sure that get_session() is called only after the session has been updated with the correct sessionState. If you're calling get_session() too soon, the session may not have been updated yet with the expected attributes.

Ensure Persistence of sessionAttributes: Double-check that your session’s state is being correctly persisted and updated in the Bedrock service. If the sessionAttributes are not being returned after invoking the agent, it could mean that the session is not correctly updated or persisted in the first place.

Corrected Example: Here's an example of how to structure the code to track session attributes and retrieve them:

import boto3

Create Bedrock client

bedrock_agent_runtime = boto3.client('bedrock-agent-runtime')

Create a new session

response = bedrock_agent_runtime.create_session() session_id = response['sessionId']

Set session attributes during invoke_agent

inv_response = bedrock_agent_runtime.invoke_agent( agentId=AGENT_ID, agentAliasId=AGENT_ALIAS_ID, sessionId=session_id, inputText=prompt, enableTrace=True, # Set to True for debugging endSession=False, sessionState={ "sessionAttributes": { 'some_key': 'some_val' # Custom session attribute } } )

Retrieve session using get_session

sess_response = bedrock_agent_runtime.get_session(sessionIdentifier=session_id)

Print response

print(sess_response) Checkpoints: sessionState format: Ensure that sessionState contains the correct structure and is being sent as part of the invoke_agent() request. This ensures that session attributes are passed correctly.

Delay after invoking the agent: It might take some time for the session to be updated. Ensure you’re not calling get_session() too quickly after invoking the agent.

regards, M Zubair https://zeonedge.com

answered a month 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