langchain-ai/deepagents · error · ValueError

Path cannot be empty

Error message

Path cannot be empty

What it means

_normalize_path rejects empty or whitespace-only path strings. Since an empty string is ambiguous (root vs. mistake), it raises ValueError('Path cannot be empty') to force callers to be explicit, suggesting '/' for root.

Source

Thrown at libs/deepagents/deepagents/backends/utils.py:751

    Args:
        path: Path to normalize (None defaults to "/")

    Returns:
        Normalized path starting with / (without trailing slash unless it's root)

    Raises:
        ValueError: If path is invalid (empty string after strip)

    Example:
        _normalize_path(None) -> "/"
        _normalize_path("/dir/") -> "/dir"
        _normalize_path("dir") -> "/dir"
        _normalize_path("/") -> "/"
    """
    path = path or "/"
    if not path or path.strip() == "":
        msg = "Path cannot be empty"
        raise ValueError(msg)

    normalized = path if path.startswith("/") else "/" + path

    # Only root should have trailing slash
    if normalized != "/" and normalized.endswith("/"):
        normalized = normalized.rstrip("/")

    return normalized


def _filter_files_by_path(files: dict[str, Any], normalized_path: str) -> dict[str, Any]:
    """Filter files dict by normalized path, handling exact file matches and directory prefixes.

    Expects a normalized path from `_normalize_path` (no trailing slash except root).

    Args:
        files: Dictionary mapping file paths to file data
        normalized_path: Normalized path from `_normalize_path` (e.g., "/", "/dir", "/dir/file")

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Pass '/' explicitly if you mean the root.
  2. Pass a real directory path instead of an empty string.
  3. Coalesce blank values before calling: path = path.strip() or '/'.
  4. Trim and validate agent-supplied paths before invoking glob/grep tools.

Example fix

// before
files = glob_search(pattern="*.py", path="   ")
// after
files = glob_search(pattern="*.py", path=(path or "/").strip() or "/")
Defensive patterns

Strategy: validation

Validate before calling

def normalize_search_path(path: str | None) -> str:
    return (path or "/").strip() or "/"

Type guard

def is_valid_search_path(path: str | None) -> bool:
    return path is None or (isinstance(path, str) and path.strip() != "")

Try / catch

try:
    results = grep_matches_from_files(pattern, path=user_path)
except ValueError as exc:
    if str(exc) == "Path cannot be empty":
        results = grep_matches_from_files(pattern, path="/")
    else:
        raise

Prevention

When it happens

Trigger: Calling _normalize_path with a whitespace-only string like ' ' (a None is coalesced to '/', but blank strings are not), typically reached via _glob_search_files or grep_matches_from_files when the path parameter is empty/blank.

Common situations: Glob or grep tool invoked with an empty 'path' parameter by an LLM agent; config values set to '' instead of '/' or a real directory; string manipulation stripping a path to nothing.

Related errors


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