Skip to content

Add defensive assertions for required keys in preprocessing - #26

Open
hgbytes wants to merge 1 commit into
google-deepmind:mainfrom
hgbytes:add-preprocessing-assertions
Open

Add defensive assertions for required keys in preprocessing#26
hgbytes wants to merge 1 commit into
google-deepmind:mainfrom
hgbytes:add-preprocessing-assertions

Conversation

@hgbytes

@hgbytes hgbytes commented Dec 4, 2025

Copy link
Copy Markdown

Add defensive assertions to preprocessing functions

Problem

compute_inputs_and_labels() and compute_token_types() access dictionary keys without validation, risking uncaught KeyError exceptions if called with malformed input. While currently safe (keys are always provided by callers), this creates a latent bug that surfaces only during future refactoring or code reuse.

Solution

Add assertions at function entry points to validate required keys:

  • compute_inputs_and_labels(): Assert "for_observation" and "for_target" exist
  • compute_token_types(): Assert "inputs" and "labels" exist

This follows the codebase's existing validation pattern (assertions throughout).

Changes

  • Added 2 assertions to compute_inputs_and_labels() (lines 59-60)
  • Added 2 assertions to compute_token_types() (lines 85-86)
  • Updated docstring in compute_token_types() to document required input format
  • Added preprocessing_test.py with 11 unit tests covering:
    • Valid inputs produce correct output
    • Missing keys raise clear AssertionError
    • Extra keys are preserved
    • Input dict is not mutated

Impact

  • No breaking changes: All existing code passes (keys are always provided)
  • Infrastructure only: No changes to model logic
  • Improves maintainability: Catches misuse early with clear error messages
  • Test coverage: 11 new tests verify assertions work correctly

Testing

  • Updated unittest covers valid inputs, missing-key cases, and preservation of extra fields.

@google-cla

google-cla Bot commented Dec 4, 2025

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

- Add assertions in compute_inputs_and_labels() for for_observation/for_target
- Add assertions in compute_token_types() for inputs/labels
- Matches existing assertion pattern throughout codebase
@hgbytes
hgbytes force-pushed the add-preprocessing-assertions branch from fe3a56e to 7385b48 Compare December 4, 2025 15:19
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