{"record":{"id":"524f2e13332d411b","repo":"FoundationAgents/MetaGPT","slug":"json-file-jsonl-file-not-exist-return","errorCode":null,"errorMessage":"json_file: {jsonl_file} not exist, return []","messagePattern":"json_file: (.+?) not exist, return \\[\\]","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/common.py","lineNumber":614,"sourceCode":"        tip = f\"Cannot serialize object of type '{type(x).__name__}'\"\n\n    raise TypeError(tip)\n\n\ndef write_json_file(json_file: str, data: Any, encoding: str = \"utf-8\", indent: int = 4, use_fallback: bool = False):\n    folder_path = Path(json_file).parent\n    if not folder_path.exists():\n        folder_path.mkdir(parents=True, exist_ok=True)\n\n    custom_default = partial(to_jsonable_python, fallback=handle_unknown_serialization if use_fallback else None)\n\n    with open(json_file, \"w\", encoding=encoding) as fout:\n        json.dump(data, fout, ensure_ascii=False, indent=indent, default=custom_default)\n\n\ndef read_jsonl_file(jsonl_file: str, encoding=\"utf-8\") -> list[dict]:\n    if not Path(jsonl_file).exists():\n        raise FileNotFoundError(f\"json_file: {jsonl_file} not exist, return []\")\n    datas = []\n    with open(jsonl_file, \"r\", encoding=encoding) as fin:\n        try:\n            for line in fin:\n                data = json.loads(line)\n                datas.append(data)\n        except Exception:\n            raise ValueError(f\"read jsonl file: {jsonl_file} failed\")\n    return datas\n\n\ndef add_jsonl_file(jsonl_file: str, data: list[dict], encoding: str = None):\n    folder_path = Path(jsonl_file).parent\n    if not folder_path.exists():\n        folder_path.mkdir(parents=True, exist_ok=True)\n\n    with open(jsonl_file, \"a\", encoding=encoding) as fout:\n        for json_item in data:","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/common.py#L596-L632","documentation":"read_jsonl_file in metagpt/utils/common.py raises FileNotFoundError when the given .jsonl path does not exist. The message text mistakenly says 'json_file' and 'return []' (copied from read_json_file), but the behavior is a raise, so callers cannot rely on the message wording.","triggerScenarios":"read_jsonl_file('workspace/ trajectory.jsonl') before any rows were appended; wrong relative path under a different cwd; typo in the filename; the producer writes only on flush and the file was never created.","commonSituations":"Reading a trajectory/history file that is created lazily on first write; batch scripts run from a different directory; case-sensitivity or extension mismatch (.json vs .jsonl).","solutions":["Check Path(jsonl_file).resolve() exists before calling.","Treat a missing file as an empty trajectory: if not p.exists(): rows = [].","Use absolute paths anchored to the project/workspace root.","Ensure the producing process actually created and flushed the file."],"exampleFix":"# before\nrows = read_jsonl_file('out/run1.jsonl')  # raises if run never wrote\n\n# after\nfrom pathlib import Path\np = Path('out/run1.jsonl')\nrows = read_jsonl_file(str(p)) if p.exists() else []","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef jsonl_readable(jsonl_file: str) -> bool:\n    return Path(jsonl_file).is_file()","typeGuard":null,"tryCatchPattern":"try:\n    rows = read_jsonl_file(path)\nexcept FileNotFoundError:\n    rows = []  # first run / empty history is a normal case","preventionTips":["Treat a missing .jsonl as empty history rather than an error.","Anchor paths to the workspace root.","Ignore the message text; it misnames the file and claims a default return."],"tags":["metagpt","jsonl","file-io","path","file-not-found"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}