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: resolve JSON.parse precision issue causing 'list index out of ra… (#21253)

baonudesifeizhaib9f56852dc72
diff --git a/api/core/helper/code_executor/template_transformer.py b/api/core/helper/code_executor/template_transformer.py
index baa792b5bc6..84f212a9c1d 100644
--- a/api/core/helper/code_executor/template_transformer.py
+++ b/api/core/helper/code_executor/template_transformer.py
@@ -28,7 +28,7 @@ class TemplateTransformer(ABC):
def extract_result_str_from_response(cls, response: str):
result = re.search(rf"{cls._result_tag}(.*){cls._result_tag}", response, re.DOTALL)
if not result:
- raise ValueError("Failed to parse result")
+ raise ValueError(f"Failed to parse result: no result tag found in response. Response: {response[:200]}...")
return result.group(1)
@classmethod
@@ -38,16 +38,53 @@ class TemplateTransformer(ABC):
:param response: response
:return:
"""
+
try:
- result = json.loads(cls.extract_result_str_from_response(response))
- except json.JSONDecodeError:
- raise ValueError("failed to parse response")
+ result_str = cls.extract_result_str_from_response(response)
+ result = json.loads(result_str)
+ except json.JSONDecodeError as e:
+ raise ValueError(f"Failed to parse JSON response: {str(e)}. Response content: {result_str[:200]}...")
+ except ValueError as e:
+ # Re-raise ValueError from extract_result_str_from_response
+ raise e
+ except Exception as e:
+ raise ValueError(f"Unexpected error during response transformation: {str(e)}")
+
+ # Check if the result contains an error
+ if isinstance(result, dict) and "error" in result:
+ raise ValueError(f"JavaScript execution error: {result['error']}")
+
if not isinstance(result, dict):
- raise ValueError("result must be a dict")
+ raise ValueError(f"Result must be a dict, got {type(result).__name__}")
if not all(isinstance(k, str) for k in result):
- raise ValueError("result keys must be strings")
+ raise ValueError("Result keys must be strings")
+
+ # Post-process the result to convert scientific notation strings back to numbers
+ result = cls._post_process_result(result)
return result
+ @classmethod
+ def _post_process_result(cls, result: dict[Any, Any]) -> dict[Any, Any]:
+ """
+ Post-process the result to convert scientific notation strings back to numbers
+ """
+
+ def convert_scientific_notation(value):
+ if isinstance(value, str):
+ # Check if the string looks like scientific notation
+ if re.match(r"^-?\d+\.?\d*e[+-]\d+$", value, re.IGNORECASE):
+ try:
+ return float(value)
+ except ValueError:
+ pass
+ elif isinstance(value, dict):
+ return {k: convert_scientific_notation(v) for k, v in value.items()}
+ elif isinstance(value, list):
+ return [convert_scientific_notation(v) for v in value]
+ return value
+
+ return convert_scientific_notation(result) # type: ignore[no-any-return]
+
@classmethod
@abstractmethod
def get_runner_script(cls) -> str: