langchain-ai/deepagents · error · ValueError

Invalid scope {scope!r}; expected 'cwd' or 'all'

Error message

Invalid scope {scope!r}; expected 'cwd' or 'all'

What it means

The thread-scope preference saver only accepts 'cwd' (current working directory) or 'all'. Any other scope raises ValueError("Invalid scope {scope!r}; expected 'cwd' or 'all'"). Validating before the TOML write keeps the stored scope a recognized value for the thread list filter.

Source

Thrown at libs/code/deepagents_code/model_config.py:6383

    return True


def save_thread_scope(scope: str, config_path: Path | None = None) -> bool:
    """Save the directory-scope preference for the thread selector.

    Args:
        scope: `"cwd"` (current working directory) or `"all"` (all directories).
        config_path: Path to config file.

    Returns:
        True if save succeeded, False on I/O error.

    Raises:
        ValueError: If `scope` is not a recognised value.
    """
    if scope not in {"cwd", "all"}:
        msg = f"Invalid scope {scope!r}; expected 'cwd' or 'all'"
        raise ValueError(msg)
    if config_path is None:
        config_path = DEFAULT_CONFIG_PATH
    try:
        with _config_write_lock:
            config_path.parent.mkdir(parents=True, exist_ok=True)
            if config_path.exists():
                with config_path.open("rb") as f:
                    data = tomllib.load(f)
            else:
                data = {}
            if "threads" not in data:
                data["threads"] = {}
            data["threads"]["scope"] = scope
            fd, tmp_path = tempfile.mkstemp(dir=config_path.parent, suffix=".tmp")
            try:
                with os.fdopen(fd, "wb") as f:
                    tomli_w.dump(data, f)
                Path(tmp_path).replace(config_path)

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Pass exactly 'cwd' or 'all'; translate UI labels to these canonical strings first.
  2. Normalize and validate: scope.lower() in {'cwd','all'} before calling.
  3. Discard or migrate legacy scope values rather than re-saving them.

Example fix

// before
save_thread_scope(scope_label)  # e.g. "global"
// after
scope = "all" if scope_label == "global" else "cwd"
save_thread_scope(scope)
Defensive patterns

Strategy: validation

Validate before calling

VALID_SCOPES = {"cwd", "all"}
if scope not in VALID_SCOPES:
    scope = "cwd"  # safe default before saving

Try / catch

try:
    save_thread_scope(scope)
except ValueError as exc:
    logging.warning("%s; using default", exc)
    save_thread_scope("cwd")

Prevention

When it happens

Trigger: Calling the scope-saving function with "global", "project", "", or any value outside {'cwd','all'} (case-sensitive), with an optional config_path.

Common situations: Mapping a UI dropdown with different labels ('this project' / 'everywhere') without translation; passing a boolean or enum member instead of the string; re-saving a legacy stored value.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/5bb99fa0bf274b0b. Report an issue: GitHub.