From 3fbb737bb3642baa90a67977486401bb55df0ffb Mon Sep 17 00:00:00 2001 From: Mohamad Zamini <32536264+mzamini92@users.noreply.github.com> Date: Mon, 31 Jul 2023 19:15:00 -0600 Subject: [PATCH] Update combined.py (#7541) from my understanding, the `check_repeated_memory_variable` validator will raise an error if any of the variables in the `memories` list are repeated. However, the `load_memory_variables` method does not check for repeated variables. This means that it is possible for the `CombinedMemory` instance to return a dictionary of memory variables that contains duplicate values. This code will check for repeated variables in the `data` dictionary returned by the `load_memory_variables` method of each sub-memory. If a repeated variable is found, an error will be raised. --------- Co-authored-by: Harrison Chase --- libs/langchain/langchain/memory/combined.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/langchain/langchain/memory/combined.py b/libs/langchain/langchain/memory/combined.py index 1c46c184d4..80316755ae 100644 --- a/libs/langchain/langchain/memory/combined.py +++ b/libs/langchain/langchain/memory/combined.py @@ -61,10 +61,12 @@ class CombinedMemory(BaseMemory): # Collect vars from all sub-memories for memory in self.memories: data = memory.load_memory_variables(inputs) - memory_data = { - **memory_data, - **data, - } + for key, value in data.items(): + if key in memory_data: + raise ValueError( + f"The variable {key} is repeated in the CombinedMemory." + ) + memory_data[key] = value return memory_data