[PR #5493] [Other] serve/frontend: deduplicate rewritten non-string XML params, coerce Python bools, and skip empty/null placeholders #5467

Open
opened 2026-11-07 21:43:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/5493
Author: @OleksiyPenkov
Created: 11/7/2026
Status: 🔄 Open

Base: main ← Head: fix/xml-tool-call-param-coercion-dedup


📝 Commits (1)

  • ffd6c47 serve/frontend: deduplicate rewritten non-string XML params, coerce Python bools, and skip empty/null placeholders

📊 Changes

3 files changed (+135 additions, -22 deletions)

View changed files

📝 serve/frontend.py (+45 -18)
📝 serve/test_server.py (+76 -0)
📝 serve/test_tool_corpus.py (+14 -4)

📄 Description

Problem and result

While running a 20-task agentic tool-use campaign (2,704 tool calls on Qwen3.8-Flash-Next via /v1/chat/completions and /v1/responses), we encountered XML tool-call parameter slips in serve/frontend.py that either broke stream_tools=True parity or failed downstream client JSON-schema validation:

  1. Rewritten non-string <parameter=P> blocks in the same <tool_call>:

    • Before: When the model starts writing an array/object parameter <parameter=bounds>[...]</parameter> and immediately rewrites <parameter=bounds>[...]</parameter> inside the same <function=...> call, param_end(..., name=pname) (#4078's reopened-string heuristic) treated the first </parameter> as part of the value even for non-string parameters, merging both arrays into [...]\n</parameter>\n<parameter=bounds>\n[...], which fails JSON decoding and leaves the raw string in arguments[pname]. Furthermore, in OutputParser._scan (stream_tools=True), emitting "pname":... for every occurrence produced duplicate keys or a mismatch with call.arguments.
    • After: param_end's same-name reopen rule (allow_reopen) applies only to string/undeclared parameters (and whole-call boundary walks call_end / function_end), and OutputParser._scan (ss == "raw") skips emitting a non-string parameter when the same parameter name appears again later in the call body.
  2. Python boolean literals (True / False):

    • Before: When a model emits <parameter=dry_run>True</parameter> or False, json.loads raises ValueError and lenient_json only handles arrays/objects, returning the string "True" instead of True.
    • After: coerce_value decodes "true" / "false" (case-insensitive) to boolean True / False when _declares(declared, "boolean").
  3. Empty or null / None placeholders on non-string parameters:

    • Before: When a model emits <parameter=timeout></parameter>, <parameter=bounds>null</parameter>, or <parameter=bounds>None</parameter> for optional non-string parameters, emitting "timeout": "" or "bounds": null caused strict client schema validators (where optional fields have "type": "array" without "null") to reject the tool call.
    • After: coerce_value returns sentinel _SKIP when a non-string parameter is empty or null/None, and both parse_tool_call and OutputParser._scan omit the key.

none

Approach

  • Added allow_reopen: bool = False to param_end so that #4078's reopened-parameter rule remains active for string/undeclared parameters and whole-call boundary walks (call_end, function_end), while non-string parameters end at their first </parameter>.
  • In coerce_value, added _SKIP sentinel for empty/null/None non-string parameters and case-insensitive boolean coercion when _declares(declared, "boolean").
  • In OutputParser._scan, delayed emitting "pname": for non-string parameters until ss == "raw" completes, skipping _SKIP values and earlier occurrences of a parameter name that is repeated later in the same <function=...> block.
  • Preserves existing behavior for all valid tool calls, string parameters that reopen their own tag (#4078), and damaged JSON arrays/objects (#4427).

Validation

Passed:

  • Command: python -m unittest serve.test_tool_corpus serve.test_parser_call_scan serve.test_quoted_call_values serve.test_param_end_options serve.test_frontend serve.test_server.ToolCallParameterCoercionAndDedup serve.test_server.ToolCallRecovery serve.test_server.UnfinishedToolCall
    • Revision / environment: commit ffd6c47 on main (51763da), Windows 11 x64 (10.0.26300), Python 3.12, Ryzen 9 9950X3D, 64 GB RAM, NVIDIA RTX 5070 Ti 16 GB + RTX 5060 Ti 16 GB.
    • Observed result: Ran 59 tests in 4.120s — OK (verifying whole, chunked, and single-byte-split parity for stream_tools=False and stream_tools=True).
  • Command: python -m unittest serve.test_tool_opener_misspelt serve.test_tool_routes serve.test_tool_name_alias serve.test_tool_name_mcp_member serve.test_tool_name_prefix serve.test_tool_name_space serve.test_tool_call_envelope serve.test_tool_call_unclosed serve.test_native_tool_parser serve.test_reasoning_rescue serve.test_reasoning_tools serve.test_parser_hold serve.test_parser_markdown_code serve.test_parser_track serve.test_dsv41
    • Revision / environment: commit ffd6c47 on main (51763da), Windows 11 x64, Python 3.12.
    • Observed result: Ran 199 tests in 35.802s — OK (skipped=1).

Failed:

  • none observed

Not run:

  • C++ engine builds (CUDA, HIP, SYCL, Metal): this change touches only Python text parsing in serve/frontend.py and unit tests in serve/test_server.py and serve/test_tool_corpus.py.

Limits

  • In streaming mode (stream_tools=True), deduplication of a rewritten parameter applies to non-string parameters (which are buffered until </parameter> in ss == "raw"); string parameters stream character-by-character and continue to use the #4078 reopened-tag rule.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/5493 **Author:** [@OleksiyPenkov](https://github.com/OleksiyPenkov) **Created:** 11/7/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/xml-tool-call-param-coercion-dedup` --- ### 📝 Commits (1) - [`ffd6c47`](https://github.com/Niko1221/Strata/commit/ffd6c47e0a21761fe7682375da194d83a11f9041) serve/frontend: deduplicate rewritten non-string XML params, coerce Python bools, and skip empty/null placeholders ### 📊 Changes **3 files changed** (+135 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `serve/frontend.py` (+45 -18) 📝 `serve/test_server.py` (+76 -0) 📝 `serve/test_tool_corpus.py` (+14 -4) </details> ### 📄 Description ## Problem and result While running a 20-task agentic tool-use campaign (2,704 tool calls on `Qwen3.8-Flash-Next` via `/v1/chat/completions` and `/v1/responses`), we encountered XML tool-call parameter slips in `serve/frontend.py` that either broke `stream_tools=True` parity or failed downstream client JSON-schema validation: 1. **Rewritten non-string `<parameter=P>` blocks in the same `<tool_call>`:** - **Before:** When the model starts writing an array/object parameter `<parameter=bounds>[...]</parameter>` and immediately rewrites `<parameter=bounds>[...]</parameter>` inside the same `<function=...>` call, `param_end(..., name=pname)` (#4078's reopened-string heuristic) treated the first `</parameter>` as part of the value even for non-string parameters, merging both arrays into `[...]\n</parameter>\n<parameter=bounds>\n[...]`, which fails JSON decoding and leaves the raw string in `arguments[pname]`. Furthermore, in `OutputParser._scan` (`stream_tools=True`), emitting `"pname":...` for every occurrence produced duplicate keys or a mismatch with `call.arguments`. - **After:** `param_end`'s same-name reopen rule (`allow_reopen`) applies only to string/undeclared parameters (and whole-call boundary walks `call_end` / `function_end`), and `OutputParser._scan` (`ss == "raw"`) skips emitting a non-string parameter when the same parameter name appears again later in the call body. 2. **Python boolean literals (`True` / `False`):** - **Before:** When a model emits `<parameter=dry_run>True</parameter>` or `False`, `json.loads` raises `ValueError` and `lenient_json` only handles arrays/objects, returning the string `"True"` instead of `True`. - **After:** `coerce_value` decodes `"true"` / `"false"` (case-insensitive) to boolean `True` / `False` when `_declares(declared, "boolean")`. 3. **Empty or `null` / `None` placeholders on non-string parameters:** - **Before:** When a model emits `<parameter=timeout></parameter>`, `<parameter=bounds>null</parameter>`, or `<parameter=bounds>None</parameter>` for optional non-string parameters, emitting `"timeout": ""` or `"bounds": null` caused strict client schema validators (where optional fields have `"type": "array"` without `"null"`) to reject the tool call. - **After:** `coerce_value` returns sentinel `_SKIP` when a non-string parameter is empty or `null`/`None`, and both `parse_tool_call` and `OutputParser._scan` omit the key. ## Related discussion none ## Approach - Added `allow_reopen: bool = False` to `param_end` so that #4078's reopened-parameter rule remains active for string/undeclared parameters and whole-call boundary walks (`call_end`, `function_end`), while non-string parameters end at their first `</parameter>`. - In `coerce_value`, added `_SKIP` sentinel for empty/`null`/`None` non-string parameters and case-insensitive boolean coercion when `_declares(declared, "boolean")`. - In `OutputParser._scan`, delayed emitting `"pname":` for non-string parameters until `ss == "raw"` completes, skipping `_SKIP` values and earlier occurrences of a parameter name that is repeated later in the same `<function=...>` block. - Preserves existing behavior for all valid tool calls, string parameters that reopen their own tag (#4078), and damaged JSON arrays/objects (#4427). ## Validation **Passed:** - Command: `python -m unittest serve.test_tool_corpus serve.test_parser_call_scan serve.test_quoted_call_values serve.test_param_end_options serve.test_frontend serve.test_server.ToolCallParameterCoercionAndDedup serve.test_server.ToolCallRecovery serve.test_server.UnfinishedToolCall` - Revision / environment: commit `ffd6c47` on `main` (`51763da`), Windows 11 x64 (`10.0.26300`), Python 3.12, Ryzen 9 9950X3D, 64 GB RAM, NVIDIA RTX 5070 Ti 16 GB + RTX 5060 Ti 16 GB. - Observed result: `Ran 59 tests in 4.120s — OK` (verifying whole, chunked, and single-byte-split parity for `stream_tools=False` and `stream_tools=True`). - Command: `python -m unittest serve.test_tool_opener_misspelt serve.test_tool_routes serve.test_tool_name_alias serve.test_tool_name_mcp_member serve.test_tool_name_prefix serve.test_tool_name_space serve.test_tool_call_envelope serve.test_tool_call_unclosed serve.test_native_tool_parser serve.test_reasoning_rescue serve.test_reasoning_tools serve.test_parser_hold serve.test_parser_markdown_code serve.test_parser_track serve.test_dsv41` - Revision / environment: commit `ffd6c47` on `main` (`51763da`), Windows 11 x64, Python 3.12. - Observed result: `Ran 199 tests in 35.802s — OK (skipped=1)`. **Failed:** - none observed **Not run:** - C++ engine builds (CUDA, HIP, SYCL, Metal): this change touches only Python text parsing in `serve/frontend.py` and unit tests in `serve/test_server.py` and `serve/test_tool_corpus.py`. ## Limits - In streaming mode (`stream_tools=True`), deduplication of a rewritten parameter applies to non-string parameters (which are buffered until `</parameter>` in `ss == "raw"`); string parameters stream character-by-character and continue to use the #4078 reopened-tag rule. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5467
No description provided.