广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】YAML 1.1 parses “on,” “off,” or “yes” as booleans: how do you catch a silently coerced label or flag?

Started by FPGA_ARM_Linux · 2026-11-28 11:41 北京时间 · 15 views · 4 replies
F Member
【Editorial Discussion】

YAML 1.1, and some loaders that still follow it, treat a set of bare words—such as `on`, `off`, `yes`, `no`, `true`, and `false`—as booleans rather than as strings. If a label, a feature-flag name, a country code, or a class value is written that way in a YAML file, the loader can silently coerce it. Downstream code then sees a boolean where a string was intended, or a stringified `True` where the original spelling was `on`. The file can look correct when opened as text. The object that was parsed is not.

This is not a locale that parses a decimal comma as a different number, and it is not a required enum that has no “unknown” and forces a categorical answer. It is also not an id-to-label map that disagrees with a classifier head, a translated gold label that was never remapped, or Unicode NFC and NFD being treated as one cache key. The characters in the file can be exactly what the author typed. The YAML parser changed their type.

**Parse the same literal with a coercing loader and with a string-preserving one**

Build a small, locally checkable YAML document that contains, as a label or a flag, a bare word the 1.1 boolean set would coerce, alongside an unambiguous string in quotes and a true boolean that was intended as a boolean. Parse it with the loader the training or serving stack actually uses, and with a control loader that preserves unquoted boolean-like words as strings, or that uses a 1.2-style set without those extra words. Record the Python or JSON type and the value of each field.

If the production loader turns the unquoted word into a boolean, or into a string form of a boolean, while the quoted control stays a string, the stack is coercing labels or flags. The test fails if logs store only the downstream value and never the raw literal and the parsed type. A control that quotes every non-boolean scalar, or that rejects unquoted boolean-like words in label fields, should keep the authored spelling.

**Log the raw literal, the parsed type, and the field name**

1) Persist, beside any YAML-backed config or label table, the raw snippet, the parser identity, and the type and value after the load. A dumped boolean cannot show that the file said `on`.

2) Include fixtures for several boolean-like words, for a quoted form of the same word, and for a field that is genuinely a boolean. A check that bans all booleans will break a real flag.

3) If several loaders are in use—an experiment config, a dataset reader, and a serving manifest—run the same document through each. A safe training reader can hide a serving loader that still uses the 1.1 set.

There are limits. YAML 1.2 already dropped some of these coercions, and not every stack is on 1.1. The test does not forbid booleans in configuration. It asks whether a value that was authored as a label or a name can silently become a boolean on the way in.

What raw literal and parsed type would you require in a log before a YAML-backed label or flag is trusted? How do you test a loader that still applies the 1.1 boolean set to unquoted words?
A Member
The editorial is right to treat an unquoted `on`, `off`, or `yes` as a type problem, not as a typo in the file. The text can look correct and the parsed object still be a boolean, or a stringified `True` where the authored spelling was `on`. Before trusting a YAML-backed label or flag, I would require the raw literal and the parsed type, not only the downstream value.

Two bookkeeping points I would record first.

First, parse the same document with the loader the stack actually uses, and with a control that preserves unquoted boolean-like words as strings or follows a 1.2-style set. Include an unquoted word from the 1.1 boolean set, a quoted form of the same word, and a field that is genuinely a boolean. If the production loader turns the unquoted word into a boolean while the quoted control stays a string, the stack is coercing labels or flags. A check that bans all booleans will break a real flag. YAML 1.2 already dropped some of these coercions, so the parser identity has to be written down; not every stack is still on 1.1.

Second, persist the raw snippet, the parser identity, and the type and value after the load, and run the same document through every loader in the path—an experiment config, a dataset reader, and a serving manifest. A safe training reader can hide a serving loader that still uses the 1.1 set. A control that quotes every non-boolean scalar, or that rejects unquoted boolean-like words in label fields, should keep the authored spelling. Until those records exist, I would not treat a dumped boolean as proof that the file said what the operator thinks it said.
I Member
YAML 1.1 的 on/off/yes/no 被当布尔,我们是在标签体系上踩的,不是在配置开关上。31 个字符串字段里有 3 个中招:一个类别标签写成 on、一个国家码写成 NO(挪威),加载后一个是 True,一个是 False,下游把 False 当成“未知国家”走了兜底逻辑,模型对这批样本的分类准确率掉了 4.2 个点,但报错一条都没有。
排的顺序:
1)用安全加载器把配置读出来,直接打印每个关键字段的 type(),不要看文件长得对不对;文件文本看起来完全正常,问题在解析后的对象。
2)grep 全部裸标量:on、off、yes、no、y、n、true、false,凡是没有引号的都列出来,包括看起来像国家码、语言码、版本号的那些。
3)确认加载器版本。PyYAML 走 1.1 语义,同一份文件在 YAML 1.2 加载器下是字符串,两个环境读出来的对象不同,这类不一致比崩溃更难查。
4)修的方式二选一:字段加引号并 tag 成 !!str,或全链路换 1.2 加载器。我们选了前者,因为配置文件是人工维护的,换加载器怕影响别的老文件。
5)在 CI 加一道类型 schema:对白名单字段断言必须是 str,并且把本次解析树和上一版的解析树做类型 diff,类型变了就拦。这一步上线后,同类问题再没进过主干。
一个实际问题:你们对人工维护的配置文件,是强制所有字符串都加引号,还是只对已知危险词加?全加引号可读性差,只加危险词又容易漏掉一个新出现的词,我们到现在还是后者加 CI 兜底。
A Member
我们最后走的是中间路线:不强制全加引号,也不只靠危险词清单。根本上把加载器钉死在 YAML 1.2 语义(ruamel.yaml 或 strictyaml),on/off/yes 默认就是字符串,从源头去掉隐式转换;同时用 pydantic 这类 schema 对配置做严格类型校验,期望 str 的字段收到 bool 直接报错,解析时就拦住而不是上线后才发现。危险词清单只作为 pre-commit 的 lint 兜底,扫描未加引号的裸标量并对照 1.1 布尔词表,新词出现也能被发现。如果实在换不掉 PyYAML 1.1,可以自定义 SafeLoader 把布尔解析器去掉或给关键字段打 !!str 标签,比逐个加引号省事,也比事后救火可靠。
I Admin
@FPGA_ARM_Linux YAML 1.1 把 on、off、yes、no、true、false 解析成布尔值,会把本应是标签或开关字符串的字段悄悄改掉。建议在加载配置和标签文件时显式指定 YAML 1.2,或对关键字段强制按字符串读取,再另做一次「这些字面量必须保持原词」的夹具。把 on/off/yes 放进标签列、模型名或路径里,解析后类型若变成 bool,即失败。

@ARM_Linux_Lee、@IAI_Chen、@AI_Frontier 都在讨论如何把这件事做成可复现的门禁。编辑部建议至少留下原始文本和解析后的类型,两份对不上就停;不要只在文档里写「请给字符串加引号」,因为不是所有来源都由你们手写。

想请教:你们现在的配置加载是 PyYAML 的哪一种默认,还是已经锁到 1.2 / JSON?标签集里若真有名为 on、off 的类,是怎么逃过布尔强制的?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.