Skip to content

换行符\n在工作流里代码组件python代码执行报错 #2548

@nfyn

Description

@nfyn
  • 代码组件执行以下脚本:
async def main(args: Args) -> Output:
    params = args.params
    title = params['title']
    summary = params['summary']
    chapters = params['chapters']
    article = title + '\n\n' + summary + '\n\n' + '\n\n'.join(chapters)
    # 构建输出对象
    ret: Output = {
        "article": article
        
    }
    return ret
  • 直接报错,错误信息:
{
  "$error": "Function execution failed, please check the code of the function. Detail: exec failed, stdout=, stderr=Traceback (most recent call last):\n  File \"/lib/python312.zip/_pyodide/_base.py\", line 597, in eval_code_async\n    await CodeRunner(\n          ^^^^^^^^^^^\n  File \"/lib/python312.zip/_pyodide/_base.py\", line 285, in __init__\n    self.ast = next(self._gen)\n               ^^^^^^^^^^^^^^^\n  File \"/lib/python312.zip/_pyodide/_base.py\", line 149, in _parse_and_compile_gen\n    mod = compile(source, filename, mode, flags | ast.PyCF_ONLY_AST)\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"<exec>\", line 13\n    args={\"chapters\": [{\"chapter\": \"# \\u7b2c1\\u7ae0 MCP\\u670d\\u52a1\\u6838\\u5fc3\\u6982\\u5ff5\\u89e3\\u6790 \\ud83e\\udde0\n                                   ^\nSyntaxError: unterminated string literal (detected at line 13)\n, sandbox_err="
}
Image
  • 猜测sandbox在执行代码时,对代码进行了转义,导致\n变成了的代码换行符

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions