{"record":{"id":"a024fae527030e1c","repo":"FoundationAgents/MetaGPT","slug":"unsupported-file-format-please-choose-py-json","errorCode":null,"errorMessage":"Unsupported file format. Please choose 'py', 'json', or 'ipynb'.","messagePattern":"Unsupported file format\\. Please choose 'py', 'json', or 'ipynb'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/save_code.py","lineNumber":40,"sourceCode":"\n    Returns:\n    - None\n    \"\"\"\n    # Create the folder path if it doesn't exist\n    os.makedirs(name=DATA_PATH / \"output\" / f\"{name}\", exist_ok=True)\n\n    # Choose to save as a Python file or a JSON file based on the file format\n    file_path = DATA_PATH / \"output\" / f\"{name}/code.{file_format}\"\n    if file_format == \"py\":\n        file_path.write_text(code_context + \"\\n\\n\", encoding=\"utf-8\")\n    elif file_format == \"json\":\n        # Parse the code content as JSON and save\n        data = {\"code\": code_context}\n        write_json_file(file_path, data, encoding=\"utf-8\", indent=2)\n    elif file_format == \"ipynb\":\n        nbformat.write(code_context, file_path)\n    else:\n        raise ValueError(\"Unsupported file format. Please choose 'py', 'json', or 'ipynb'.\")\n","sourceCodeStart":22,"sourceCodeEnd":41,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/save_code.py#L22-L41","documentation":"Raised by metagpt.utils.save_code.save_code: after handling the three supported formats ('py' writes text, 'json' wraps a {'code': ...} dict, 'ipynb' uses nbformat.write), any other file_format string reaches the else-branch and is rejected with ValueError.","triggerScenarios":"Calling save_code(ctx, name='proj', file_format='md') or 'txt', 'yaml', or with an uppercase 'PY' (comparison is exact, not lowercased).","commonSituations":"Extending example scripts to save other formats; passing a value derived from a file suffix without stripping the dot ('.py' fails); typos or case differences in the format string.","solutions":["Use exactly 'py', 'json' or 'ipynb'.","If the value comes from a filename, strip and lower it: file_format = suffix.lstrip('.').lower().","For other formats, write the file yourself (file_path.write_text) instead of save_code."],"exampleFix":"# before\nsave_code(ctx, name='proj', file_format='.PY')  # ValueError\n\n# after\nfmt = file_format.lstrip('.').lower()  # 'py'\nsave_code(ctx, name='proj', file_format=fmt)","handlingStrategy":"validation","validationCode":"file_format = file_format.lstrip('.').lower()\nassert file_format in {'py', 'json', 'ipynb'}, f'bad file_format: {file_format}'","typeGuard":"def is_supported_save_format(fmt: str) -> bool:\n    return isinstance(fmt, str) and fmt.lstrip('.').lower() in {'py', 'json', 'ipynb'}","tryCatchPattern":"try:\n    save_code(ctx, name=name, file_format=fmt)\nexcept ValueError:\n    save_code(ctx, name=name, file_format='py')  # default to python","preventionTips":["Normalize suffix-derived values: strip the dot and lowercase.","Expose only the three formats in CLI/UI choices.","For markdown/yaml, write files directly instead of save_code."],"tags":["metagpt","save","validation"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}