langchain-ai/langchain · error · ValueError

Got unsupported file type {file_path.suffix}

Error message

Got unsupported file type {file_path.suffix}

What it means

`_load_prompt_from_file` dispatches on the file suffix: `.json` is parsed with `json.load`, `.yaml`/`.yml` with `yaml.safe_load`; every other suffix raises `ValueError: Got unsupported file type {suffix}`. Prompt files on disk must therefore be JSON or YAML.

Source

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

def _load_prompt_from_file(
    file: str | Path,
    encoding: str | None = None,
    *,
    allow_dangerous_paths: bool = False,
) -> BasePromptTemplate[str]:
    """Load prompt from file."""
    # Convert file to a Path object.
    file_path = Path(file)
    # Load from either json or yaml.
    if file_path.suffix == ".json":
        with file_path.open(encoding=encoding) as f:
            config = json.load(f)
    elif file_path.suffix.endswith((".yaml", ".yml")):
        with file_path.open(encoding=encoding) as f:
            config = yaml.safe_load(f)
    else:
        msg = f"Got unsupported file type {file_path.suffix}"
        raise ValueError(msg)
    # Load the prompt from the config now.
    return load_prompt_from_config(config, allow_dangerous_paths=allow_dangerous_paths)


def _load_chat_prompt(
    config: dict[str, Any],
    *,
    allow_dangerous_paths: bool = False,  # noqa: ARG001
) -> ChatPromptTemplate:
    """Load chat prompt from config."""
    messages = config.pop("messages")
    template = messages[0]["prompt"].pop("template") if messages else None
    config.pop("input_variables")

    if not template:
        msg = "Can't load chat prompt without template"
        raise ValueError(msg)

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Wrap the template in a proper config: `{"_type": "prompt", "template": "<contents>", "input_variables": [...]}` and save as `.json` or `.yaml`.
  2. Fix the extension (e.g. remove a stray `.txt` so the file ends in `.json`).
  3. For plain template text, load it in code: `PromptTemplate.from_file('prompt.txt')` instead of `load_prompt`.

Example fix

# before
load_prompt('greeting.txt')  # ValueError: Got unsupported file type .txt

# after
from langchain_core.prompts import PromptTemplate
prompt = PromptTemplate.from_file('greeting.txt')  # raw template file
# or write greeting.json: {"_type": "prompt", "template": "Hi {name}", "input_variables": ["name"]}
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
suffix = Path(path).suffix
if suffix not in {'.json', '.yaml', '.yml'}:
    raise ValueError(f'prompt file must be .json/.yaml/.yml, got {suffix!r}')
load_prompt(path)

Type guard

def is_supported_prompt_file(path: str | Path) -> bool:
    return Path(path).suffix in {'.json', '.yaml', '.yml'}

Prevention

When it happens

Trigger: `load_prompt('prompt.toml')`, `load_prompt('prompt.txt')`, `load_prompt('prompt')` (no suffix), or a misnamed file like `prompt.json.txt`.

Common situations: Naming mistakes (double extensions from download/save dialogs); attempting to load a raw `.txt` template file directly; config tooling writing `.toml`/`.ini`.

Related errors


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