Repository navigation
Description
Expected Behaviour
app.context is cleared at the end of every invocation, including when the tool raises. The next invocation in the same execution environment starts with an empty context. ApiGatewayResolver (#8538) and AppSyncResolver (#8584) already work this way.
Current Behaviour
BedrockAgentFunctionResolver._resolve() calls clear_context() only after the tool returns. If the tool raises, the except Exception branch returns the Error: ... response without clearing the context. The next warm invocation then sees the data the failed invocation appended, which can belong to another caller.
The same happens when resolve() raises ValueError("Missing required field: ...") for an invalid event: context appended before resolve() is never cleared.
Code snippet
from aws_lambda_powertools.event_handler import BedrockAgentFunctionResolver
app = BedrockAgentFunctionResolver()
@app.tool(name="charge")
def charge(user: str):
app.append_context(user=user)
if user == "alice":
raise RuntimeError("payment backend down")
return f"charged {user}"
@app.tool(name="whoami")
def whoami():
return {"context_seen": dict(app.context)}
def event(function: str, parameters: list) -> dict:
return {
"messageVersion": "1.0",
"agent": {"alias": "a", "name": "n", "version": "1", "id": "i"},
"sessionId": "s",
"actionGroup": "g",
"function": function,
"parameters": parameters,
"inputText": "",
"sessionAttributes": {},
"promptSessionAttributes": {},
}
app.resolve(event("charge", [{"name": "user", "type": "string", "value": "alice"}]), None)
print(app.resolve(event("whoami", []), None)["response"]["functionResponse"]["responseBody"])Output:
{'TEXT': {'body': '{"context_seen": {"user": "alice"}}'}}
Expected output: {'TEXT': {'body': '{"context_seen": {}}'}}
Possible Solution
Clear the context in a finally block in resolve(), the same fix used in #8539 for ApiGatewayResolver and in #8629 for AppSyncResolver, and remove the success-only clear_context() call in _resolve().
Steps to Reproduce
- Register a tool that calls
app.append_context(...)and then raises. - Invoke it through
app.resolve(...). - Invoke any other tool on the same resolver instance and read
app.context.
Did you run the reproduction yourself?
Yes, I ran it and saw the failure
Powertools for AWS Lambda (Python) version
3.41.0
AWS Lambda function runtime
3.14
Packaging format used
PyPi
Debugging logs
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsComing soon
Activity
boring-cyborg commentedon Nov 19, 2026
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link
powertools-for-aws-oss-automation commentedon Nov 19, 2026
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out our GitHub Discussions: Join the conversation
powertools-for-aws-oss-automation commentedon Nov 25, 2026
Warning
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.