{"record":{"id":"bacc307ec85fe98b","repo":"langchain-ai/langchain","slug":"unsupported-cache-value-cache","errorCode":null,"errorMessage":"Unsupported cache value {cache}","messagePattern":"Unsupported cache value (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/language_models/llms.py","lineNumber":155,"sourceCode":"    llm_cache: BaseCache | None\n    if isinstance(cache, BaseCache):\n        llm_cache = cache\n    elif cache is None:\n        llm_cache = get_llm_cache()\n    elif cache is True:\n        llm_cache = get_llm_cache()\n        if llm_cache is None:\n            msg = (\n                \"No global cache was configured. Use `set_llm_cache`.\"\n                \"to set a global cache if you want to use a global cache.\"\n                \"Otherwise either pass a cache object or set cache to False/None\"\n            )\n            raise ValueError(msg)\n    elif cache is False:\n        llm_cache = None\n    else:\n        msg = f\"Unsupported cache value {cache}\"  # type: ignore[unreachable]\n        raise ValueError(msg)\n    return llm_cache\n\n\ndef get_prompts(\n    params: dict[str, Any],\n    prompts: list[str],\n    cache: BaseCache | bool | None = None,  # noqa: FBT001\n) -> tuple[dict[int, list[Generation]], str, list[int], list[str]]:\n    \"\"\"Get prompts that are already cached.\n\n    Args:\n        params: Dictionary of parameters.\n        prompts: List of prompts.\n        cache: Cache object.\n\n    Returns:\n        A tuple of existing prompts, llm_string, missing prompt indexes,\n            and missing prompts.","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/language_models/llms.py#L137-L173","documentation":"`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.","triggerScenarios":"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=`.","commonSituations":"Assuming `cache` takes a path string (common guess from other libraries); forwarding unvalidated config dicts from YAML/env into LLM calls.","solutions":["Pass a `BaseCache` instance: `from langchain_core.caches import SQLiteCache; cache=SQLiteCache(\"./cache.db\")`.","Use `True` with a pre-set global cache, or `False` to disable.","Validate/normalize the `cache` setting in your config loader before it reaches LLM calls."],"exampleFix":"# before\nllm.generate([\"hi\"], cache=\"./cache.db\")  # ValueError\n\n# after\nfrom langchain_core.caches import SQLiteCache\nllm.generate([\"hi\"], cache=SQLiteCache(\"./cache.db\"))","handlingStrategy":"type-guard","validationCode":"from langchain_core.caches import BaseCache\nif cache is not None and cache is not True and cache is not False and not isinstance(cache, BaseCache):\n    raise ValueError(f\"cache must be None/True/False/BaseCache, got {type(cache)}\")","typeGuard":"from langchain_core.caches import BaseCache\ndef is_valid_cache_arg(cache: object) -> bool:\n    return cache is None or cache is True or cache is False or isinstance(cache, BaseCache)","tryCatchPattern":"try:\n    result = llm.generate(prompts, cache=cache_setting)\nexcept ValueError as e:\n    if \"Unsupported cache value\" in str(e):\n        result = llm.generate(prompts, cache=SQLiteCache(str(cache_setting)))\n    else:\n        raise","preventionTips":["Construct cache objects (`SQLiteCache(path)`) instead of passing path strings.","Normalize cache settings in config loaders to `None|True|False|BaseCache`.","Keep enums/explicit flags in configuration rather than raw strings."],"tags":["cache","input-validation","type-mismatch"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}