FoundationAgents/MetaGPT · error · Exception

Error reading file '{FILE_NAME}': {str(e)}

Error message

Error reading file '{FILE_NAME}': {str(e)}

What it means

A generic wrapper raised when opening or reading the SPO settings yaml fails for a non-YAMLError reason — typically PermissionError, IsADirectoryError, or UnicodeDecodeError from config_path.open(). The original exception text is embedded and is the real clue.

Source

Thrown at metagpt/ext/spo/utils/load.py:28

def set_file_name(name: str):
    global FILE_NAME
    FILE_NAME = name


def load_meta_data(k: int = SAMPLE_K):
    # load yaml file
    config_path = Path(__file__).parent.parent / "settings" / FILE_NAME

    if not config_path.exists():
        raise FileNotFoundError(f"Configuration file '{FILE_NAME}' not found in settings directory")

    try:
        with config_path.open("r", encoding="utf-8") as file:
            data = yaml.safe_load(file)
    except yaml.YAMLError as e:
        raise ValueError(f"Error parsing YAML file '{FILE_NAME}': {str(e)}")
    except Exception as e:
        raise Exception(f"Error reading file '{FILE_NAME}': {str(e)}")

    qa = []

    for item in data["qa"]:
        question = item["question"]
        answer = item["answer"]
        qa.append({"question": question, "answer": answer})

    prompt = data["prompt"]
    requirements = data["requirements"]
    count = data["count"]

    if isinstance(count, int):
        count = f", within {count} words"
    else:
        count = ""

    random_qa = random.sample(qa, min(k, len(qa)))

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Check the embedded exception text to identify PermissionError vs UnicodeDecodeError vs IsADirectoryError.
  2. chmod/chown the settings file so the running user can read it; keep extensions plain (no directories) in set_file_name.
  3. Re-save the yaml as UTF-8 without BOM.

Example fix

# before: file saved as UTF-16 by an editor
# after: re-encode
# iconv -f UTF-16 -t UTF-8 settings/my.yaml > settings/my.yaml.utf8 && mv settings/my.yaml.utf8 settings/my.yaml
Defensive patterns

Strategy: try-catch

Validate before calling

import os, pathlib

def readable_utf8(p: str) -> bool:
    path = pathlib.Path(p)
    if not path.is_file() or not os.access(path, os.R_OK):
        return False
    try:
        path.read_text(encoding="utf-8")
        return True
    except UnicodeDecodeError:
        return False

Try / catch

try:
    data = load_meta_data()
except Exception as e:
    if "Error reading file" in str(e):
        logger.error("settings file unreadable (permissions/encoding): %s", e)
    raise

Prevention

When it happens

Trigger: FILE_NAME set to a directory name instead of a file; insufficient file permissions (e.g. file created by root, read by another user); file saved in a non-UTF-8 encoding so encoding='utf-8' decoding fails.

Common situations: Settings files created under Docker/root and mounted read-only or with restrictive modes; editors saving UTF-16 or with BOM issues; FILE_NAME accidentally including a path separator resolving to a directory.

Related errors


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