Skip to content

fix: preserve AgentCore context in RFT handlers - #6571

Merged
aviruthen merged 3 commits into
aws:masterfrom
luyuzhe111:codex/fix-rft-handler-arguments
Nov 12, 2026
Merged

aviruthen merged 3 commits into
aws:masterfrom
luyuzhe111:codex/fix-rft-handler-arguments

Conversation

@luyuzhe111

Copy link
Copy Markdown
Contributor

Description

Fix sagemaker_rft_handler failing when an AgentCore entrypoint accepts (payload, context). The SageMaker integration guide requires applying this decorator to AgentCore entrypoints. The official AgentCore Python SDK supports the context parameter and covers it in both sync and async tests.

For example:

from bedrock_agentcore.runtime import BedrockAgentCoreApp
from sagemaker.train.rft import sagemaker_rft_handler

app = BedrockAgentCoreApp()

@app.entrypoint
@sagemaker_rft_handler
async def invoke(payload, context):
    return {"reward": 1.0, "session_id": context.session_id}

AgentCore uses inspect.signature() to decide whether to pass the request context. Because the RFT decorator uses functools.wraps, inspection exposes the original two-argument signature, but the wrapper accepts only payload. Invoking the example therefore returns HTTP 500 with TypeError: invoke() takes 1 positional argument but 2 were given, before the handler or rollout feedback logic runs.

Forward additional positional and keyword arguments through both RFT wrappers while retaining wraps. This lets AgentCore supply the context and preserves existing single-argument handlers. Add unit coverage for sync/async handlers, positional and keyword context forwarding, feedback reporting, and rollout context cleanup. Update the decorator's argument documentation and correct its name in the existing example.

Validation

  • pytest sagemaker-train/tests/unit/train/test_rft_decorators.py: 8 passed.
  • Local compatibility checks with the official BedrockAgentCoreApp, with feedback HTTP requests mocked: sync and async context handlers fail before the fix and succeed after it.
  • Black and flake8 checks on the changed Python files: passed.
  • Focused Sphinx build of the modified docstring with warnings treated as errors: passed.

@luyuzhe111

luyuzhe111 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

cc @992X @SifeiLi for review. issue identified by @qingliz in awslabs/agentcore-rl-toolkit#209

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 AI code review

This PR fixes sagemaker_rft_handler so that both the sync and async wrappers forward extra positional/keyword arguments to the wrapped handler, allowing AgentCore entrypoints with a (payload, context) signature to work. The change is minimal, backward compatible, and comes with new unit tests covering sync/async handlers, positional and keyword context forwarding, feedback reporting, and context cleanup. The docstring is also corrected to use the real decorator name. The code is correct — imports (RolloutFeedbackClient, sagemaker_rft_handler, get_inference_params, get_rollout_context) and the monkeypatched _bearer_post all exist in the base checkout. No issues found.

Reviewed commit 2c195a5. Automated review; verify before acting.

aviruthen
aviruthen previously approved these changes Nov 11, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 AI code review

This PR fixes sagemaker_rft_handler so it forwards extra positional/keyword arguments (e.g. AgentCore's context) through both the sync and async wrappers while still using functools.wraps. The change is small, correct, and backward-compatible with single-argument handlers. It adds solid unit coverage (sync/async, positional/keyword context, feedback reporting, context cleanup) and corrects a stale decorator name in the docstring example. I found no issues; the exports and context helpers used by the new test exist at the base commit, and the monkeypatched _bearer_post is the real method on the feedback client.

Reviewed commit 898efb0. Automated review; verify before acting.

@aviruthen

Copy link
Copy Markdown
Collaborator

Checks are passing (including AI code reviewer), remaining issue is a merge conflict just on a unit test file, can be run locally so that checks do not need to be rerun!

@aviruthen

Copy link
Copy Markdown
Collaborator

Checks already passed previously, and test_rft_decorators.py was run locally and verified due to merge conflicts introduced by merging #6572.

@aviruthen
aviruthen merged commit d4fb17b into aws:master Nov 12, 2026
3 of 4 checks passed
@luyuzhe111
luyuzhe111 deleted the codex/fix-rft-handler-arguments branch November 12, 2026 21:59

This branch is waiting to be deployed

1 waiting deployment
manual-approval — 2957d32b Waiting Nov 12, 2026 by luyuzhe111 via wait-for-approval #2225
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants