langchain-ai/langchain · error · ValueError
Asked to cache, but no cache found at `langchain.cache`.
Error message
Asked to cache, but no cache found at `langchain.cache`.
What it means
`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.
Source
Thrown at libs/core/langchain_core/language_models/chat_models.py:1917
else msg
)
for msg in messages
]
prompt = dumps(normalized_messages)
cache_val = llm_cache.lookup(prompt, llm_string)
if isinstance(cache_val, list):
converted_generations = self._convert_cached_generations(cache_val)
self._replay_v2_events_for_cache_hit(
converted_generations,
run_manager=run_manager,
**kwargs,
)
return ChatResult(generations=converted_generations)
elif self.cache is None:
pass
else:
msg = "Asked to cache, but no cache found at `langchain.cache`."
raise ValueError(msg)
# Apply the rate limiter after checking the cache, since
# we usually don't want to rate limit cache lookups, but
# we do want to rate limit API requests.
if self.rate_limiter:
self.rate_limiter.acquire(blocking=True)
# v2 streaming: preferred over v1 when any attached handler opts in via
# `_V2StreamingCallbackHandler`. Drives the protocol event generator
# (native or `_stream` compat bridge) through the shared helper so
# `on_stream_event` fires per event, then returns a normal `ChatResult`
# so caching / `on_llm_end` stay on the existing generate path.
if self._should_use_protocol_streaming(
async_api=False,
run_manager=run_manager,
**kwargs,
):
stream_accum = ChatModelStream(View on GitHub (pinned to e32fa9a52e)
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.
Example fix
# before
model.invoke("hi", cache=True) # ValueError: no cache configured
# after
from langchain_core.globals import set_llm_cache
from langchain_core.caches import InMemoryCache
set_llm_cache(InMemoryCache())
model.invoke("hi", cache=True) Defensive patterns
Strategy: validation
Validate before calling
from langchain_core.globals import get_llm_cache
if cache_flag and get_llm_cache() is None and cache_obj is None:
from langchain_core.caches import InMemoryCache
from langchain_core.globals import set_llm_cache
set_llm_cache(InMemoryCache()) Try / catch
try:
result = model.invoke(prompt, cache=True)
except ValueError as e:
if "no cache found" in str(e):
set_llm_cache(InMemoryCache())
result = model.invoke(prompt, cache=True)
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- maxsize must be greater than 0
- No global cache was configured. Use `set_llm_cache`.to set a
- Could not resolve content_key {full_path!r}: expected a mapp
- Could not resolve content_key {full_path!r}: missing key {ke
- Unsupported cache value {cache}
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/d7ace6b344393e8f.
Report an issue: GitHub.