graphrag icon indicating copy to clipboard operation
graphrag copied to clipboard

fix(config): preserve literal $ in config values

Open jluocsa opened this issue 6 months ago • 0 comments

Description

Fixes #2349.

_parse_env_variables in graphrag-common calls string.Template(text).substitute(os.environ). substitute() treats every $ character as a placeholder prefix and raises ValueError: Invalid placeholder in string for any $ that is not followed by a valid Python identifier or {ident} / $$ escape. Common config values fall into that trap, most notably regex anchors like file_pattern: ".*\\.md$".

Reproduction

# settings.yaml
input:
  type: markitdown
  file_pattern: ".*\\.md$"
$ graphrag index --root <dir>
ValueError: Invalid placeholder in string: line 50, col 25

Fix

Swap substitute() for safe_substitute(). safe_substitute() leaves unrecognized $ sequences as literal text, which is the expected behaviour for arbitrary string config values.

To keep the existing "missing env var raises ConfigParsingError" contract (and the test that exercises it), the function now also inspects Template.get_identifiers() (Python 3.11+, which is graphrag's minimum) and raises ConfigParsingError up-front if any referenced identifier has no corresponding entry in os.environ.

Net result:

Input Before After
file_pattern: ".*\\.md$" ValueError crash preserved literally
val: $$literal $literal $literal (unchanged)
val: $MY_VAR (set) substituted substituted
val: ${MISSING} ConfigParsingError ConfigParsingError (unchanged)

Tests

  • New unit test test_load_config_preserves_literal_dollar_sign covering bare $ in a regex-like value, $$ escape, and a mixed payload that combines a regex $ with a real env-var reference.
  • Existing test_load_config_validation (which expects ConfigParsingError for a missing env var) still passes — the get_identifiers() pre-check keeps that path intact.

Local run:

.venv\Scripts\python.exe -m pytest tests/unit/load_config -q
... 3 passed in 1.98s

ruff check and ruff format --check are clean on the changed files.

Notes

  • Changelog: added .semversioner/next-release/patch-…json per CONTRIBUTING.md.
  • Diff: +1 source file, +1 test, +1 semversioner entry. No behavioural change for any code path that was previously succeeding.

jluocsa avatar May 23 '26 19:05 jluocsa