Repository navigation
fix(event_handler): clear Bedrock Agent function context when a tool raises - #8874
Conversation
…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.
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
leandrodamascena
left a comment
There was a problem hiding this comment.
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.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
|
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |



Issue number: closes #8873
Summary
Changes
BedrockAgentFunctionResolveronly cleared the context after a tool returned. When the tool raised,_resolve()built theError: ...response without clearing it, so data added withappend_contextcarried over to the next invocation. An event missing a required field, which makesresolve()raiseValueError, had the same problem.finallyblock inresolve(), as fix(event_handler): clear context when route resolution raises #8539 did forApiGatewayResolverand fix(event_handlers): Bedrock kwargs and media type, AppSync registry and context, Router duplicates, Function URL base path, Depends with classes #8629 did forAppSyncResolver.clear_context()call in_resolve(), sinceresolve()now handles it.User experience
Before: a tool that calls
app.append_context(...)and then raises leaves that data inapp.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
developwithassert {'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,mypyandty. Two logger tests (test_append_context_keys_handles_empty_dict,test_log_dict_key_strip_nones) fail in the full run ondevelopas 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.