langchain-ai/langchain · error · ValueError

Invalid file format. Only json or yaml formats are supported

Error message

Invalid file format. Only json or yaml formats are supported.

What it means

In `_load_examples`, when a few-shot config gives `examples` as a string path, the loader opens the file and parses it by suffix: `.json` via `json.load`, `.yaml`/`.yml` via `yaml.safe_load`. Any other suffix raises `ValueError: Invalid file file format...` because there is no parser for it.

Source

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

def _load_examples(
    config: dict[str, Any], *, allow_dangerous_paths: bool = False
) -> dict[str, Any]:
    """Load examples if necessary."""
    if isinstance(config["examples"], list):
        pass
    elif isinstance(config["examples"], str):
        path = Path(config["examples"])
        if not allow_dangerous_paths:
            _validate_path(path)
        with path.open(encoding="utf-8") as f:
            if path.suffix == ".json":
                examples = json.load(f)
            elif path.suffix in {".yaml", ".yml"}:
                examples = yaml.safe_load(f)
            else:
                msg = "Invalid file format. Only json or yaml formats are supported."
                raise ValueError(msg)
        config["examples"] = examples
    else:
        msg = "Invalid examples format. Only list or string are supported."
        raise ValueError(msg)  # noqa:TRY004
    return config


def _load_output_parser(config: dict[str, Any]) -> dict[str, Any]:
    """Load output parser."""
    if config_ := config.get("output_parser"):
        if output_parser_type := config_.get("_type") != "default":
            msg = f"Unsupported output parser {output_parser_type}"
            raise ValueError(msg)
        config["output_parser"] = StrOutputParser(**config_)
    return config


def _load_few_shot_prompt(

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Convert the examples file to `.json` (a JSON array of objects) or `.yaml`/`.yml`.
  2. Inline the examples list directly in the config under `"examples": [ ... ]`.
  3. For JSONL, merge records into one JSON array: `json.dumps([json.loads(l) for l in open(f)])`.

Example fix

# before
# {"_type": "few_shot", "examples": "examples.csv", ...}
load_prompt('prompt.json')  # ValueError

# after
# convert to examples.json: [{"q": "...", "a": "..."}, ...]
# {"_type": "few_shot", "examples": "examples.json", ...}
load_prompt('prompt.json')
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
if isinstance(config.get('examples'), str):
    suffix = Path(config['examples']).suffix
    if suffix not in {'.json', '.yaml', '.yml'}:
        raise ValueError(f'examples file must be .json/.yaml/.yml, got {suffix}')
load_prompt_from_config(config)

Type guard

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

Prevention

When it happens

Trigger: A few-shot prompt config with `"examples": "data/examples.csv"` (or `.txt`, `.jsonl`, no suffix, etc.) loaded via `load_prompt`.

Common situations: Examples maintained in CSV/JSONL by a data team and referenced directly; renaming example files during a refactor; miscounting `.jsonl` as JSON.

Related errors


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