Skip to content

Use native Qwen tool continuations - #1296

Open
Sayan Shaw (sayanshaw24) wants to merge 27 commits into
mainfrom
sayanshaw/qwen-native-tool-continuations
Open

Sayan Shaw (sayanshaw24) wants to merge 27 commits into
mainfrom
sayanshaw/qwen-native-tool-continuations

Conversation

@sayanshaw24

Copy link
Copy Markdown
Contributor

Why

Qwen templates define native tool protocols and <tool_response> history, but Foundry's canonical projection bypasses those contracts on continuation turns. In real multi-turn requests this can make the model repeat an already completed call, exhaust its output in hidden reasoning, or fail to select legitimate pending work.

This is stacked on #1231, which provides template-qualified protocol detection. #1264 remains a separate narrow Qwen2 required/named compatibility change.

What

  • Project qualified Qwen tools, assistant calls, and tool results through the model's native template.
  • Support native function/parameter XML and tagged-JSON tool-call dialects.
  • Track completed name+arguments pairs and reject exact repeats before publication.
  • Permit the same function with different arguments.
  • Retry an intercepted exact repeat once with completed argument objects excluded, while retaining declared-tool lookup and full JSON Schema validation.
  • Recover native post-tool turns that consume their budget in hidden reasoning without producing visible text.
  • Keep native activation template-qualified so legacy/canonical models do not change behavior.
  • Harden tagged-JSON parsing against malformed envelopes, duplicate/extra fields, and partial publication.

Validation

Deterministic combined stack validation:

  • Qwen2 focused compatibility: 10/10 passed.
  • Native Qwen regression selection: 114/114 passed.
  • SDK integration: 158/158 passed.
  • Cache suite: 17 passed, 2 optional BYOM skips.
  • Unit suite: 2,630 passed, 3 skipped; one unrelated host-lifecycle timeout reproduced independently.

Real-model validation:

  • Qwen 3.8 27B CUDA: 16/16 Ignite scenarios passed across streaming and non-streaming.
  • Qwen 3.8 Flash Next FP8 Engine: 16/16 Ignite scenarios passed across streaming and non-streaming.
  • Covered automatic greeting/no-call, required and named choice, single-result final answers, serial different-tool work, same-tool/different-arguments work, parallel results, exact-repeat prevention, and failed-tool no-retry.

The exact repeated call observed before this change is intercepted and never published; no repeated call occurred in either real-model matrix.

Probe model-published tool boundaries and tool-result projection semantics for every compatible model type. Preserve explicit catalog capability metadata and fail closed when the rendered protocol is ambiguous.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Treat unknown catalog metadata as inferable, preserve single-call support when result rendering fails, and require probe sentinels to occur in one complete non-nested tool region.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restore unrelated formatting while retaining the review fixes and their regression coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the tool-capability precedence helper on the catalog API's int64_t type so MSVC does not reject a narrowing optional conversion under warnings-as-errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Preserve free-form automatic responses, enforce tool-only choices, and normalize strict Qwen tool envelopes without leaking raw markup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Scope Qwen2 compatibility, distinguish executable wrappers from Markdown examples, prevent tool-only text leakage, and preserve stop and retry semantics across backends.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Restore automatic tool-calling behavior to the frozen base and retain only the Qwen2 required/named compatibility path with focused regression coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use compatibility boundaries during guided retry and admit validated bare JSON only for fenced tool-only recovery. Detect Qwen2 catalog variants by model ID when needed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Project template-qualified Qwen tools and results through the native protocol, reject completed-call repetition, and recover bounded final answers after tool results.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vercel

vercel Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
foundry-local Ready Ready Preview Nov 8, 2026 3:10am UTC

Request Review

Resolve native tool boundaries from the model-published GenAI tags and compare completed calls using semantic JSON equality.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

# Conflicts:
#	sdk_v2/cpp/src/inferencing/generative/chat/chat_session.cc
#	sdk_v2/cpp/src/inferencing/generative/toolcalling/tool_call_context.h
Base automatically changed from sayanshaw/auto-tool-protocol to main November 6, 2026 01:02
Copilot AI balanced review requested due to automatic review settings November 6, 2026 19:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved critical issues can cause freed-memory reads and route valid tool calls as ordinary text, bypassing parsing, validation, and repeat checks.

14 open issues
What changed in this PR

This PR extends Foundry Local’s C++ chat pipeline to use template-qualified native Qwen tool protocols for multi-turn continuations, building on the protocol detection work in #1231.

Changes:

  • Adds native XML and tagged-JSON prompt projection, parsing, and configurable tool boundaries.
  • Tracks completed calls and introduces repeat-call and hidden-reasoning recovery.
  • Expands capability and continuation tests, and includes the Qwen2 required/named fenced-output compatibility path.
File Description
sdk_v2/​cpp/​test/​internal_api/​toolcalling/​tool_call_stream_accumulator_test.cc Tests tagged-JSON parsing and custom boundaries.
sdk_v2/​cpp/​test/​internal_api/​genai_model_instance_test.cc Tests template-qualified native capabilities.
sdk_v2/​cpp/​test/​internal_api/​chat/​chat_template_test.cc Tests fenced required/named prompt instructions.
sdk_v2/​cpp/​test/​internal_api/​chat/​chat_session_test.cc Tests repeat interception, retries, and capability precedence.
sdk_v2/​cpp/​src/​inferencing/​generative/​toolcalling/​tool_call_context.h Adds native protocol and continuation state.
sdk_v2/​cpp/​src/​inferencing/​generative/​toolcalling/​qwen_xml_tool_call_decoder.h Declares tagged-JSON parsing and configurable boundaries.
sdk_v2/​cpp/​src/​inferencing/​generative/​toolcalling/​qwen_xml_tool_call_decoder.cc Implements tagged-JSON parsing and generalized XML boundaries.
sdk_v2/​cpp/​src/​inferencing/​generative/​genai_model_instance.h Exposes inferred tool-protocol capabilities.
sdk_v2/​cpp/​src/​inferencing/​generative/​genai_model_instance.cc Qualifies protocols through rendered-template probes.
sdk_v2/​cpp/​src/​inferencing/​generative/​chat/​prepared_chat_prompt.cc Projects native tool definitions and conversation history.
sdk_v2/​cpp/​src/​inferencing/​generative/​chat/​chat_template.cc Adds fenced tool-only instructions.
sdk_v2/​cpp/​src/​inferencing/​generative/​chat/​chat_session.h Declares tool-support resolution helper.
sdk_v2/​cpp/​src/​inferencing/​generative/​chat/​chat_session.cc Integrates native parsing, completion tracking, and recovery.

Review effort: Balanced

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch was successfully deployed

1 active deployment
Preview — 2b0e3bf4 Deployed Nov 8, 2026 by vercel[bot]
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.

2 participants