Skip to content

fix(event_handler): clear Bedrock Agent function context when a tool raises - #8874

Open
hyeonsang010716 wants to merge 2 commits into
aws-powertools:developfrom
hyeonsang010716:fix/bedrock-agent-function-clear-context
Open

hyeonsang010716 wants to merge 2 commits into
aws-powertools:developfrom
hyeonsang010716:fix/bedrock-agent-function-clear-context

Conversation

@hyeonsang010716

Copy link
Copy Markdown

Issue number: closes #8873

Summary

Changes

BedrockAgentFunctionResolver only cleared the context after a tool returned. When the tool raised, _resolve() built the Error: ... response without clearing it, so data added with append_context carried over to the next invocation. An event missing a required field, which makes resolve() raise ValueError, had the same problem.

User experience

Before: a tool that calls app.append_context(...) and then raises leaves that data in app.context, and the next invocation on the same warm container can read it. The repro in #8873 prints {"context_seen": {"user": "alice"}}.

After: the context is cleared after every invocation, whether the tool returns, raises, or the event is invalid. The repro prints {"context_seen": {}}.

Verification

Both new tests fail on develop with assert {'user_id': '12345'} == {} and pass with this change.

Tested locally on Python 3.14 with the full pytest -m "not perf" --ignore tests/e2e, ruff format --check, ruff check, mypy and ty. Two logger tests (test_append_context_keys_handles_empty_dict, test_log_dict_key_strip_nones) fail in the full run on develop as well and pass on their own, so they are unrelated to this change.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…raises

BedrockAgentFunctionResolver only cleared the context after a tool
returned. When the tool raised, the error response was built without
clearing it, so data added with append_context carried over to the next
invocation on the same warm container. An event missing a required
field had the same problem.

Clear the context in a finally block in resolve(), as ApiGatewayResolver
and AppSyncResolver already do.
@hyeonsang010716
hyeonsang010716 requested a review from a team as a code owner November 19, 2026 05:24
@boring-cyborg

boring-cyborg Bot commented Nov 19, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 19, 2026
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

event_handlers size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: BedrockAgentFunctionResolver keeps app.context when a tool raises

1 participant