Repository navigation
fix: validate RFT rewards before reporting rollout success - #6572
Conversation
|
cc @992X @SifeiLi for review. issue identified by @qingliz in awslabs/agentcore-rl-toolkit#209 |
There was a problem hiding this comment.
🤖 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) |
There was a problem hiding this comment.
🟡 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)) |
There was a problem hiding this comment.
We may need to update the types we support in the future, but this looks fine to me for now!
There was a problem hiding this comment.
🤖 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 |
There was a problem hiding this comment.
🟠 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
There was a problem hiding this comment.
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"), |
There was a problem hiding this comment.
🟡 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
There was a problem hiding this comment.
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.
|
One failing unit test seems unrelated to changes, train unit tests passing on py310 and py311 too |
Summary
sagemaker_rft_handlercurrently reports dictionary results without a reward asreadywithout submitting a reward. Invalid reward values can also triggerreadybefore 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_errorpath to reportfailedwith reward 0. Tuple inputs and the existing handling of booleans are preserved. The handler's return value, expliciterror/skippedhandling, 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