fix: parse XML-like function.arguments from OpenAI-compatible models#12038
Open
octo-patch wants to merge 1 commit intocontinuedev:mainfrom
Open
fix: parse XML-like function.arguments from OpenAI-compatible models#12038octo-patch wants to merge 1 commit intocontinuedev:mainfrom
octo-patch wants to merge 1 commit intocontinuedev:mainfrom
Conversation
…ixes continuedev#11453) Some OpenAI-compatible models (e.g. local Qwen endpoints) return tool_calls[].function.arguments in an XML-like format rather than JSON: <parameter=name>value</parameter> Continue assumed JSON-only arguments, causing tool-call execution to silently fail with empty args when JSON.parse() returned {}. Changes: - core/tools/parseArgs.ts: add parseXmlToolCallArgs() and fall back to it in safeParseToolCallArgs() when JSON parsing fails. Scalar coercion applied: true/false to boolean, numeric strings to number, JSON-parseable strings to parsed value, otherwise string. - core/tools/parseArgs.vitest.ts: add regression tests for the new XML-fallback path. - extensions/cli/src/stream/streamChatResponse.ts: apply the same XML fallback after streaming ends for any tool call whose accumulated argumentsStr was never JSON-parseable.
Contributor
There was a problem hiding this comment.
2 issues found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="core/tools/parseArgs.ts">
<violation number="1" location="core/tools/parseArgs.ts:12">
P2: XML numeric coercion accepts non-finite numbers (e.g., `Infinity`), and downstream `getNumberArg` does not reject them.</violation>
<violation number="2" location="core/tools/parseArgs.ts:31">
P1: Model-controlled XML parameter names can pollute object prototype via `__proto__`, enabling inherited-arg presence bypasses.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
| * <parameter=…> tags are found so callers can detect a failed parse. | ||
| */ | ||
| export function parseXmlToolCallArgs(argsStr: string): Record<string, any> { | ||
| const result: Record<string, any> = {}; |
Contributor
There was a problem hiding this comment.
P1: Model-controlled XML parameter names can pollute object prototype via __proto__, enabling inherited-arg presence bypasses.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At core/tools/parseArgs.ts, line 31:
<comment>Model-controlled XML parameter names can pollute object prototype via `__proto__`, enabling inherited-arg presence bypasses.</comment>
<file context>
@@ -1,5 +1,46 @@
+ * <parameter=…> tags are found so callers can detect a failed parse.
+ */
+export function parseXmlToolCallArgs(argsStr: string): Record<string, any> {
+ const result: Record<string, any> = {};
+ const paramRegex = /<parameter=([^>]+)>([\s\S]*?)<\/parameter>/g;
+ let match: RegExpExecArray | null;
</file context>
Suggested change
| const result: Record<string, any> = {}; | |
| const result: Record<string, any> = Object.create(null); |
| if (value.toLowerCase() === "false") return false; | ||
|
|
||
| // Numeric coercion — only if the string is entirely numeric | ||
| if (value.trim() !== "" && !isNaN(Number(value))) return Number(value); |
Contributor
There was a problem hiding this comment.
P2: XML numeric coercion accepts non-finite numbers (e.g., Infinity), and downstream getNumberArg does not reject them.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At core/tools/parseArgs.ts, line 12:
<comment>XML numeric coercion accepts non-finite numbers (e.g., `Infinity`), and downstream `getNumberArg` does not reject them.</comment>
<file context>
@@ -1,5 +1,46 @@
+ if (value.toLowerCase() === "false") return false;
+
+ // Numeric coercion — only if the string is entirely numeric
+ if (value.trim() !== "" && !isNaN(Number(value))) return Number(value);
+
+ // JSON coercion (for embedded arrays/objects/quoted strings)
</file context>
Suggested change
| if (value.trim() !== "" && !isNaN(Number(value))) return Number(value); | |
| const num = Number(value); | |
| if (value.trim() !== "" && Number.isFinite(num)) return num; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #11453
Problem
Some OpenAI-compatible models (e.g. local Qwen endpoints) return
tool_calls[].function.argumentsin an XML-like format rather than JSON:Continue assumed JSON-only arguments in all tool-call parsing paths. When
JSON.parse()failed, it silently returned{}, causing all required-argument validation to fail and tool calls to never execute correctly.Solution
Add
parseXmlToolCallArgs()tocore/tools/parseArgs.tsas a fallback when JSON parsing fails. The function:<parameter=name>value</parameter>pairs via regex"true"/"false"→ boolean, numeric strings → number, JSON-parseable strings → parsed value, everything else stays as a stringsafeParseToolCallArgs()now tries XML parsing before returning{}.The CLI streaming path (
extensions/cli/src/stream/streamChatResponse.ts) also gets the same fallback: after all streaming chunks arrive, any tool call whoseargumentsStrwas never JSON-parseable is retried throughparseXmlToolCallArgs().Testing
core/tools/parseArgs.vitest.tscovering:True/False)Summary by cubic
Fixes tool-call parsing when some OpenAI-compatible models return XML-like function.arguments, so tools receive correct parameters. Adds a fallback parser with simple type coercion and uses it in core parsing and CLI streaming.
safeParseToolCallArgs(): parses<parameter=name>value</parameter>and coerces booleans, numbers, and JSON literals.Written for commit f175f3b. Summary will update on new commits.