{"record":{"id":"189720000d81f77e","repo":"FoundationAgents/MetaGPT","slug":"json-file-json-file-not-exist-return","errorCode":null,"errorMessage":"json_file: {json_file} not exist, return []","messagePattern":"json_file: (.+?) not exist, return \\[\\]","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/common.py","lineNumber":574,"sourceCode":"        if retry_state.fn is None:\n            fn_name = \"<unknown>\"\n        else:\n            # Retrieve the callable's name using a utility function\n            fn_name = _utils.get_callback_name(retry_state.fn)\n\n        # Log an error message with the function name, time since start, attempt number, and the exception\n        i.error(\n            f\"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), \"\n            f\"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. \"\n            f\"exp: {retry_state.outcome.exception()}\"\n        )\n\n    return log_it\n\n\ndef read_json_file(json_file: str, encoding: str = \"utf-8\") -> list[Any]:\n    if not Path(json_file).exists():\n        raise FileNotFoundError(f\"json_file: {json_file} not exist, return []\")\n\n    with open(json_file, \"r\", encoding=encoding) as fin:\n        try:\n            data = json.load(fin)\n        except Exception:\n            raise ValueError(f\"read json file: {json_file} failed\")\n    return data\n\n\ndef handle_unknown_serialization(x: Any) -> str:\n    \"\"\"For `to_jsonable_python` debug, get more detail about the x.\"\"\"\n\n    if inspect.ismethod(x):\n        tip = f\"Cannot serialize method '{x.__func__.__name__}' of class '{x.__self__.__class__.__name__}'\"\n    elif inspect.isfunction(x):\n        tip = f\"Cannot serialize function '{x.__name__}'\"\n    elif hasattr(x, \"__class__\"):\n        tip = f\"Cannot serialize instance of '{x.__class__.__name__}'\"","sourceCodeStart":556,"sourceCodeEnd":592,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/common.py#L556-L592","documentation":"read_json_file in metagpt/utils/common.py checks Path(json_file).exists() first and raises FileNotFoundError when the path is missing. Note the message text says 'return []' but the function actually raises — the message is a leftover from an older returning-default behavior.","triggerScenarios":"read_json_file('data/config.json') when the file was never created; relative path resolved against a different working directory; typo in the filename; file expected to be generated by an earlier pipeline stage that silently skipped it.","commonSituations":"Workspace-relative paths under pytest or a daemon where cwd differs; downloading/scaffolding step that should have produced the JSON failed; case-sensitive filename mismatch on Linux.","solutions":["Verify the path: print Path(json_file).resolve() and check it exists.","Use absolute paths or anchor relative paths to the project root (__file__ or CONFIG_PATH).","Ensure the upstream step that writes the JSON ran successfully.","If a missing file is a normal case, guard with Path(json_file).exists() and supply a default yourself."],"exampleFix":"# before\ndata = read_json_file('configs/settings.json')  # cwd-dependent, may raise\n\n# after\nfrom pathlib import Path\np = Path(__file__).parent / 'configs' / 'settings.json'\ndata = read_json_file(str(p)) if p.exists() else []","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef json_readable(json_file: str) -> bool:\n    return Path(json_file).is_file()","typeGuard":null,"tryCatchPattern":"try:\n    data = read_json_file(path)\nexcept FileNotFoundError:\n    data = []  # or raise a domain-specific error with context","preventionTips":["Resolve paths against an explicit base, not cwd.","Check Path(...).exists() before reading when absence is normal.","Note the misleading 'return []' wording — the function raises."],"tags":["metagpt","file-io","json","path","file-not-found"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}