Skip to content

Handle brackets inside string arguments in split_args#2164

Closed
winklemad wants to merge 1 commit into
NVIDIA-NeMo:developfrom
winklemad:fix/split-args-brackets-in-strings
Closed

Handle brackets inside string arguments in split_args#2164
winklemad wants to merge 1 commit into
NVIDIA-NeMo:developfrom
winklemad:fix/split-args-brackets-in-strings

Conversation

@winklemad

Copy link
Copy Markdown

Description

split_args (the Colang 1.0 argument parser in nemoguardrails/colang/v1_0/lang/utils.py) treats every bracket and quote as structural. While inside a quoted string argument, a closing bracket ), ], } — or the other quote character — is still handled as syntax, so it raises ValueError and crashes flow parsing.

This hits ordinary string values: an emoticon, a citation marker, or any clause that ends with a bracket.

>>> from nemoguardrails.colang.v1_0.lang.utils import split_args
>>> split_args('msg="Hello :)"')
ValueError: Invalid syntax for string: msg="Hello :)"; expecting " and got )

It also crashes through the public parse path — any Colang 1.0 flow with such an inline string parameter fails to parse:

>>> from nemoguardrails.colang.v1_0.lang.coyml_parser import parse_flow_elements
>>> parse_flow_elements([{"user": 'ask(reason="closed ]")'}])
ValueError: Invalid syntax for string: reason="closed ]"; expecting " and got ]

The function's own docstring says it "correctly handles strings and lists/dicts", and the sibling splitters in the same module (word_split, char_split, params_tokenize) already track string state — only split_args doesn't.

Fix

Add a real in-string short-circuit: while the top of the stack is a quote, only the matching closing quote is special; brackets, commas, and the other quote type are literal text. This preserves list/dict argument parsing and still raises ValueError for genuinely unbalanced unquoted brackets (the behavior added in #2145).

Test Plan

Added test_split_args_bracket_inside_string to tests/test_parser_utils.py, covering closing brackets )]}, an emoticon, a citation marker, a comma, and the other quote character inside a quoted string, plus a bracket-in-string argument alongside a normal one. The new cases fail on develop (ValueError) and pass with the fix; the existing test_1 and the unbalanced-unquoted-bracket cases still pass.

$ pytest tests/test_parser_utils.py tests/colang
70 passed

$ ruff check nemoguardrails/colang/v1_0/lang/utils.py tests/test_parser_utils.py
All checks passed!
$ ruff format --check nemoguardrails/colang/v1_0/lang/utils.py tests/test_parser_utils.py
2 files already formatted

No linked issue — found by reading the code.

@github-actions github-actions Bot added size: S status: needs triage New issues that have not yet been reviewed or categorized. needs: signing labels Jul 11, 2026
split_args treated every bracket and quote as structural, so a closing
bracket inside a quoted string argument (e.g. reason="closed ]" or an
emoticon "Hello :)") raised ValueError and crashed Colang flow parsing.
Track string state so that while inside a quoted string only the matching
closing quote is special and brackets, commas and the other quote type are
literal, matching the sibling splitters (word_split, char_split,
params_tokenize).

Signed-off-by: winklemad <winklemad@outlook.com>
@winklemad
winklemad force-pushed the fix/split-args-brackets-in-strings branch from cc7e807 to 000d69b Compare July 11, 2026 19:24
@Pouyanpi Pouyanpi added the duplicate This issue or pull request already exists label Jul 14, 2026
@Pouyanpi

Pouyanpi commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

duplicate of #2145 note that #2144 is already closed

@Pouyanpi Pouyanpi closed this Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists size: S status: needs triage New issues that have not yet been reviewed or categorized.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants