Skip to content

studio: add native IFM reasoning and tool-call support - #10948

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
TaskPuppyNatani:feature/k2-horizon-support
Nov 14, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
TaskPuppyNatani:feature/k2-horizon-support

Conversation

@TaskPuppyNatani

Copy link
Copy Markdown
Contributor

Summary

Adds native IFM protocol support to Unsloth Studio, motivated by IFM/K2-Horizon models.

This PR adds:

  • IFM reasoning-channel detection and normalization, including high/medium/low variants and generic close handling
  • IFM turn-end EOS support
  • native IFM tool-call parsing for XML, typed XML, and JSON forms
  • preservation of native IFM tool control tokens across safetensors/MLX streaming paths
  • agentic-loop handling for IFM tool calls, replay, and streaming boundaries
  • safety handling so quoted/fenced examples are not promoted as executable calls, while ordinary quote/fence text inside legitimate IFM argument values remains data
  • regression coverage for malformed/truncated IFM markup, sibling envelopes, quoted examples, control-markup neutralization, reasoning-prefill behavior, and native-token provenance

Validation

Focused current-upstream verification on this branch:

  • 1008 passed, 1 skipped
  • git diff --check clean

The IFM-specific suite passes 67/67 tests, including regressions for unmatched quotes, unclosed fences, multi-envelope parsing, and quoted fake-envelope suppression.

I also validated the behavior end-to-end with IFM/K2-Horizon in Studio for reasoning and native tool calling.

Scope notes

This PR intentionally does not include:

  • the separate Transformers-version tier-routing fix needed for K2-Horizon custom remote code on some Studio setups
  • GGUF/llama.cpp K2-Horizon architecture support
  • unrelated export checkpoint trust changes

Those are being kept separate so this PR stays focused on the generic Studio IFM protocol support.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8189d294a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +4484 to +4486
for signal in TOOL_XML_SIGNALS:
if signal == _IFM_TOOL_CALLS_OPEN:
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Skip bare [ARGS] prose during IFM precedence checks

When ordinary explanatory text before a valid IFM envelope contains [ARGS] (for example, The [ARGS] marker denotes arguments. <ifm|tool_calls>...), this loop treats it as an earlier foreign protocol signal because [ARGS] is included in TOOL_XML_SIGNALS. Unlike the other entries, a bare [ARGS] is only executable when preceded by an enabled tool name, and the rehearsal regex below already detects that genuine form. As written, _parse_ifm_tool_calls_with_spans stops before parsing the valid IFM envelope, so the requested tool is silently never executed.

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

(Python 3.13, l-r) is failing on the refactor guard rather than on behaviour:

FAILED tests/test_refactor_guard.py::test_ast_inventory_matches_the_baseline - ast.core.inference.tool_call_parser.compiled._IFM_NAME_RE: added
FAILED tests/test_refactor_guard.py::test_runtime_surface_matches_the_baseline - runtime.core.inference.tool_call_parser.dir: added '_IFM_ARG_KEY_CLOSE'
FAILED tests/test_refactor_guard.py::test_guarded_functions_produce_the_same_bytes - golden.core.inference.tool_call_parser._ifm_decode_xml_value: added
FAILED tests/test_refactor_guard.py::test_no_guarded_function_is_driven_by_a_sentinel - no argument fixture for: ['_ifm_decode_xml_value', '_ifm_find_call_close', '_ifm_first_unprotected_foreign_signal']

The guard is doing its job: this PR adds new names to core/inference/tool_call_parser, and the baseline has not been regenerated to accept them. The fourth one is the only one asking for real work, since it wants an argument fixture for each new guarded function rather than just a baseline refresh.

The rest of the red on this PR (Core zoo) was a separate unsloth-zoo main breakage, fixed in unslothai/unsloth-zoo#1219.

Copy link
Copy Markdown
Contributor Author

General validation note for my open PRs:

I’m no longer able to keep my primary machine pinned several versions behind, or repeatedly rebuild historical ROCm/Unsloth environments solely for PR testing. My current priority is restoring and maintaining a stable training setup, because training attempts are currently crashing my system and blocking normal work.

Going forward, I can validate changes on my current working/supported local stack and provide logs or reproductions there. If a PR requires hardware validation on an older ROCm/Unsloth stack, that will need CI or another tester who already has an appropriate environment. If I later have dedicated isolated lab hardware available, I may be able to test older stacks there, but I will not hold back or destabilize my primary environment for PR validation.

This does not withdraw the PR; it is a boundary on what version-specific hardware validation I can personally provide.

Copy link
Copy Markdown
Contributor Author

Hardware testing update: I’m planning to put Ubuntu on an NVMe so I can help with any additional validation you still need on these PRs. It’s going to take me a couple of days to get that environment set up, but once it’s ready I should be available for hardware-specific testing.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T01:17:11.339984Z fb01a99 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 409e9627fa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

spans: list[tuple[int, int, bool]] = []
pos = 0
while True:
start = text.find(_IFM_BLOCK_OPEN, pos)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Exclude quoted and fenced examples from IFM envelope detection

When a tool-enabled model outputs a complete IFM call as a quoted or fenced documentation example, this unconditional opener search accepts it, and both strict and Auto-Heal parsing return an executable call. The safetensors agentic loop consequently dispatches an enabled tool shown only as an example, while display stripping removes the example from the answer. Exclude top-level quote/code regions when identifying IFM envelopes, without excluding quote or fence characters inside genuine argument values.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing: every envelope format on main (Hermes <tool_call>, MiniMax, GLM) executes a complete call inside a fence or quotes the same way; IFM matches that. Quote/fence heuristics also hid real calls after an unmatched apostrophe in the original version of this PR, which dropped legitimate calls.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 858322cbef

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

(_ds_pos, _parse_deepseek_tool_calls),
(_km_pos, _parse_kimi_tool_calls),
(_mm_pos if _mm_pos >= 0 else len(content), _parse_minimax_tool_calls),
(_ifm_pos if _ifm_pos >= 0 else len(content), _parse_ifm_tool_calls),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve enclosing rehearsals before the IFM pre-pass

With web_search and terminal enabled, a valid web_search[ARGS]{"query":"..."} rehearsal whose query string contains a complete IFM terminal example now returns the inner terminal call in both strict and Auto-Heal modes; the parent returns web_search. _marker_inside_leading_envelope() does not recognize NAME[ARGS], and the existing _inside_leading_markerless_body() guard runs only after this pre-pass, so it cannot preserve the string as argument data. Check rehearsal ownership before dispatching IFM; otherwise, the safetensors loop can execute a tool that appeared only in another tool’s input.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not changing here: the same input with a MiniMax block (minimax:tool_call) returns the inner call on main in both modes, so this is how the shared pre-pass treats every native envelope inside a bracket rehearsal, not an IFM regression. IFM models do not emit NAME[ARGS] rehearsals; changing pre-pass ownership for all formats belongs in its own PR.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 13, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 6518a2e211

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 13, 2026
danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 13, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 14, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 14, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 14, 2026
danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 14, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

danielhanchen added a commit to NilayYadav/unsloth that referenced this pull request Nov 14, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: fb01a99a6b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 14, 2026
@danielhanchen
danielhanchen force-pushed the feature/k2-horizon-support branch from fb01a99 to 23b5376 Compare November 14, 2026 08:08
@danielhanchen

Copy link
Copy Markdown
Member

Thank you so much for the PR and for all the work on IFM / K2-Horizon support!

After discussing it, we've decided not to add model-specific tool-call syntax for individual custom models in Studio for now, so we won't be merging the parser changes. We still really appreciate the contribution and want you credited, so this PR is merged as a single empty commit authored by you.

Thanks again, and we'd be happy to see more PRs from you in the future!

@danielhanchen
danielhanchen merged commit 8788430 into unslothai:main Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants