langchain-ai/langchain · error · ValueError

Unsupported cache value {cache}

Error message

Unsupported cache value {cache}

What it means

`ValueError` from `get_cache` in llms.py: the `cache` argument has an unsupported value. Only `None` (use global), `True` (require global), `False` (no cache), or a `BaseCache` instance are accepted; anything else — a string path, a dict, an int — falls into the unreachable-typed else branch.

Source

Thrown at libs/core/langchain_core/language_models/llms.py:155

    llm_cache: BaseCache | None
    if isinstance(cache, BaseCache):
        llm_cache = cache
    elif cache is None:
        llm_cache = get_llm_cache()
    elif cache is True:
        llm_cache = get_llm_cache()
        if llm_cache is None:
            msg = (
                "No global cache was configured. Use `set_llm_cache`."
                "to set a global cache if you want to use a global cache."
                "Otherwise either pass a cache object or set cache to False/None"
            )
            raise ValueError(msg)
    elif cache is False:
        llm_cache = None
    else:
        msg = f"Unsupported cache value {cache}"  # type: ignore[unreachable]
        raise ValueError(msg)
    return llm_cache


def get_prompts(
    params: dict[str, Any],
    prompts: list[str],
    cache: BaseCache | bool | None = None,  # noqa: FBT001
) -> tuple[dict[int, list[Generation]], str, list[int], list[str]]:
    """Get prompts that are already cached.

    Args:
        params: Dictionary of parameters.
        prompts: List of prompts.
        cache: Cache object.

    Returns:
        A tuple of existing prompts, llm_string, missing prompt indexes,
            and missing prompts.

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Pass a `BaseCache` instance: `from langchain_core.caches import SQLiteCache; cache=SQLiteCache("./cache.db")`.
  2. Use `True` with a pre-set global cache, or `False` to disable.
  3. Validate/normalize the `cache` setting in your config loader before it reaches LLM calls.

Example fix

# before
llm.generate(["hi"], cache="./cache.db")  # ValueError

# after
from langchain_core.caches import SQLiteCache
llm.generate(["hi"], cache=SQLiteCache("./cache.db"))
Defensive patterns

Strategy: type-guard

Validate before calling

from langchain_core.caches import BaseCache
if cache is not None and cache is not True and cache is not False and not isinstance(cache, BaseCache):
    raise ValueError(f"cache must be None/True/False/BaseCache, got {type(cache)}")

Type guard

from langchain_core.caches import BaseCache
def is_valid_cache_arg(cache: object) -> bool:
    return cache is None or cache is True or cache is False or isinstance(cache, BaseCache)

Try / catch

try:
    result = llm.generate(prompts, cache=cache_setting)
except ValueError as e:
    if "Unsupported cache value" in str(e):
        result = llm.generate(prompts, cache=SQLiteCache(str(cache_setting)))
    else:
        raise

Prevention

When it happens

Trigger: Calling `llm.generate(prompts, cache="./cache.db")` or `cache={}` / `cache=1`, expecting the value to configure a cache. Also from custom code forwarding arbitrary user kwargs into `cache=`.

Common situations: Assuming `cache` takes a path string (common guess from other libraries); forwarding unvalidated config dicts from YAML/env into LLM calls.

Related errors


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