Skip to content

fix: handle bracketless list arguments from LLM#785

Open
octo-patch wants to merge 3 commits into
emcie-co:developfrom
octo-patch:fix/issue-431-bracketless-list-arguments
Open

fix: handle bracketless list arguments from LLM#785
octo-patch wants to merge 3 commits into
emcie-co:developfrom
octo-patch:fix/issue-431-bracketless-list-arguments

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #431

Problem

LLMs occasionally produce list arguments as comma-separated values without surrounding brackets (e.g. red, green instead of ['red', 'green']). This non-deterministic hallucination caused crashes in:

  • split_arg_list() in core/tools.py — raised ValueError (invalid list format)
  • split_arg_list() in core/services/tools/mcp_service.py — raised ValueError
  • _validate_argument_value() in both tool-calling batch modules — ast.literal_eval() raised SyntaxError / ValueError

Solution

  • split_arg_list() (str/Enum item types): Try literal_eval() first; fall back to comma-splitting with strip when it fails.
  • split_arg_list() (scalar types like int/float): Strip optional [...] brackets if present, then split by comma — making brackets optional.
  • _validate_argument_value() (both batch files): Wrap ast.literal_eval() in a try/except and fall back to comma-splitting for array enum validation.

Testing

Added 13 unit tests in tests/core/stable/test_tool_argument_handling.py covering:

  • Already-a-list passthrough
  • Bracketed and bracketless string lists
  • Bracketed and bracketless enum lists (with and without spaces)
  • Bracketed and bracketless int lists
  • cast_tool_argument with bracketless list[int], list[str], and list[Enum]

All 13 tests pass. Ruff lint and mypy checks pass (pre-existing mypy errors in optional adapter stubs are unrelated to this change).

octo-patch added 3 commits April 19, 2026 09:33
The GLM NLP adapter in glm_service.py used the OpenAI client to access
the bigmodel.cn API, while the Zhipu adapter (zhipu_service.py) already
provides proper integration using the official zhipuai SDK with support
for GLM-4 model variants.

Remove the duplicate glm_service.py and the NLPServices.glm() factory
method from the SDK. Users should use NLPServices.zhipu() instead.

Signed-off-by: octo-patch <octo-patch@github.com>
Signed-off-by: octo-patch <octo-patch@github.com>
LLMs occasionally produce list arguments as comma-separated values
without the surrounding brackets (e.g. "red, green" instead of
"['red', 'green']"). This caused ValueError/SyntaxError in:

- split_arg_list() in core/tools.py
- split_arg_list() in core/services/tools/mcp_service.py
- _validate_argument_value() in the two tool-calling batch modules

The fix: when literal_eval() fails (str/Enum item types), fall back to
comma-splitting; for scalar types, strip optional brackets then split by
comma, making both paths robust to the bracketless LLM output format.

Also adds unit tests covering both the bracketed and bracketless cases
for str, Enum, and int item types.

Signed-off-by: octo-patch <octo-patch@github.com>
@kichanyurd
Copy link
Copy Markdown
Contributor

Hi @octo-patch - thanks for the contribution! Can you please resolve the conflicts and push?

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.

[Bug] Deal with list arguments that are provided by LLM without brackets

2 participants