Skip to content

fix: validate RFT rewards before reporting rollout success - #6572

Merged
aviruthen merged 3 commits into
aws:masterfrom
luyuzhe111:codex/rft-reward-validation
Nov 12, 2026
Merged

aviruthen merged 3 commits into
aws:masterfrom
luyuzhe111:codex/rft-reward-validation

Conversation

@luyuzhe111

@luyuzhe111 luyuzhe111 commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

sagemaker_rft_handler currently reports dictionary results without a reward as ready without submitting a reward. Invalid reward values can also trigger ready before reward submission fails.

Reuse the feedback client's existing scalar-or-iterable conversion and validate the resulting list before reporting success. Missing rewards, values that cannot be converted, empty results, non-numeric elements, and non-finite floats use the existing report_error path to report failed with reward 0. Tuple inputs and the existing handling of booleans are preserved. The handler's return value, explicit error/skipped handling, public signatures, and feedback exception policy remain unchanged.

A decorated handler that manually reports feedback but returns a dictionary without a valid reward will now also attempt to report failed.

Validation

  • RFT unit tests: 34 parameterized cases passed across synchronous and asynchronous handlers, including tuple rewards. Tests run the real decorator and feedback client, mocking only HTTP submission to verify the feedback request sequence and unchanged return values.
  • Black and flake8 passed for both changed Python files.

@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 tightens _handle_result in the RFT handler so that automatic rollout success is only reported for valid rewards (finite int/float, or a non-empty list of those; rejecting None, bools, non-numeric values, and non-finite floats). Invalid/missing rewards now go through report_error (status failed, reward 0). The implementation is correct, matches the documented behavior change, and comes with a thorough set of 36 parameterized sync/async tests. Overall the change looks clean; I only have a minor readability note.

Reviewed commit da6d95a. Automated review; verify before acting.

else:
rewards = reward if isinstance(reward, list) else [reward]
if not rewards or not all(
not isinstance(value, bool)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Low: The validity check is correct but relies on Python's operator precedence (and binding tighter than or) inside a not all(...) with a double negation (not isinstance(value, bool)), which makes it easy to misread. Consider extracting a small helper such as _is_valid_reward(value) returning isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value) (note: math.isfinite accepts ints too), and using if not rewards or not all(_is_valid_reward(v) for v in rewards):. Behavior is unchanged but intent becomes clearer for future maintenance.

rewards = reward if isinstance(reward, list) else [reward]
if not rewards or not all(
not isinstance(value, bool)
and (isinstance(value, int) or isinstance(value, float) and math.isfinite(value))

@aviruthen aviruthen Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We may need to update the types we support in the future, but this looks fine to me for now!

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

The PR tightens _handle_result in the RFT handler decorator so that only finite numeric rewards (or non-empty iterables thereof) report rollout success; everything else now reports failed with reward 0 via report_error. The refactor correctly reuses the feedback client's scalar/iterable conversion, handles tuples, and adds good parameterized tests. One correctness gap: booleans are not actually rejected despite the PR's stated intent, and no test covers them.

Reviewed commit b93c603. Automated review; verify before acting.


if not rewards or not all(
isinstance(value, int) or (isinstance(value, float) and math.isfinite(value))
for value in rewards

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 Medium: This validation accepts boolean rewards, but both the PR description and the retrieved guidance for this change say booleans should be rejected ("Reject None, booleans, non-numeric values, and non-finite floats as invalid rewards"). Because bool is a subclass of int, isinstance(True, (int, float)) is True, so reward=True becomes rewards=[True], and then isinstance(True, int) is also True, so it passes validation and gets reported as a successful reward of true. If booleans should be invalid, add an explicit not isinstance(value, bool) guard (both in the initial conversion and the all(...) check). Source: #6572

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what the bot quoted is not in the current pr description. currently boolean is accepted, and this PR kept this behavior.

pytest.param({"reward": None}, "failed", [0.0], id="none"),
pytest.param({"reward": "0.5"}, "failed", [0.0], id="string"),
pytest.param({"reward": {}}, "failed", [0.0], id="dict"),
pytest.param({"reward": float("nan")}, "failed", [0.0], id="nan"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Low: The parameterized cases cover many edge cases but omit boolean rewards (e.g. {"reward": True} and {"reward": [True]}), which the PR description and retrieved guidance call out as a case that should be rejected. Adding these cases would pin down the intended boolean behavior and catch the issue noted on the validation logic. Source: #6572

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same here. the bot seems to quote my last version of this PR where i rejected boolean. I have removed this rejection to avoid behavior changes.

@aviruthen

Copy link
Copy Markdown
Collaborator

One failing unit test seems unrelated to changes, train unit tests passing on py310 and py311 too

@aviruthen
aviruthen merged commit 2151812 into aws:master Nov 12, 2026
25 of 27 checks passed
@luyuzhe111
luyuzhe111 deleted the codex/rft-reward-validation branch November 12, 2026 21:59

This branch was previously deployed

1 inactive deployment
manual-approval — b93c6039 Deployed Nov 12, 2026 by luyuzhe111 via wait-for-approval #2217
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