Skip to content

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

Merged
leandrodamascena merged 3 commits into
aws-powertools:developfrom
hyeonsang010716:fix/bedrock-agent-function-clear-context
Nov 25, 2026
Merged

leandrodamascena merged 3 commits into
aws-powertools:developfrom
hyeonsang010716:fix/bedrock-agent-function-clear-context

Conversation

@hyeonsang010716

Copy link
Copy Markdown
Contributor

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

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hyeonsang010716! Same shape as the API Gateway and AppSync fixes, one finally in resolve() covering the tool raising and the invalid event, and the old call in _resolve() gone. I ran the repro from your issue on both sides: develop leaks {"user": "alice"} into the next invocation, this branch gives {}. Goes out in 3.42.0, no need to rebase, you'll be in the release notes.

@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 25, 2026
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.15%. Comparing base (a1f97bd) to head (a43d26c).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8874   +/-   ##
========================================
  Coverage    97.15%   97.15%           
========================================
  Files          327      327           
  Lines        16813    16813           
  Branches      1539     1539           
========================================
  Hits         16334    16334           
  Misses         336      336           
  Partials       143      143           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit dbe1662 into aws-powertools:develop Nov 25, 2026
14 checks passed
@boring-cyborg

boring-cyborg Bot commented Nov 25, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

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

2 participants