fix: accept long JSON strings in ChatTemplateConfig.from_json - #5009
Open
MohammadHijjawi97 wants to merge 1 commit into
Open
MohammadHijjawi97 wants to merge 1 commit into
MohammadHijjawi97 wants to merge 1 commit into
Conversation
from_json tried to open its argument as a file and only treated it as a JSON string on FileNotFoundError. Opening a JSON string as a path can raise other OSErrors, such as ENAMETOOLONG on Linux once the string exceeds the file name limit (e.g. a long meta_instruction), or EINVAL on Windows for any string containing quotes. Those were turned into "Invalid input. Must be a file path or a valid JSON string." even though the input was valid JSON. Check for an existing file first and otherwise parse the argument as JSON.
This branch has not been deployed
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.
Motivation
ChatTemplateConfig.from_jsonis documented to accept "a JSON file or JSON string". It first triesopen()on the argument and only falls back to parsing it as JSON onFileNotFoundError. Opening a JSON string as a path can raise otherOSErrors, and these are turned intoValueError('Invalid input. Must be a file path or a valid JSON string.')even though the input is valid JSON:EINVALfor any JSON string, since it contains quotes and colons (reproduced);ENAMETOOLONGcan be raised once the string exceeds the file-name length limit, e.g. a config with a longmeta_instruction.Modification
Read the argument as a file only when
os.path.isfile()is true; otherwise parse it as a JSON string. Invalid input still raises aValueError(json.JSONDecodeError). Addedtest_chat_template_config_from_jsontotests/test_lmdeploy/test_model.py, covering a long JSON string and a JSON file.BC-breaking (Optional)
No.
Checklist