Skip to content

add case to handle indented .json parsing (#175) #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions openai/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,14 +526,21 @@ def read_any_format(fname, fields=["prompt", "completion"]):
else:
pass # this is what we expect for a .jsonl file
elif fname.lower().endswith(".json"):
df = pd.read_json(fname, lines=True, dtype=str).fillna("")
if len(df) == 1:
# this is what we expect for a .json file
try:
# to handle case where .json file is actually a .jsonl file
df = pd.read_json(fname, lines=True, dtype=str).fillna("")
if len(df) == 1:
# this code path corresponds to a .json file that has one line
df = pd.read_json(fname, dtype=str).fillna("")
else:
# this is NOT what we expect for a .json file
immediate_msg = "\n- Your JSON file appears to be in a JSONL format. Your file will be converted to JSONL format"
necessary_msg = (
"Your format `JSON` will be converted to `JSONL`"
)
except ValueError:
# this code path corresponds to a .json file that has multiple lines (i.e. it is indented)
df = pd.read_json(fname, dtype=str).fillna("")
else:
# this is NOT what we expect for a .json file
immediate_msg = "\n- Your JSON file appears to be in a JSONL format. Your file will be converted to JSONL format"
necessary_msg = "Your format `JSON` will be converted to `JSONL`"
else:
error_msg = "Your file must have one of the following extensions: .CSV, .TSV, .XLSX, .TXT, .JSON or .JSONL"
if "." in fname:
Expand Down