The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): handle falsy LLM and chat model caches (#39283)
Hunter Lovellb6eccc5f975e
diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.pyindex bf5e6e58f9..ae75c67604 100644--- a/libs/core/langchain_core/language_models/chat_models.py+++ b/libs/core/langchain_core/language_models/chat_models.py@@ -1888,9 +1888,9 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC):# We should check the cache unless it's explicitly set to False# A None cache means we should use the default global cache# if it's configured.- check_cache = self.cache or self.cache is None+ check_cache = self.cache is not Falseif check_cache:- if llm_cache:+ if llm_cache is not None:llm_string = self._get_llm_string(stop=stop, **kwargs)normalized_messages = [(@@ -2032,7 +2032,7 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC):**result.llm_output,**result.generations[0].message.response_metadata,}- if check_cache and llm_cache:+ if check_cache and llm_cache is not None:llm_cache.update(prompt, llm_string, result.generations)return result@@ -2047,9 +2047,9 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC):# We should check the cache unless it's explicitly set to False# A None cache means we should use the default global cache# if it's configured.- check_cache = self.cache or self.cache is None+ check_cache = self.cache is not Falseif check_cache:- if llm_cache:+ if llm_cache is not None:llm_string = self._get_llm_string(stop=stop, **kwargs)normalized_messages = [(@@ -2189,7 +2189,7 @@ class BaseChatModel(BaseLanguageModel[AIMessage], ABC):**result.llm_output,**result.generations[0].message.response_metadata,}- if check_cache and llm_cache:+ if check_cache and llm_cache is not None:await llm_cache.aupdate(prompt, llm_string, result.generations)return resultdiff --git a/libs/core/langchain_core/language_models/llms.py b/libs/core/langchain_core/language_models/llms.pyindex 14b09ac29b..7903eec452 100644--- a/libs/core/langchain_core/language_models/llms.py+++ b/libs/core/langchain_core/language_models/llms.py@@ -182,13 +182,13 @@ def get_prompts(llm_cache = _resolve_cache(cache=cache)for i, prompt in enumerate(prompts):- if llm_cache:+ if llm_cache is not None:cache_val = llm_cache.lookup(prompt, llm_string)if isinstance(cache_val, list):existing_prompts[i] = cache_val- else:- missing_prompts.append(prompt)- missing_prompt_idxs.append(i)+ continue+ missing_prompts.append(prompt)+ missing_prompt_idxs.append(i)return existing_prompts, llm_string, missing_prompt_idxs, missing_prompts@@ -217,13 +217,13 @@ async def aget_prompts(existing_prompts = {}llm_cache = _resolve_cache(cache=cache)for i, prompt in enumerate(prompts):- if llm_cache:+ if llm_cache is not None:cache_val = await llm_cache.alookup(prompt, llm_string)if isinstance(cache_val, list):existing_prompts[i] = cache_val- else:- missing_prompts.append(prompt)- missing_prompt_idxs.append(i)+ continue+ missing_prompts.append(prompt)+ missing_prompt_idxs.append(i)return existing_prompts, llm_string, missing_prompt_idxs, missing_prompts@@ -288,7 +288,7 @@ async def aupdate_cache(for i, result in enumerate(new_results.generations):existing_prompts[missing_prompt_idxs[i]] = resultprompt = prompts[missing_prompt_idxs[i]]- if llm_cache:+ if llm_cache is not None:await llm_cache.aupdate(prompt, llm_string, result)return new_results.llm_outputdiff --git a/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py b/libs/core/tests/unit_tests/language_models/chat_models/test_cache.pyindex 804c7c7c90..7715f184b5 100644--- a/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py+++ b/libs/core/tests/unit_tests/language_models/chat_models/test_cache.py@@ -38,6 +38,10 @@ class InMemoryCache(BaseCache):"""Clear cache."""self._cache = {}+ def __len__(self) -> int:+ """Return the number of cached entries."""+ return len(self._cache)+def test_local_cache_sync() -> None:"""Test that the local cache is being populated but not the global one."""diff --git a/libs/core/tests/unit_tests/language_models/llms/test_cache.py b/libs/core/tests/unit_tests/language_models/llms/test_cache.pyindex 720c247a9c..e3379682c6 100644--- a/libs/core/tests/unit_tests/language_models/llms/test_cache.py+++ b/libs/core/tests/unit_tests/language_models/llms/test_cache.py@@ -27,6 +27,10 @@ class InMemoryCache(BaseCache):"""Clear cache."""self._cache = {}+ def __len__(self) -> int:+ """Return the number of cached entries."""+ return len(self._cache)+async def test_local_cache_generate_async() -> None:global_cache = InMemoryCache()