Skip to content

fix(toml): quote string values in the generated Hydra overrides - #253

Closed
kevin9327 wants to merge 1 commit into
NVIDIA:mainfrom
kevin9327:fix-toml-string-quoting
Closed

kevin9327 wants to merge 1 commit into
NVIDIA:mainfrom
kevin9327:fix-toml-string-quoting

Conversation

@kevin9327

Copy link
Copy Markdown

Problem

A string value in an SFT TOML can be changed or rejected on its way into the config. _hydra_format only quotes a top-level string when it holds a comma or a space, and Hydra re-types an unquoted value:

TOML value (string field) Override emitted What the config receives
name = "20260913" job.name=20260913 int 20260913
name = "1e-4" job.name=1e-4 float 0.0001
name = "true" / "null" job.name=true / job.name=null True / None
name = "lr=1e-5" job.name=lr=1e-5 OverrideParseException: mismatched input '=' expecting <EOF>
name = "run(v2)" job.name=run(v2) Unknown function 'run'
name = "run[1]", "exp#3" unquoted parse error
name = "bob's run" job.name='bob's run' parse error (quote not escaped)

So a run name, group, project or path containing =, (, [, # or an apostrophe stops --sft-toml loading before training starts. A numeric-looking run name loads but is an int, which Config.validate then passes to bytearray(self.job.name, "utf-8"), and that raises TypeError: encoding without a string argument. String items inside lists were already quoted, but their apostrophes weren't escaped either.

Fix

Quote every string value, in lists or not. The new _hydra_quote escapes ' and doubles only the backslashes Hydra would otherwise consume, meaning those just before a quote or at the end of the string. Numbers, bools, lists, ${oc.env:...} interpolation (still resolved inside quotes) and the "???" skip are unchanged. The now-unused in_list parameter is removed.

Tests

New tests in cosmos_framework/configs/toml_config/sft_config_test.py (TestBuildHydraOverrides) feed the emitted override back through Hydra's own OverridesParser:

  • test_string_value_reaches_hydra_as_the_same_string: parametrized over the values in the table plus a\'b and ends\. The result must be a str equal to the TOML value.
  • test_string_list_items_reach_hydra_as_the_same_strings: ["480", "bob's"].
  • test_non_string_values_keep_their_types: lr=1e-5, betas=[0.9, 0.99], max_iter=200, ema.enabled=false, vae_path="${oc.env:WAN_VAE_PATH}" and load_path="???" parse to the same types and values as before. This test passes both before and after the change, so the change doesn't widen what is emitted for non-strings.

Fail-before (source reverted, tests kept):

E   AssertionError: job.name=20260913
    assert (False)
     +  where False = isinstance(20260913, str)
E   AssertionError: job.name=true
    assert (False)
     +  where False = isinstance(True, str)
E   hydra.errors.HydraException: mismatched input '=' expecting <EOF>
E   hydra.errors.HydraException: HydraException while evaluating 'run(v2)': Unknown function 'run'
E   hydra.errors.HydraException: token recognition error at: '#'
E   hydra.errors.HydraException: mismatched input 's' expecting <EOF>
E   hydra.errors.HydraException: token recognition error at: '']'
FAILED .../sft_config_test.py::TestBuildHydraOverrides::test_string_value_reaches_hydra_as_the_same_string[20260913]
FAILED .../sft_config_test.py::TestBuildHydraOverrides::test_string_value_reaches_hydra_as_the_same_string[lr=1e-5]
FAILED .../sft_config_test.py::TestBuildHydraOverrides::test_string_value_reaches_hydra_as_the_same_string[bob's run]
FAILED .../sft_config_test.py::TestBuildHydraOverrides::test_string_list_items_reach_hydra_as_the_same_strings
11 failed, 23 passed, 2 skipped

(ends\ passes before too: an unquoted trailing backslash already parsed.)

Counts for sft_config_test.py:

passed failed skipped
main, existing tests 21 0 2
new tests, source reverted 23 11 2
new tests, with fix 34 0 2

The shipped examples/toml/sft_config/*.toml still validate and build overrides; those parametrized cases are part of the 34.

Run on CPU: PYTHONUTF8=1 COSMOS_TRAINING=0 pytest cosmos_framework/configs/toml_config/sft_config_test.py. On a non-UTF-8 locale, PYTHONUTF8=1 is needed for the existing example-TOML test to read the files. COSMOS_TRAINING=0 avoids the object-store backend import.

Lint

ruff 0.12.7 (repo pin) on both touched files: ruff check output is identical before and after (one pre-existing I001 in toml_config_helper.py). ruff format --diff is the same size before and after (28 changed lines, all pre-existing). sft_config_test.py is clean both ways.

🤖 Generated with Claude Code

build_hydra_overrides left top-level TOML strings unquoted unless they
held a comma or a space. Hydra re-types an unquoted right-hand side, so a
string field such as job.name = "20260913", "true" or "null" reached the
config as an int, bool or None, and a string containing "=", "(", "[",
"#" or an apostrophe was rejected by the override grammar, aborting the
load before training starts.

Quote every string value, escaping quotes and the backslashes Hydra would
otherwise consume. Numbers, bools, lists, ${...} interpolation and the
"???" skip are unchanged.

Signed-off-by: kevin9327 <5299031+kevin9327@users.noreply.github.com>

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@lfengad lfengad closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants