Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): fix `VariablePool.get` adding unexpected keys to variable_dictionary (#26767)

QuantumGhostf4c82d0010ed
diff --git a/api/core/workflow/runtime/variable_pool.py b/api/core/workflow/runtime/variable_pool.py
index d41a20dfd7e..7fbaec9e701 100644
--- a/api/core/workflow/runtime/variable_pool.py
+++ b/api/core/workflow/runtime/variable_pool.py
@@ -153,7 +153,11 @@ class VariablePool(BaseModel):
return None
node_id, name = self._selector_to_keys(selector)
- segment: Segment | None = self.variable_dictionary[node_id].get(name)
+ node_map = self.variable_dictionary.get(node_id)
+ if node_map is None:
+ return None
+
+ segment: Segment | None = node_map.get(name)
if segment is None:
return None
diff --git a/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py b/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py
index f9de456b190..18f6753b053 100644
--- a/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py
+++ b/api/tests/unit_tests/core/workflow/entities/test_variable_pool.py
@@ -111,3 +111,26 @@ class TestVariablePoolGetAndNestedAttribute:
assert segment_false is not None
assert isinstance(segment_false, BooleanSegment)
assert segment_false.value is False
+
+
+class TestVariablePoolGetNotModifyVariableDictionary:
+ _NODE_ID = "start"
+ _VAR_NAME = "name"
+
+ def test_convert_to_template_should_not_introduce_extra_keys(self):
+ pool = VariablePool.empty()
+ pool.add([self._NODE_ID, self._VAR_NAME], 0)
+ pool.convert_template("The start.name is {{#start.name#}}")
+ assert "The start" not in pool.variable_dictionary
+
+ def test_get_should_not_modify_variable_dictionary(self):
+ pool = VariablePool.empty()
+ pool.get([self._NODE_ID, self._VAR_NAME])
+ assert len(pool.variable_dictionary) == 1 # only contains `sys` node id
+ assert "start" not in pool.variable_dictionary
+
+ pool = VariablePool.empty()
+ pool.add([self._NODE_ID, self._VAR_NAME], "Joe")
+ pool.get([self._NODE_ID, "count"])
+ start_subdict = pool.variable_dictionary[self._NODE_ID]
+ assert "count" not in start_subdict