Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): accept string variable selectors in VariableAggregator (#19413)

Harsh Kashyape4c15a0b85d3
diff --git a/agent/component/variable_aggregator.py b/agent/component/variable_aggregator.py
index 903410cd05..d46a4997c9 100644
--- a/agent/component/variable_aggregator.py
+++ b/agent/component/variable_aggregator.py
@@ -13,11 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
-from typing import Any
import os
+from typing import Any
-from common.connection_utils import timeout
from agent.component.base import ComponentBase, ComponentParamBase
+from common.connection_utils import timeout
class VariableAggregatorParam(ComponentParamBase):
@@ -40,7 +40,7 @@ class VariableAggregatorParam(ComponentParamBase):
if not g.get("variables"):
raise ValueError(f"[VariableAggregator] variables of group `{g.get('group_name')}` can not be empty")
if not isinstance(g.get("variables"), list):
- raise ValueError(f"[VariableAggregator] variables of group `{g.get('group_name')}` should be a list of strings")
+ raise TypeError(f"[VariableAggregator] variables of group `{g.get('group_name')}` should be a list of strings")
def get_input_form(self) -> dict[str, dict]:
return {
@@ -57,7 +57,7 @@ class VariableAggregator(ComponentBase):
def param_refs(self) -> list[str]:
return [selector.get("value") if isinstance(selector, dict) else selector for group in self._param.groups for selector in group.get("variables", [])]
- @timeout(int(os.environ.get("COMPONENT_EXEC_TIMEOUT", 3)))
+ @timeout(int(os.environ.get("COMPONENT_EXEC_TIMEOUT", "3")))
def _invoke(self, **kwargs):
# Group mode: for each group, pick the first available variable
for group in self._param.groups:
@@ -66,7 +66,16 @@ class VariableAggregator(ComponentBase):
# record candidate selectors within this group
self.set_input_value(f"{gname}.variables", list(group.get("variables", [])))
for selector in group.get("variables", []):
- val = self._canvas.get_variable_value(selector["value"])
+ # Selectors may be {"value": ref} dicts (UI form) or plain
+ # reference strings (SDK/API-built canvases); param_refs
+ # already accepts both, so resolve them the same way here.
+ ref = selector.get("value") if isinstance(selector, dict) else selector
+ if not isinstance(ref, str):
+ continue
+ ref = ref.strip("{}").strip()
+ if not ref:
+ continue
+ val = self._canvas.get_variable_value(ref)
if val:
self.set_output(gname, val)
break
@@ -76,7 +85,7 @@ class VariableAggregator(ComponentBase):
# Try to convert value to serializable object if it has to_object()
try:
return value.to_object() # type: ignore[attr-defined]
- except Exception:
+ except (AttributeError, TypeError):
return value
def thoughts(self) -> str:
diff --git a/internal/agent/component/variable_aggregator.go b/internal/agent/component/variable_aggregator.go
index 40b824593f..18c23875fe 100644
--- a/internal/agent/component/variable_aggregator.go
+++ b/internal/agent/component/variable_aggregator.go
@@ -30,6 +30,7 @@ package component
import (
"context"
"fmt"
+ "strings"
"ragflow/internal/agent/runtime"
@@ -42,7 +43,8 @@ const componentNameVariableAggregator = "VariableAggregator"
// It mirrors the Python VariableAggregatorParam surface.
type variableAggregatorParam struct {
// Groups is a list of {group_name, variables} dicts. Each
- // group.variables entry is itself a {value: <ref-string>} dict.
+ // group.variables entry may be a plain ref string or a
+ // {value: <ref-string>} dict.
Groups []map[string]any `json:"groups"`
}
@@ -212,11 +214,7 @@ func (v *VariableAggregatorComponent) Invoke(ctx context.Context, db *gorm.DB, i
}
selectors, _ := g["variables"].([]any)
for _, raw := range selectors {
- sel, ok := raw.(map[string]any)
- if !ok {
- continue
- }
- ref, _ := sel["value"].(string)
+ ref := normalizeSelectorRef(raw)
if ref == "" {
continue
}
@@ -231,6 +229,23 @@ func (v *VariableAggregatorComponent) Invoke(ctx context.Context, db *gorm.DB, i
return out, nil
}
+// normalizeSelectorRef accepts selector maps from the UI and plain strings
+// from SDK/API-built canvases. Invalid or empty selectors are skipped.
+func normalizeSelectorRef(raw any) string {
+ var ref string
+ switch sel := raw.(type) {
+ case string:
+ ref = sel
+ case map[string]any:
+ ref, _ = sel["value"].(string)
+ default:
+ return ""
+ }
+ ref = strings.TrimSpace(ref)
+ ref = strings.Trim(ref, "{}")
+ return strings.TrimSpace(ref)
+}
+
// Stream mirrors Invoke; VariableAggregator is a single-shot reduce.
func (v *VariableAggregatorComponent) Stream(ctx context.Context, db *gorm.DB, inputs map[string]any) (<-chan map[string]any, error) {
out, err := v.Invoke(ctx, db, inputs)
diff --git a/internal/agent/component/variable_aggregator_test.go b/internal/agent/component/variable_aggregator_test.go
index 48fda966ff..4432d7aadf 100644
--- a/internal/agent/component/variable_aggregator_test.go
+++ b/internal/agent/component/variable_aggregator_test.go
@@ -211,3 +211,36 @@ func TestVariableAggregator_Registered(t *testing.T) {
t.Errorf("Name()=%q, want VariableAggregator", c.Name())
}
}
+
+func TestVariableAggregator_StringSelectors(t *testing.T) {
+ state := canvas.NewCanvasState("run-str", "task-str")
+ state.Outputs["cpn_1"] = map[string]any{"y": "from-string"}
+ ctx := canvas.WithState(t.Context(), state)
+ groups := []map[string]any{{"group_name": "g", "variables": []any{"cpn_1@y"}}}
+ c, err := NewVariableAggregatorComponent(map[string]any{"groups": groups})
+ if err != nil {
+ t.Fatalf("NewVariableAggregatorComponent: %v", err)
+ }
+ out, err := c.Invoke(ctx, nil, nil)
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+ if got, want := out["g"], "from-string"; got != want {
+ t.Errorf("g: got %v, want %v", got, want)
+ }
+}
+
+func TestNormalizeSelectorRef(t *testing.T) {
+ cases := []struct {
+ in any
+ want string
+ }{
+ {"a@x", "a@x"}, {" a@x ", "a@x"}, {"{a@x}", "a@x"}, {" {a@x} ", "a@x"},
+ {map[string]any{"value": "a@x"}, "a@x"}, {"", ""}, {map[string]any{"value": ""}, ""}, {map[string]any{}, ""}, {nil, ""}, {42, ""},
+ }
+ for _, tc := range cases {
+ if got := normalizeSelectorRef(tc.in); got != tc.want {
+ t.Errorf("normalizeSelectorRef(%v)=%q, want %q", tc.in, got, tc.want)
+ }
+ }
+}
diff --git a/test/unit_test/agent/component/test_variable_aggregator_selectors.py b/test/unit_test/agent/component/test_variable_aggregator_selectors.py
new file mode 100644
index 0000000000..352987b5c7
--- /dev/null
+++ b/test/unit_test/agent/component/test_variable_aggregator_selectors.py
@@ -0,0 +1,153 @@
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""Regression test for selector handling in `agent/component/variable_aggregator.py`.
+
+`VariableAggregatorParam.param_refs` accepts variable selectors either
+as `{"value": ref}` dicts (the UI form) or as plain reference strings
+(SDK/API-built canvases), but `_invoke` indexed `selector["value"]`
+unconditionally and crashed with `TypeError: string indices must be
+integers` on the string form. `_invoke` now normalizes both forms the
+same way `param_refs` does.
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import sys
+from pathlib import Path
+from types import ModuleType
+from unittest.mock import MagicMock
+
+
+def _load_variable_aggregator_module(monkeypatch):
+ """Load `agent.component.variable_aggregator` with heavy deps stubbed."""
+ repo_root = Path(__file__).resolve().parents[4]
+
+ quart_stub = ModuleType("quart")
+ quart_stub.make_response = MagicMock()
+ quart_stub.jsonify = MagicMock()
+ monkeypatch.setitem(sys.modules, "quart", quart_stub)
+
+ def _timeout(*args, **kwargs):
+ def deco(fn):
+ return fn
+
+ return deco
+
+ for name in ("api", "api.utils"):
+ pkg = ModuleType(name)
+ pkg.__path__ = []
+ monkeypatch.setitem(sys.modules, name, pkg)
+ api_utils_mod = ModuleType("api.utils.api_utils")
+ api_utils_mod.timeout = _timeout
+ monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)
+
+ constants_mod = ModuleType("common.constants")
+
+ class _RetCode:
+ SUCCESS = 0
+ EXCEPTION_ERROR = 100
+
+ constants_mod.RetCode = _RetCode
+ monkeypatch.setitem(sys.modules, "common.constants", constants_mod)
+
+ common_pkg = ModuleType("common")
+ common_pkg.__path__ = [str(repo_root / "common")]
+ monkeypatch.setitem(sys.modules, "common", common_pkg)
+ for name in ("connection_utils", "misc_utils"):
+ spec = importlib.util.spec_from_file_location(f"common.{name}", repo_root / "common" / f"{name}.py")
+ mod = importlib.util.module_from_spec(spec)
+ monkeypatch.setitem(sys.modules, f"common.{name}", mod)
+ spec.loader.exec_module(mod)
+
+ agent_pkg = ModuleType("agent")
+ agent_pkg.__path__ = [str(repo_root / "agent")]
+ monkeypatch.setitem(sys.modules, "agent", agent_pkg)
+ component_pkg = ModuleType("agent.component")
+ component_pkg.__path__ = [str(repo_root / "agent" / "component")]
+ monkeypatch.setitem(sys.modules, "agent.component", component_pkg)
+
+ base_spec = importlib.util.spec_from_file_location("agent.component.base", repo_root / "agent" / "component" / "base.py")
+ base_mod = importlib.util.module_from_spec(base_spec)
+ monkeypatch.setitem(sys.modules, "agent.component.base", base_mod)
+ base_spec.loader.exec_module(base_mod)
+
+ spec = importlib.util.spec_from_file_location("agent.component.variable_aggregator", repo_root / "agent" / "component" / "variable_aggregator.py")
+ mod = importlib.util.module_from_spec(spec)
+ monkeypatch.setitem(sys.modules, "agent.component.variable_aggregator", mod)
+ spec.loader.exec_module(mod)
+ return mod
+
+
+class _Canvas:
+ def __init__(self, variables=None):
+ self.variables = variables or {}
+
+ def is_canceled(self):
+ return False
+
+ def get_variable_value(self, key):
+ return self.variables.get(key)
+
+
+def _component(mod, canvas, groups):
+ cpn = mod.VariableAggregator.__new__(mod.VariableAggregator)
+ cpn._canvas = canvas
+ cpn._id = "aggregator"
+ param = mod.VariableAggregatorParam()
+ param.groups = groups
+ cpn._param = param
+ return cpn
+
+
+def test_string_selectors_resolve(monkeypatch):
+ mod = _load_variable_aggregator_module(monkeypatch)
+ cpn = _component(mod, _Canvas({"a@x": "hello"}), [{"group_name": "G", "variables": ["a@x"]}])
+
+ cpn._invoke()
+
+ assert cpn.output("G") == "hello"
+
+
+def test_dict_selectors_still_resolve(monkeypatch):
+ mod = _load_variable_aggregator_module(monkeypatch)
+ cpn = _component(mod, _Canvas({"a@x": "hello"}), [{"group_name": "G", "variables": [{"value": "a@x"}]}])
+
+ cpn._invoke()
+
+ assert cpn.output("G") == "hello"
+
+
+def test_mixed_selectors_pick_first_available(monkeypatch):
+ mod = _load_variable_aggregator_module(monkeypatch)
+ cpn = _component(
+ mod,
+ _Canvas({"b@y": "fallback"}),
+ [{"group_name": "G", "variables": [{"value": "a@x"}, "b@y"]}],
+ )
+
+ cpn._invoke()
+
+ assert cpn.output("G") == "fallback"
+
+
+def test_whitespace_only_selector_is_skipped(monkeypatch):
+ mod = _load_variable_aggregator_module(monkeypatch)
+ cpn = _component(mod, _Canvas({"b@y": "fallback"}), [{"group_name": "G", "variables": [" ", "b@y"]}])
+
+ cpn._invoke()
+
+ assert cpn.output("G") == "fallback"