fix(tokenizer): resolve prepend_id scope issue in encode()#421
Open
heloanc wants to merge 1 commit intokarpathy:masterfrom
Open
fix(tokenizer): resolve prepend_id scope issue in encode()#421heloanc wants to merge 1 commit intokarpathy:masterfrom
heloanc wants to merge 1 commit intokarpathy:masterfrom
Conversation
Initialize prepend_id before conditional blocks to prevent NameError, and add try/except fallback for multi-token prepend strings that cause encode_single_token to raise ValueError. Fixes karpathy#348 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Agolid
pushed a commit
to Agolid/autoresearch
that referenced
this pull request
Mar 27, 2026
- Initialize prepend_id = None before conditional blocks - Wrap encode_single_token() in try/except with fallback to encode_ordinary() - Check prepend_id is not None instead of prepend for insertion - Prevents NameError when encode_single_token() raises - Handles multi-token prepend strings gracefully Fixes karpathy#421
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
NameErrorandValueErrorinTokenizer.encode()whenprependis a string.Problem
prepend_idis assigned insideif prepend is not None:but referenced in a nested block — ifencode_single_token()raises,prepend_idis never definedencode_single_token()raisesValueErrorfor multi-token prepend stringsprependinstead ofprepend_id, masking failed computationFix
prepend_id = Nonebefore conditional blocksencode_single_token()in try/except with fallback toencode_ordinary()prepend_id is not Noneinstead ofprepend is not Nonefor insertionFixes #348