Skip to content

Replace asserts with ValueError in chat_template/conversation.py#94

Open
lonexreb wants to merge 1 commit intoNVlabs:mainfrom
lonexreb:refactor/conversation-asserts-to-valueerror
Open

Replace asserts with ValueError in chat_template/conversation.py#94
lonexreb wants to merge 1 commit intoNVlabs:mainfrom
lonexreb:refactor/conversation-asserts-to-valueerror

Conversation

@lonexreb
Copy link
Copy Markdown
Contributor

@lonexreb lonexreb commented May 4, 2026

Why

Continues the pattern of #50 (create_message), #73 (load_physical_aiavdataset), and #77 (assert→ValueError in public APIs):

Asserts can be disabled with python -O (or PYTHONOPTIMIZE=1), so any input-validation guard expressed as an assert silently disappears in optimized runs. Public-API guards must run unconditionally.

src/alpamayo_r1/chat_template/conversation.py has four such asserts that all guard caller-provided inputs to public functions. They're invisible to anyone running optimized Python.

What

Convert all four asserts to explicit if … raise ValueError(...):

Line Function Guarded condition
38 get_component_str exactly one of content_str / padding_str (XOR)
115 construct_image camera_ids is sorted ascending
195 construct_cot "cot" key present in data when not asking the model to generate it
227 construct_meta_action "meta_action_strings" key present in data when not asking the model to generate it

Two readability wins beyond the strip-resistance:

  • The XOR check on L38 originally read as (a is None) != (b is None). The new form if (a is None) == (b is None): raise ValueError(...) catches both "neither provided" and "both provided" with the same wording — without needing to mentally parse a != of two booleans.
  • The construct_image check now includes the offending tensor in the error message (f"got {camera_ids.tolist()}"), instead of a bare AssertionError that hides the value.

Verification

After this PR, grep -n "^\s*assert " src/alpamayo_r1/chat_template/conversation.py returns nothing. Behavior with optimization off is identical; with optimization on, the checks now actually run.

Continues the pattern of NVlabs#50 (create_message), NVlabs#73
(load_physical_aiavdataset), and NVlabs#77 (assert→ValueError in public
APIs): asserts can be stripped with `python -O` (or by callers using
`PYTHONOPTIMIZE=1`), so any input-validation guard expressed as an
assert silently disappears in optimized runs.

Convert all four asserts in src/alpamayo_r1/chat_template/conversation.py
to explicit `if ... raise ValueError(...)`:

- get_component_str L38: callers must pass exactly one of `content_str`
  or `padding_str` (XOR). The new check `(a is None) == (b is None)`
  catches both "neither provided" and "both provided" with the same
  message.
- construct_image L115: `camera_ids` must be sorted ascending; the
  unsorted case now raises with the offending tensor in the message
  for easier debugging instead of a bare AssertionError.
- construct_cot L195: `data["cot"]` must exist when not asking the
  model to generate it.
- construct_meta_action L227: `data["meta_action_strings"]` must exist
  when not asking the model to generate it.

Pure validation hardening. Behavior with optimization off is identical;
with optimization on, the checks now actually run.

Signed-off-by: lonexreb <[email protected]>
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.

1 participant