{"record":{"id":"d7ace6b344393e8f","repo":"langchain-ai/langchain","slug":"asked-to-cache-but-no-cache-found-at-langchain-c","errorCode":null,"errorMessage":"Asked to cache, but no cache found at `langchain.cache`.","messagePattern":"Asked to cache, but no cache found at `langchain\\.cache`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/language_models/chat_models.py","lineNumber":1917,"sourceCode":"                        else msg\n                    )\n                    for msg in messages\n                ]\n                prompt = dumps(normalized_messages)\n                cache_val = llm_cache.lookup(prompt, llm_string)\n                if isinstance(cache_val, list):\n                    converted_generations = self._convert_cached_generations(cache_val)\n                    self._replay_v2_events_for_cache_hit(\n                        converted_generations,\n                        run_manager=run_manager,\n                        **kwargs,\n                    )\n                    return ChatResult(generations=converted_generations)\n            elif self.cache is None:\n                pass\n            else:\n                msg = \"Asked to cache, but no cache found at `langchain.cache`.\"\n                raise ValueError(msg)\n\n        # Apply the rate limiter after checking the cache, since\n        # we usually don't want to rate limit cache lookups, but\n        # we do want to rate limit API requests.\n        if self.rate_limiter:\n            self.rate_limiter.acquire(blocking=True)\n\n        # v2 streaming: preferred over v1 when any attached handler opts in via\n        # `_V2StreamingCallbackHandler`. Drives the protocol event generator\n        # (native or `_stream` compat bridge) through the shared helper so\n        # `on_stream_event` fires per event, then returns a normal `ChatResult`\n        # so caching / `on_llm_end` stay on the existing generate path.\n        if self._should_use_protocol_streaming(\n            async_api=False,\n            run_manager=run_manager,\n            **kwargs,\n        ):\n            stream_accum = ChatModelStream(","sourceCodeStart":1899,"sourceCodeEnd":1935,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/language_models/chat_models.py#L1899-L1935","documentation":"`ValueError` raised in sync `_generate_with_cache` when the model was asked to cache (`cache=True` on the call or model) but no cache object is set — neither a `cache` argument, a model-level cache, nor the global cache at `langchain_core.globals.get_llm_cache()`. The library refuses to silently skip caching because the caller explicitly requested it.","triggerScenarios":"Calling `.invoke`/`.generate` with `cache=True` (or instantiating `BaseChatModel(cache=True)`) without ever calling `set_llm_cache(InMemoryCache())` (or another `BaseCache`) and without passing a `cache` object.","commonSituations":"Copy-pasting code that relies on a global cache set elsewhere (e.g. in a notebook that was restarted); enabling caching in one process but forgetting in workers; version upgrades where cache setup moved out of `get_chain` helpers.","solutions":["Set a global cache once at startup: `from langchain_core.globals import set_llm_cache; from langchain_core.caches import InMemoryCache; set_llm_cache(InMemoryCache())`.","Or pass a concrete cache per call/model: `model.invoke(prompt, cache=SQLiteCache(...))` — no global needed.","Or set `cache=False` if caching is not actually wanted.","Note in newer versions the global lives in `langchain_core.globals` (not top-level `langchain.cache`); import from the right module."],"exampleFix":"# before\nmodel.invoke(\"hi\", cache=True)  # ValueError: no cache configured\n\n# after\nfrom langchain_core.globals import set_llm_cache\nfrom langchain_core.caches import InMemoryCache\nset_llm_cache(InMemoryCache())\nmodel.invoke(\"hi\", cache=True)","handlingStrategy":"validation","validationCode":"from langchain_core.globals import get_llm_cache\nif cache_flag and get_llm_cache() is None and cache_obj is None:\n    from langchain_core.caches import InMemoryCache\n    from langchain_core.globals import set_llm_cache\n    set_llm_cache(InMemoryCache())","typeGuard":null,"tryCatchPattern":"try:\n    result = model.invoke(prompt, cache=True)\nexcept ValueError as e:\n    if \"no cache found\" in str(e):\n        set_llm_cache(InMemoryCache())\n        result = model.invoke(prompt, cache=True)\n    else:\n        raise","preventionTips":["Set the global cache once in app startup, not per request.","Prefer passing a concrete `BaseCache` instance over `cache=True`.","Assert `get_llm_cache() is not None` in a health check when caching is required."],"tags":["cache","configuration","sync"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}