Skip to content

[Fix] qwen3_coder: Preserve structured values in qwen3_coder mixed unions - #44124

Merged
JustinTong0323 merged 8 commits into
sgl-project:mainfrom
rystewart-nvidia:fix/qwen3-coder-structured-unions
Oct 27, 2026
Merged

JustinTong0323 merged 8 commits into
sgl-project:mainfrom
rystewart-nvidia:fix/qwen3-coder-structured-unions

Conversation

@rystewart-nvidia

@rystewart-nvidia rystewart-nvidia commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Closes #44120

When a tool parameter allows a string or an object, qwen3_coder treats the model's object JSON as a string. After one decode of function.arguments, callers get {"location": "{\"city\": \"Paris\"}"} instead of {"location": {"city": "Paris"}}. String/array unions have the same problem, including streamed responses.

This can affect any model using --tool-call-parser qwen3_coder. The string result is schema-valid, but it loses the structured value emitted by the model.

Related work: #30832 added nullable-array support, and #36626 fixed top-level schema property lookup. This addresses a separate case: a known parameter whose own union allows both strings and structured values.

Modifications

In the detector's existing string conversion branch, check anyOf/oneOf parameters for complete object or array JSON. Keep the decoded value only when it satisfies the parameter schema.

Plain strings, scalar unions, malformed JSON, values that do not match the schema, and unresolved references keep their existing behavior. Schema validation does not fetch external references.

This intentionally prefers an allowed object or array when mixed-union text could also be read as a string. String-only parameters still keep JSON-looking text as strings.

The change is limited to the Qwen3-Coder detector and its existing registered tests. The shared inference helper, other detector implementations, public APIs, and dependency lists are unchanged. step3p5 and nanbeige also use this detector and receive the same behavior. Type arrays and root-level reference resolution are outside this fix.

Accuracy Tests

Does not change kernel or affect model forward code, but does affect structured output correctness.

  • Verified the regression on native v0.5.21 before the fix: 24 structured-value subtests failed at the expected assertions.
  • With the fix on v0.5.21, all 20 Qwen3-Coder tests and 60 subtests pass. Coverage includes complete parsing, one- and seven-character streaming chunks, both union types and branch orders, objects/arrays, schema constraints, and string compatibility.
  • Replayed six outputs captured from the live v0.5.21 Qwen3-Coder server through complete and streaming parsing. All three previously lost objects stay objects; the genuine date string and both object-only controls keep their values and types.
  • A live patched v0.5.21 server using Qwen3-Coder-30B-A3B-Instruct (BF16, TP1, one B200) passed health, model listing, deterministic chat, and all 12 tool requests, including three streams. Object/array unions stay structured after one arguments decode; object-only, string-only JSON text, and numeric/string controls pass.
  • Changed executable production lines have 100% coverage (11/11).

Validation used lmsysorg/sglang:v0.5.21 (digest sha256:b1259f3ea3275f66237c498ea388919729018bc9f01c3d638391e06e2cf3f469). Baseline raw-output logging left conversion unchanged; the patched serving run installed only the exact candidate detector. The regression tests ran against the native runtime without adding compatibility modules.

The full original detector and relevant type-inference function are byte-identical to main (7dc2930585782c09630d617a510bec08c1724845), so I didn't run a separate test on main.

These checks were only for parser correctness; no general model accuracy benchmark was run.

Speed Tests and Profiling

No speed benchmark was run. This adds JSON parsing for string-inferred anyOf/oneOf parameters, followed by schema validation when the decoded value is an object or array. Model execution and decoding kernels are unchanged.

Checklist

Review and Merge Process

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ✅ Run #39374134476
Latest PR Test (Extra): ❌ Run #39374134442
Latest PR Test (AMD ROCm 10): ❌ Run #39374134512

@nvpohanh nvpohanh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[by Claude Code] Reviewed at ed4ff0111609cec3946c68e691c4c266cd4d7f72. 3 inline comments; no PR-level findings.

schema = param_config[param_name]
if isinstance(schema, dict) and ("anyOf" in schema or "oneOf" in schema):
try:
value = json.loads(param_value)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plain-text values of union and nullable string parameters now pay a failed json.loads, about 3 to 5 us each. How about decoding only when the stripped value starts with { or [?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah makes sense. added the prefix check before decoding in 7e233cd

Comment on lines +127 to +130
if isinstance(value, (dict, list)) and Draft202012Validator(
schema, registry=Registry()
).is_valid(value):
return value

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A decoded NaN or Infinity now reaches the arguments as non-standard JSON that strict parsers reject, where the string stayed valid. Would it make sense to reject those constants while decoding?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

877837b - Updated to check for finite numbers to catch nan, inf, and overflows, and keep the original string if parsing fails

@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/unit/function_call/test_function_call_parser.py test/registered/unit/entrypoints/openai/test_serving_responses_stream.py

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Rebase Required Before Re-run

A major update has landed on main. Your PR is diverged relative to required base commit 6a39ddf1f2e0.

Re-run was not dispatched. What to do:

  • Rebase your branch onto the latest main and push again
  • Follow issue #21065 for context
  • CI-fix PRs may request the bypass-maintenance label to skip this check

@rystewart-nvidia
rystewart-nvidia force-pushed the fix/qwen3-coder-structured-unions branch from 9a5321e to c67585a Compare October 19, 2026 21:23
@github-actions

This comment has been minimized.

@nvpohanh

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Oct 20, 2026
@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Claude Code] I've started babysitting the NVIDIA CI of this PR. I'll check it every 30 minutes until all NV pipelines pass. If a relevant job fails, I may rerun it, merge the latest main into this branch, push a small test-only fix, fix the PR title if the title check fails, or post a note here.

Anyone can stop me at any time: just post a comment asking me to stop (for example, stop babysitting). I'll stop at my next check, within 30 minutes, and take no further action on this PR.

@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Claude Code] @rystewart-nvidia, the PR Title check failed, so I renamed this PR from [Fix] Preserve structured values in qwen3_coder mixed unions to [Fix] qwen3_coder: Preserve structured values in qwen3_coder mixed unions to match the required [Type] scope: title format. Feel free to change the type or scope if I picked the wrong one.

@nvpohanh nvpohanh changed the title [Fix] Preserve structured values in qwen3_coder mixed unions [Fix] qwen3_coder: Preserve structured values in qwen3_coder mixed unions Oct 20, 2026
@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

Copy link
Copy Markdown
Collaborator

/rerun-failed-ci

@nvpohanh

Copy link
Copy Markdown
Collaborator

[by Claude Code] All NV pipelines have passed.

@nvpohanh

Copy link
Copy Markdown
Collaborator

@JustinTong0323 Could you review this? Thanks!

@nvpohanh

Copy link
Copy Markdown
Collaborator

@JustinTong0323 could you review this? Thanks!

@JustinTong0323 JustinTong0323 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JustinTong0323
JustinTong0323 merged commit 9d45fdd into sgl-project:main Oct 27, 2026
430 of 475 checks passed
detain pushed a commit to detain/sglang that referenced this pull request Oct 29, 2026
…ions (sgl-project#44124)

Port of merged upstream 9d45fdd.

Union resolutions against local stack surface (all in
qwen3_coder_detector.py):
1. _convert_param_value string branch: upstream hunk conflicted with
   our STRING_PARAM_TYPES module constant (stack entries 157/160 era
   refactor, value-identical to the upstream inline list). Kept the
   constant reference, took the PR union-decode block verbatim as the
   branch body.
2. Entry 159 (_strip_complete_tool_calls / _iter_tool_call_bodies
   linear scan) and entry 160 (_schema_allows_null + gated null
   handling): untouched by the PR's hunks; retained in full.
3. Extra call site the upstream PR cannot know about: our streaming
   string fast path (entry 109 + entry 160 null_as_json gate) calls
   _get_arguments_config before the tuple was returned; adapted to
   'param_config, _ = ...'. Semantics unchanged (only the per-param
   schema is needed at that site).

Union parity note: entry 109's streaming fast path opens only for
exact type:"string" schemas, so string|object unions keep taking the
buffered path through _convert_param_value; the one-shot and streaming
union decode stay in parity.

(cherry picked from commit 9d45fdd)
(cherry picked from commit 01abd11cb2a645dd7d74abfe19011106d56f0fd5)
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026
…5631/sgl-project#45855/sgl-project#46167 (entries 271-274)

ONE docs commit: the logits trilogy is a CONTRACT UNIT -- recorded as
'never take 45631/45855 without 46167' per entry 274. Also on record:
sgl-project#44124's STRING_PARAM_TYPES union + entry-159/160 retention + the
streaming-fast-path call-site adaptation (141F before amend); sgl-project#45631's
muse_spark hunk DROP with the sgl-project#44666-then re-check note; sgl-project#46167's
wrapper-only softcap keep and the SILENT-AUTO-MERGE hazard fix
(CUDA_ONLY -> local _CUDA, value-identical, kernels_namespace 76P proves).

Battery EXACT: function_call 652P/1964sub on top of batch-1+2, numeric
4P/156sub, kernels_namespace 76P, spec dir 335P/11S/180sub (batch-1 pin
survives all 14 picks). FLAGGED: sampling 223P/2S/43sub measured vs 263P
predicted -- predicted number irreproducible on the worker's OWN tip with
diff-0 across waves (prediction-vs-method gap, not landed code); logits
56P/7S unverifiable (pre-existing module-level get_device() CPU collect
failure + all 4 GPUs busy with the live prod server) -- folded into the
trilogy's GPU-debt block.
detain added a commit to detain/sglang that referenced this pull request Oct 29, 2026
…k-274, wanted_stack LANDED marks, CAVEATS docs-only ledger items

(A) Header: STACK line 260->274; three wave lines (+spec-mamba-2026-10-28
(pr45491+pr45435+pr45472+pr45670), +admission-2026-10-28(pr46272*+
pr43484*+pr46496*+pr46643*+pr45607*+pr45789*), +parsers-2026-10-28
(pr44124+pr45631+pr45855+pr46167)); tip line -> 2714457; updated
261-274 headline block (entry-119 SUPERSEDED by upstream sgl-project#45435/sgl-project#45472;
classic-pool mamba admission gate w/ upstream ImportError CAUGHT;
UNCONDITIONAL latch reset = TTFT watch; coder-parser union; logits
trilogy as CONTRACT UNIT; 14 picks, 0 integrator conflicts).

(B) wanted_stack.txt: FAM-A5 + FAM-B234 blocks deleted -> LANDED records
(chain shas, interlock audit, supersession + behavior-change pointers
into entries 261-270); SECTION-B sgl-project#44124/trilogy blocks -> LANDED;
sgl-project#37958/sgl-project#45559 block -> DONE (verdict now in CAVEATS).

(C) DOCS-ONLY LEDGER ITEMS: CAVEATS gains the sgl-project#37958/sgl-project#45559 VERIFIED-
INERT bullet (two independent hard gates, coarse-barrier fallback,
do-not-re-litigate-at-merge-sgl-project#5 line, revisit condition = dropping NEXTN
or HiCache) and the dated C0b/C0c corrections (sgl-project#44030 obsolete rationale,
sgl-project#44858 REVERSED by sgl-project#45289 [ported-pending wt-moe], sgl-project#44115 permanent N/A,
10-28 open-status re-verification list, no-pin-moves note: 0.7.2/0.4.9
floor unchanged) with the C0a tick (sgl-project#37625/sgl-project#37941/sgl-project#39727 still
outstanding; only sgl-project#43927/sgl-project#44849 partially satisfied). C0b/C0c/C8 marked
DONE in wanted_stack; C0a stays open.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] qwen3_coder returns object and array arguments as strings in mixed unions

4 participants