{"record":{"id":"fc6ac8f455d527b1","repo":"iflytek/astron-agent","slug":"code-node-result-type-error","errorCode":"CODE_NODE_RESULT_TYPE_ERROR","errorMessage":"Code return type is not str, please check the type!","messagePattern":"Code return type is not str, please check the type!","errorType":"error_code","errorClass":"CustomException","httpStatus":null,"severity":"error","filePath":"core/workflow/engine/nodes/code/code_node.py","lineNumber":229,"sourceCode":"        outputs = {}\n        for var_name in self.output_identifier:\n            var_type = variable_pool.get_output_schema(\n                node_id=self.node_id, key_name=var_name\n            ).get(\"type\")\n\n            # If variable not in result, use existing value from variable pool\n            if var_name not in code_result_dict:\n                final_result = variable_pool.get_variable(\n                    node_id=self.node_id, key_name=var_name, span=span\n                )\n                outputs.update({var_name: final_result})\n                continue\n\n            # Type validation based on expected output schema\n            match var_type:\n                case \"string\":\n                    if isinstance(code_result_dict[var_name], str) is False:\n                        raise CustomException(\n                            CodeEnum.CODE_NODE_RESULT_TYPE_ERROR,\n                            \"Code return type is not str, please check the type!\",\n                        )\n                case \"integer\":\n                    if isinstance(code_result_dict[var_name], int) is False:\n                        raise CustomException(\n                            CodeEnum.CODE_NODE_RESULT_TYPE_ERROR,\n                            \"Code return type is not integer, please check the type!\",\n                        )\n\n                case \"number\":\n                    if isinstance(code_result_dict[var_name], (int, float)) is False:\n                        raise CustomException(\n                            CodeEnum.CODE_NODE_RESULT_TYPE_ERROR,\n                            \"Code return type is not integer or float, please check the type!\",\n                        )\n\n                case \"boolean\":","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/workflow/engine/nodes/code/code_node.py#L211-L247","documentation":"After the sandboxed code runs, _check_and_set_variable_pool validates each declared output variable against the node's output schema. When an output variable is declared with type \"string\" but the returned dict holds a non-str value (int, list, None, etc.), it raises CODE_NODE_RESULT_TYPE_ERROR. This catches contract drift between what the code node's output schema promises and what the script actually returns.","triggerScenarios":"The code's main() returns a dict whose value for an output variable declared as \"string\" is not a Python str — e.g. returns 42 for a field typed string, or None when a branch fails to set the value.","commonSituations":"User edits the output schema to string but forgets to str() the value in code; JSON-parsed numbers passed through as-is; None returned on an error path; language coercion differences inside the sandbox (e.g. numbers for IDs like zip codes).","solutions":["Open the code node and coerce the offending variable to str before returning: `return {\"result\": str(value)}`.","Or change the output variable type in the node schema to match the actual runtime type (e.g. integer).","Add explicit checks in the script for None/error paths so the variable is always a string.","Re-run the code node in debug mode to inspect the raw returned dict and confirm each declared type.","Note: bool is a subclass of int but not of str — ensure string outputs aren't accidentally booleans."],"exampleFix":"// before\ndef main() -> dict:\n    return {\"name\": 123}\n\n// after\ndef main() -> dict:\n    return {\"name\": str(123)}","handlingStrategy":"type-guard","validationCode":"outputs = {\"name\": value}\nfor k, t in declared_schema.items():\n    if t == \"string\" and not isinstance(outputs.get(k), str):\n        raise TypeError(f\"output '{k}' must be str, got {type(outputs[k]).__name__}\")","typeGuard":"def is_str_output(v) -> bool:\n    return isinstance(v, str)","tryCatchPattern":"try:\n    result = code_node.async_execute(...)\nexcept CustomException as e:\n    if e.err_code == CodeEnum.CODE_NODE_RESULT_TYPE_ERROR:\n        log.error(\"code node output schema mismatch: coerce output to declared type\")\n        raise","preventionTips":["Coerce every return value to its declared type before returning from main()","Keep the node's output schema and the script's return statement in sync after edits","Handle None/error branches explicitly so declared variables are always set","Debug-run the node and print types of returned values"],"tags":["type-error","code-node","validation","workflow"],"backgroundTag":"type-mismatch","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}