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(segments): Support NoneType. (#6581)

-LAN-2bc0632d0d18
diff --git a/api/core/app/segments/__init__.py b/api/core/app/segments/__init__.py
index e5cecd35fd5..0179d288873 100644
--- a/api/core/app/segments/__init__.py
+++ b/api/core/app/segments/__init__.py
@@ -1,11 +1,12 @@
from .segment_group import SegmentGroup
-from .segments import Segment
+from .segments import NoneSegment, Segment
from .types import SegmentType
from .variables import (
ArrayVariable,
FileVariable,
FloatVariable,
IntegerVariable,
+ NoneVariable,
ObjectVariable,
SecretVariable,
StringVariable,
@@ -23,5 +24,7 @@ __all__ = [
'Variable',
'SegmentType',
'SegmentGroup',
- 'Segment'
+ 'Segment',
+ 'NoneSegment',
+ 'NoneVariable',
]
diff --git a/api/core/app/segments/factory.py b/api/core/app/segments/factory.py
index 4f0b361d956..187042ec038 100644
--- a/api/core/app/segments/factory.py
+++ b/api/core/app/segments/factory.py
@@ -10,6 +10,7 @@ from .variables import (
FileVariable,
FloatVariable,
IntegerVariable,
+ NoneVariable,
ObjectVariable,
SecretVariable,
StringVariable,
@@ -39,6 +40,8 @@ def build_variable_from_mapping(m: Mapping[str, Any], /) -> Variable:
def build_anonymous_variable(value: Any, /) -> Variable:
+ if value is None:
+ return NoneVariable(name='anonymous')
if isinstance(value, str):
return StringVariable(name='anonymous', value=value)
if isinstance(value, int):
diff --git a/api/core/app/segments/segments.py b/api/core/app/segments/segments.py
index b7ca250ff2c..e6bf6cc3a38 100644
--- a/api/core/app/segments/segments.py
+++ b/api/core/app/segments/segments.py
@@ -43,6 +43,23 @@ class Segment(BaseModel):
return self.value
+class NoneSegment(Segment):
+ value_type: SegmentType = SegmentType.NONE
+ value: None = None
+
+ @property
+ def text(self) -> str:
+ return 'null'
+
+ @property
+ def log(self) -> str:
+ return 'null'
+
+ @property
+ def markdown(self) -> str:
+ return 'null'
+
+
class StringSegment(Segment):
value_type: SegmentType = SegmentType.STRING
value: str
diff --git a/api/core/app/segments/types.py b/api/core/app/segments/types.py
index 517f210533e..346b6596016 100644
--- a/api/core/app/segments/types.py
+++ b/api/core/app/segments/types.py
@@ -14,4 +14,5 @@ class SegmentType(str, Enum):
ARRAY_STRING = 'array[string]'
ARRAY_NUMBER = 'array[number]'
ARRAY_OBJECT = 'array[object]'
- ARRAY_FILE = 'array[file]'
\ No newline at end of file
+ ARRAY_FILE = 'array[file]'
+ NONE = 'none'
\ No newline at end of file
diff --git a/api/core/app/segments/variables.py b/api/core/app/segments/variables.py
index e600b442d69..02c06074ab1 100644
--- a/api/core/app/segments/variables.py
+++ b/api/core/app/segments/variables.py
@@ -81,3 +81,8 @@ class SecretVariable(StringVariable):
@property
def log(self) -> str:
return encrypter.obfuscated_token(self.value)
+
+
+class NoneVariable(Variable):
+ value_type: SegmentType = SegmentType.NONE
+ value: None = None
\ No newline at end of file
diff --git a/api/tests/unit_tests/app/test_variables.py b/api/tests/unit_tests/app/test_variables.py
index 05b080bdcff..40872c8d53c 100644
--- a/api/tests/unit_tests/app/test_variables.py
+++ b/api/tests/unit_tests/app/test_variables.py
@@ -2,14 +2,16 @@ import pytest
from pydantic import ValidationError
from core.app.segments import (
+ ArrayVariable,
FloatVariable,
IntegerVariable,
+ NoneVariable,
+ ObjectVariable,
SecretVariable,
SegmentType,
StringVariable,
factory,
)
-from core.app.segments.variables import ArrayVariable, ObjectVariable
def test_string_variable():
@@ -134,3 +136,13 @@ def test_variable_to_object():
assert var.to_object() == 3.14
var = SecretVariable(name='secret', value='secret_value')
assert var.to_object() == 'secret_value'
+
+
+def test_build_a_object_variable_with_none_value():
+ var = factory.build_anonymous_variable(
+ {
+ 'key1': None,
+ }
+ )
+ assert isinstance(var, ObjectVariable)
+ assert isinstance(var.value['key1'], NoneVariable)