
本文详解 YAML 中长字符串(尤其是含换行、特殊符号的提示词)的规范写法,指出三重引号 """ 的常见误用,并推荐使用字面量块标量(|)或折叠块标量(>)来安全表达多行内容。
本文详解 yaml 中长字符串(尤其是含换行、特殊符号的提示词)的规范写法,指出三重引号 `"""` 的常见误用,并推荐使用字面量块标量(`|`)或折叠块标量(`>`)来安全表达多行内容。
YAML 对字符串格式有严格语法规则,尤其在处理包含换行、缩进和特殊字符(如 \n、=>、{})的长提示词(Prompt)时,极易因格式不当导致解析失败。常见错误是误将 Python 的三重引号("""...""")直接套用于 YAML——YAML 并不支持该语法,连续双引号 "" 后紧跟另一个 "..." 会被解析器视为语法冲突,触发类似 expected <block end>, but found '<scalar>' 的解析错误。
✅ 正确做法是根据实际需求选择合适的 YAML 多行字符串风格:
- 字面量块标量(|):保留所有换行符和内部空白,适合需严格保持格式的提示词(如带缩进的示例输入/输出)。注意:\n 在此模式下为普通字符(即字面量 \ + n),不可用于转义换行;换行直接通过回车实现。
- 折叠块标量(>):将内部换行折叠为空格,适合段落式文本;同样不解析 \n,但更紧凑。
以下是修正后的 config.yml 片段(使用 | 风格,确保提示结构完整、可读且合法):
WORKFLOW:
EXTRACT_SUMMARIZATION:
PROMPT_SUMMARY: |
Extracting the timestamps of the goals scored by players in a football match using the commentator's description.
The structure of input is:
timestamp => text
timestamp => text
timestamp => text
timestamp => text
The output only consists of timestamps and does not include any text.
Example:
Input:
0:01 => [Applause]
0:04 => Welcome, ladies and gentlemen, to the match between Liverpool and Chelsea.
0:14 => Silva has scored, and the score is 1-0 in favor of Liverpool.
0:20 => So, there has been a foul committed by a Liverpool player.
0:25 => The referee has blown the whistle to end the first half.
0:30 => Chelsea has equalized through Ch.
0:45 => Tr has put Chelsea in the lead.
0:50 => So, the match has...
0:55 => ended, and the victory goes to Chelsea.
Output: 0:14, 0:30, 0:45
This is an incorrect format of output:
0:14 => Silva has scored, and the score is 1-0 in favor of Liverpool.
0:45 => Tr has put Chelsea in the lead.
This is another incorrect format of output:
0:01 => [Applause]
0:04 => 0:14 => 0:25 =>
Do it for the following text:
Input:
{}
Output:
SCALE_TIME_BEFORE: 6
SCALE_TIME_AFTER: 58
ABSTRACT_SUMMARIZATION:
PROMPT: "Rewrite the following sentences to form a paragraph: {}"
COMBINE_CLIP:
SPEECH_RATE: 130
SUB_VIDEO:
FORCE_DOWNLOAD: true
USE_IMAGEIO: false⚠️ 关键注意事项:
- 避免在线校验工具:许多 YAML 在线校验器(如 yaml-validator)基于过时或非标准解析器,可能给出误导性错误提示;建议使用权威库(如 Python 的 ruamel.yaml)本地验证。
- 文件扩展名统一用 .yaml:官方推荐 .yaml 而非 .yml,虽二者均被广泛支持,但 .yaml 更符合 YAML 规范文档定义。
- 布尔值小写:YAML 中 true/false 必须小写(如 FORCE_DOWNLOAD: true),大写(True)可能被某些解析器识别为字符串。
- 占位符 {} 无需转义:在 | 块中,{} 是普通字符,可安全保留,运行时由程序动态替换。
总结:YAML 不是 Python,没有“三重引号字符串”概念。面对长 Prompt,优先选用 |(保留换行)或 >(折叠换行),配合合理缩进与空行分隔,即可写出健壮、可维护、零语法错误的配置文件。

















