Why a server fault on the Megatron Gym path reaches Gym as a "bad request"

PR #5097 (part 6 of 6) · permalinks at head e6beabe5, Megatron-LM pin e0a4bfed, ai-dynamo 1.8.0.dev20261117

Before
The Megatron backend did not serve NeMo-Gym behind Dynamo, so this code did not exist.
This PR
Adds the Megatron decode leg for Gym calls. It wraps the whole leg in except ValueError and answers every catch with a 400 refusal.
Status
Open — not fixed in this PR. Raised in review comment C2, with a one-line fix.

Four pages for this PR, one question each: this one shows one bug.

A refusal tells Gym "your request was wrong". The comment on the catch says it is meant for one thing: Megatron-LM refusing the request, which its own endpoint answers with 400. But Python's ValueError is wider than that. Two server faults raise a plain ValueError inside the same block. One is Dynamo's check that a decode leg got its prefill result (called here), which fails when a decode leg arrives without one. The other is a validation error while staging the capture, after the tokens already exist.

Three different errors, one answer

What can raise inside the decode leg (_serve_gym_call)(a) Megatron-LM refuses the requestInferenceRequestRejected (a ValueError)(b) Dynamo: decode got no prefill resultplain ValueError (no live prefill worker)(c) Capture staging after the tokens existpydantic ValidationError (a ValueError)except ValueErroradapter.py:205400 "bad request" refusalacknowledge=False(a), (b), (c) all blame the callerSame faults on the vLLM backend, and after the fixvLLM: a ValueError that is not a context overflow→ 500, the chat engine's rule for engine failuresFix: except InferenceRequestRejected(a) → 400 as natively · (b), (c) → 500 like vLLMright answerwrong answer today

The fix — proposed, not in the PR

Where the fix lands: RL only, one source file and one test. adapter.py catches only Megatron-LM's own refusal type. test_dynamo_megatron_adapter.py raises that type instead of a plain ValueError. No Megatron-LM or Dynamo change.

Decode leg, today

# decode engine, rank zero, one task per Gym call
try:
    chunk = await serve_gym_call()  # generate (a, b), then stage (c)
except ValueError:               # catches (a), (b) and (c)
    chunk = refusal(400)                # Gym: "your request was bad"

The fix, as pseudocode (proposed, not run)

# decode engine, rank zero, one task per Gym call
try:
    chunk = await serve_gym_call()  # generate (a, b), then stage (c)
except ValueError:
except InferenceRequestRejected: # only (a): Megatron-LM refused it
    chunk = refusal(400)                # (b), (c) raise → chat engine answers 500

Megatron-LM's other plain ValueErrors (sampling checks) cannot be reached from a Gym call, because gym_sampling_request always sends n=1 and log probs 0 or none.

So what. When a decode leg arrives without its prefill result, or staging hits a bug, Gym sees a "bad request" for a request that was fine. The real cause is hidden behind a status that blames the caller, and the vLLM backend answers the same faults with 500. Catching only InferenceRequestRejected keeps Megatron-LM's own 400 and sends everything else down the chat engine's 500 path.