From dda39edd6d635549faab1d770a587dbf38bc9cfc Mon Sep 17 00:00:00 2001 From: FarhanChowdhury248 Date: Fri, 8 Nov 2024 08:30:54 -0500 Subject: [PATCH 1/3] added check for cached result being of type ChatGeneration[] --- .../language_models/chat_models.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py index 39fd11c247f9f..0d800acbda460 100644 --- a/libs/core/langchain_core/language_models/chat_models.py +++ b/libs/core/langchain_core/language_models/chat_models.py @@ -815,7 +815,14 @@ def _generate_with_cache( prompt = dumps(messages) cache_val = llm_cache.lookup(prompt, llm_string) if isinstance(cache_val, list): - return ChatResult(generations=cache_val) + # When objects that are not Serializable are cached, + # they may not be succesfully serialized upon read. + # Currently, many caches failover to returning a list + # of Generation objects in this case, which is invalid + # for ChatResult, so we check and treat this as an + # uncached case. + if len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): + return ChatResult(generations=cache_val) elif self.cache is None: pass else: @@ -888,7 +895,14 @@ async def _agenerate_with_cache( prompt = dumps(messages) cache_val = await llm_cache.alookup(prompt, llm_string) if isinstance(cache_val, list): - return ChatResult(generations=cache_val) + # When objects that are not Serializable are cached, + # they may not be succesfully serialized upon read. + # Currently, many caches failover to returning a list + # of Generation objects in this case, which is invalid + # for ChatResult, so we check and treat this as an + # uncached case. + if len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): + return ChatResult(generations=cache_val) elif self.cache is None: pass else: From 8094184fc3851034cfe3771f4fcf83d784986d7f Mon Sep 17 00:00:00 2001 From: FarhanChowdhury248 Date: Fri, 8 Nov 2024 09:12:41 -0500 Subject: [PATCH 2/3] fixed linting issue --- .../language_models/chat_models.py | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py index 0d800acbda460..c4f0d59999a0e 100644 --- a/libs/core/langchain_core/language_models/chat_models.py +++ b/libs/core/langchain_core/language_models/chat_models.py @@ -814,15 +814,14 @@ def _generate_with_cache( llm_string = self._get_llm_string(stop=stop, **kwargs) prompt = dumps(messages) cache_val = llm_cache.lookup(prompt, llm_string) - if isinstance(cache_val, list): - # When objects that are not Serializable are cached, - # they may not be succesfully serialized upon read. - # Currently, many caches failover to returning a list - # of Generation objects in this case, which is invalid - # for ChatResult, so we check and treat this as an - # uncached case. - if len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): - return ChatResult(generations=cache_val) + # When objects that are not Serializable are cached, + # they may not be succesfully serialized upon read. + # Currently, many caches failover to returning a list + # of Generation objects in this case, which is invalid + # for ChatResult, so we check and treat this as an + # uncached case. + if isinstance(cache_val, list) and len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): + return ChatResult(generations=cache_val) elif self.cache is None: pass else: @@ -894,15 +893,14 @@ async def _agenerate_with_cache( llm_string = self._get_llm_string(stop=stop, **kwargs) prompt = dumps(messages) cache_val = await llm_cache.alookup(prompt, llm_string) - if isinstance(cache_val, list): - # When objects that are not Serializable are cached, - # they may not be succesfully serialized upon read. - # Currently, many caches failover to returning a list - # of Generation objects in this case, which is invalid - # for ChatResult, so we check and treat this as an - # uncached case. - if len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): - return ChatResult(generations=cache_val) + # When objects that are not Serializable are cached, + # they may not be succesfully serialized upon read. + # Currently, many caches failover to returning a list + # of Generation objects in this case, which is invalid + # for ChatResult, so we check and treat this as an + # uncached case. + if isinstance(cache_val, list) and len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): + return ChatResult(generations=cache_val) elif self.cache is None: pass else: From 54a99785d7677bc55cf09da37171c23a880a4eaf Mon Sep 17 00:00:00 2001 From: FarhanChowdhury248 Date: Fri, 8 Nov 2024 09:23:55 -0500 Subject: [PATCH 3/3] reduced long lines --- .../langchain_core/language_models/chat_models.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libs/core/langchain_core/language_models/chat_models.py b/libs/core/langchain_core/language_models/chat_models.py index c4f0d59999a0e..cffc80171e6be 100644 --- a/libs/core/langchain_core/language_models/chat_models.py +++ b/libs/core/langchain_core/language_models/chat_models.py @@ -820,7 +820,11 @@ def _generate_with_cache( # of Generation objects in this case, which is invalid # for ChatResult, so we check and treat this as an # uncached case. - if isinstance(cache_val, list) and len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): + if ( + isinstance(cache_val, list) + and len(cache_val) > 0 + and isinstance(cache_val[0], ChatGeneration) + ): return ChatResult(generations=cache_val) elif self.cache is None: pass @@ -899,7 +903,11 @@ async def _agenerate_with_cache( # of Generation objects in this case, which is invalid # for ChatResult, so we check and treat this as an # uncached case. - if isinstance(cache_val, list) and len(cache_val) > 0 and isinstance(cache_val[0], ChatGeneration): + if ( + isinstance(cache_val, list) + and len(cache_val) > 0 + and isinstance(cache_val[0], ChatGeneration) + ): return ChatResult(generations=cache_val) elif self.cache is None: pass