FoundationAgents/MetaGPT · error · ValueError

Unsupported file format. Please choose 'py', 'json', or 'ipy

Error message

Unsupported file format. Please choose 'py', 'json', or 'ipynb'.

What it means

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.

Source

Thrown at metagpt/utils/save_code.py:40

    Returns:
    - None
    """
    # Create the folder path if it doesn't exist
    os.makedirs(name=DATA_PATH / "output" / f"{name}", exist_ok=True)

    # Choose to save as a Python file or a JSON file based on the file format
    file_path = DATA_PATH / "output" / f"{name}/code.{file_format}"
    if file_format == "py":
        file_path.write_text(code_context + "\n\n", encoding="utf-8")
    elif file_format == "json":
        # Parse the code content as JSON and save
        data = {"code": code_context}
        write_json_file(file_path, data, encoding="utf-8", indent=2)
    elif file_format == "ipynb":
        nbformat.write(code_context, file_path)
    else:
        raise ValueError("Unsupported file format. Please choose 'py', 'json', or 'ipynb'.")

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Use exactly 'py', 'json' or 'ipynb'.
  2. If the value comes from a filename, strip and lower it: file_format = suffix.lstrip('.').lower().
  3. For other formats, write the file yourself (file_path.write_text) instead of save_code.

Example fix

# before
save_code(ctx, name='proj', file_format='.PY')  # ValueError

# after
fmt = file_format.lstrip('.').lower()  # 'py'
save_code(ctx, name='proj', file_format=fmt)
Defensive patterns

Strategy: validation

Validate before calling

file_format = file_format.lstrip('.').lower()
assert file_format in {'py', 'json', 'ipynb'}, f'bad file_format: {file_format}'

Type guard

def is_supported_save_format(fmt: str) -> bool:
    return isinstance(fmt, str) and fmt.lstrip('.').lower() in {'py', 'json', 'ipynb'}

Try / catch

try:
    save_code(ctx, name=name, file_format=fmt)
except ValueError:
    save_code(ctx, name=name, file_format='py')  # default to python

Prevention

When it happens

Trigger: Calling save_code(ctx, name='proj', file_format='md') or 'txt', 'yaml', or with an uppercase 'PY' (comparison is exact, not lowercased).

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/a024fae527030e1c. Report an issue: GitHub.