langchain-ai/langchain · error · ValueError

Path '{path}' contains '..' components. Directory traversal

Error message

Path '{path}' contains '..' components. Directory traversal sequences are not allowed when loading prompt configurations. Use direct relative paths instead, or pass `allow_dangerous_paths=True` if you trust the input.

What it means

`_validate_path` rejects any path containing a `..` component when loading prompt configs (default `allow_dangerous_paths=False`). `..` segments can escape the intended directory, enabling directory-traversal attacks when loading untrusted prompt files.

Source

Thrown at libs/core/langchain_core/prompts/loading.py:46

    Raises:
        ValueError: If the path is absolute or contains `..` components.
    """
    if path.is_absolute():
        msg = (
            f"Path '{path}' is absolute. Absolute paths are not allowed "
            f"when loading prompt configurations to prevent path traversal "
            f"attacks. Use relative paths instead, or pass "
            f"`allow_dangerous_paths=True` if you trust the input."
        )
        raise ValueError(msg)
    if ".." in path.parts:
        msg = (
            f"Path '{path}' contains '..' components. Directory traversal "
            f"sequences are not allowed when loading prompt configurations. "
            f"Use direct relative paths instead, or pass "
            f"`allow_dangerous_paths=True` if you trust the input."
        )
        raise ValueError(msg)


@deprecated(
    since="1.2.21",
    removal="2.0.0",
    alternative="Use `dumpd`/`dumps` from `langchain_core.load` to serialize "
    "prompts and `load`/`loads` to deserialize them.",
)
def load_prompt_from_config(
    config: dict[str, Any], *, allow_dangerous_paths: bool = False
) -> BasePromptTemplate[str]:
    """Load prompt from config dict.

    Args:
        config: Dict containing the prompt configuration.
        allow_dangerous_paths: If `False` (default), file paths in the
            config (such as `template_path`, `examples`, and
            `example_prompt_path`) are validated to reject absolute paths

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Resolve the real location and reference it via a safe relative path with no `..` components (e.g. run from a common parent directory).
  2. If the input is trusted, pass `allow_dangerous_paths=True` to the loader.
  3. Restructure so templates live at or below the config directory.

Example fix

# before
load_prompt('prompts/../shared/prompt.json')  # ValueError

# after
load_prompt('shared/prompt.json')  # invoke from the parent dir
# or, for trusted input
load_prompt('prompts/../shared/prompt.json', allow_dangerous_paths=True)
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path

p = Path(user_path)
assert '..' not in p.parts, f'reject traversal path: {p}'
load_prompt(str(p))

Type guard

def has_no_traversal(path: str | Path) -> bool:
    return '..' not in Path(path).parts

Prevention

When it happens

Trigger: `load_prompt('prompts/../../etc/config.json')` or a config whose `template_path: ../../shared/t.txt` includes `..`, without `allow_dangerous_paths=True`.

Common situations: Configs written on a machine with a different directory layout (template lives one level up); prompt packages that reference shared templates across folders; testing with ad-hoc `../` paths.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/33cd8a845f42fb02. Report an issue: GitHub.