fix: use json.loads with Path.read_text() instead of json.load with Path#10361
Open
satishkc7 wants to merge 2 commits intohiyouga:mainfrom
Open
fix: use json.loads with Path.read_text() instead of json.load with Path#10361satishkc7 wants to merge 2 commits intohiyouga:mainfrom
satishkc7 wants to merge 2 commits intohiyouga:mainfrom
Conversation
json.load() expects a file-like object, not a pathlib.Path. Passing a Path directly raises AttributeError: 'PosixPath' object has no attribute 'read'. Replace with json.loads(path.read_text()) to correctly read the JSON config file. Fixes hiyouga#10316
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the JSON configuration loading logic in parser.py to read file contents as text before parsing. A review comment suggests using OmegaConf.load() instead to improve consistency with YAML handling and simplify the implementation.
src/llamafactory/hparams/parser.py
Outdated
| elif len(sys.argv) > 1 and sys.argv[1].endswith(".json"): | ||
| override_config = OmegaConf.from_cli(sys.argv[2:]) | ||
| dict_config = OmegaConf.create(json.load(Path(sys.argv[1]).absolute())) | ||
| dict_config = OmegaConf.create(json.loads(Path(sys.argv[1]).absolute().read_text())) |
Contributor
There was a problem hiding this comment.
Since OmegaConf.load() is already used for YAML files and it natively supports JSON, you can use it here as well. This improves consistency with the YAML loading logic and simplifies the code by avoiding the intermediate string conversion and explicit json.loads() call. Note that if you apply this change, the import json on line 18 will become unused and can be removed.
dict_config = OmegaConf.load(Path(sys.argv[1]).absolute())
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
json.load()expects a file-like object, not apathlib.Path. Passing aPathdirectly raises:This happens whenever a
.jsonconfig file is passed as the first CLI argument (e.g.llamafactory-cli train config.json).Root Cause
In
src/llamafactory/hparams/parser.py, the JSON branch ofread_args():The YAML branch correctly uses
OmegaConf.load()which acceptsPathobjects, butjson.load()does not.Fix
Using
Path.read_text()reads the file content as a string, andjson.loads()parses a string - no file object needed.Fixes #10316