fix: add explicit encoding="utf-8" to .txt read in _validators.py#3288
Open
Ghraven wants to merge 1 commit into
Open
fix: add explicit encoding="utf-8" to .txt read in _validators.py#3288Ghraven wants to merge 1 commit into
Ghraven wants to merge 1 commit into
Conversation
The text-mode open() for user-supplied .txt fine-tuning files relied on the platform default encoding (cp1252 on Windows), which raises UnicodeDecodeError or corrupts non-ASCII content on valid UTF-8 files. Pinning encoding="utf-8" makes the read consistent across platforms.
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.
What
src/openai/lib/_validators.pyreads a user-supplied.txtfine-tuning file in text mode without an explicit encoding:In text mode,
open()uses the platform default encoding (locale.getpreferredencoding()). On Windows that is typically cp1252, not UTF-8.Why it matters
.txtdatasets frequently contain non-ASCII characters (smart quotes, accented text, CJK, emoji). On a default-cp1252 platform, reading a valid UTF-8 file raisesUnicodeDecodeErroror silently corrupts characters.Before / After
How I verified
Reproduced the failure on a simulated cp1252 default: the explicit utf-8 read preserves content, while a cp1252 read of the same valid UTF-8 file raises
UnicodeDecodeError. Withencoding="utf-8"the read succeeds regardless of platform. No behavior change on systems that already default to UTF-8.Scope
Single-line change in
src/openai/lib/, a hand-maintained (non-generated) path per CONTRIBUTING. Happy to adjust if you would prefer a different approach.