Skip to content

Unsloth Studio: a JSONL reply that quotes <tool_call> no longer stalls the tool parser and fails the turn with RecursionError - #14097

Merged
danielhanchen merged 7 commits into
unslothai:mainfrom
LeoBorcherding:fix/tool-parser-leading-json-loop
Oct 20, 2026
Merged

danielhanchen merged 7 commits into
unslothai:mainfrom
LeoBorcherding:fix/tool-parser-leading-json-loop

Conversation

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Line numbers are at main e795149.

What's broken

When a reply opens with a JSON value that quotes tool markup but isn't a call, parse_tool_calls_from_text (studio/backend/core/inference/tool_call_parser.py:2367) calls itself on the rest of the text (:2403, and the Gemma path at :2426). It does that once per leading value, and every level re-masks the whole remainder. N lines means N levels of recursion and O(N²) masking.

A Hermes-format JSONL dataset pasted into chat is exactly this shape. With tools on, the reply is re-parsed after every streamed answer, so the answer finishes, then the turn stalls and fails with RecursionError.

Fix

Strip and mask once, then loop. A skipped leading value advances content and continues instead of recursing. The mask is equal-length, so slicing it gives the same view the tail call built.

Evidence: CPU on Windows 11, base 08fb7bc vs 66f4b53, with tests/test_tool_parser_leading_json_lines.py.

base this PR
1600 quoting lines RecursionError: maximum recursion depth exceeded passes in 0.34 s
400 lines 4.3 s 0.06 s

Every suite that imports the parser: 3821 pass, plus the same 10 Windows-only failures as base. Not run: a live model reply.

Full logs: LeoBorcherding#322

…instead of recursing

When a reply opened with a bare JSON value quoting tool markup and held no
call, parse_tool_calls_from_text called itself on the rest, once per
value, and re-masked the whole remainder each time. A JSONL reply quoting
<tool_call> on every line (a Hermes-format dataset) stalled for about a
minute and then failed the turn with RecursionError. The skipped values
now advance a loop over the text masked once.
@LeoBorcherding LeoBorcherding changed the title Studio: a JSONL reply that quotes <tool_call> no longer stalls the tool parser and fails the turn with RecursionError Unsloth Studio: a JSONL reply that quotes <tool_call> no longer stalls the tool parser and fails the turn with RecursionError Oct 18, 2026
@danielhanchen danielhanchen self-assigned this Oct 18, 2026
danielhanchen added a commit that referenced this pull request Oct 18, 2026
…pability picks (#14138)

## Summary

`Repo tests (CPU, studio)` fails on every PR whose base includes #14083,
with the same single test:

```
tests/studio/test_model_picker_contracts.py::test_diffusion_capability_labeled_image_video_gen
    assert '{ value: "diffusion", label: "Image/video gen" }' in _read("features/hub/lib/model-type-filter.ts")
```

Seen on #14097, #14106, #14112, #14115, #14122 and #14125 (bases
`72d0d2db`, `b4a4855e`, `4c5fb400`), and it reproduces on main at
`1e2cedde`.

## Cause

#14083 (Studio Hub: On Device filters and sort) moved the On Device list
onto Discover's capability picks, on purpose. `model-type-filter.ts` no
longer keeps its own `MODEL_TYPE_FILTER_OPTIONS` (which held `{ value:
"diffusion", label: "Image/video gen" }`). Its
`matchesInventoryCapabilities` now takes `HubCapabilityFilter` picks and
matches them with Discover's own `matchesHubCapability`, so the On
Device panel shows Discover's "Image gen" and "Video gen" tiles. The PR
describes this as change 2.

The product is right and the test is stale. What the test protects
(#11681) is that the diffusion capability is never presented as "Image
to text". That still holds: the only names now shown for it are
Discover's "Image gen" / "Video gen" options and the capability badge's
"Image gen" / "Video gen".

## Change

Test only, `test_diffusion_capability_labeled_image_video_gen`:

- **Kept as before:**
  - "Image to text" must not appear in the three hub files.
- Discover's options must be `{ value: "image-gen", label: "Image gen"
}` and `{ value: "video-gen", label: "Video gen" }`.
- **New:** the capability badge in `model-capabilities.ts` must name the
same two kinds.
- **Replaced** the literal from the deleted On Device list. Now, if
`model-type-filter.ts` has a `diffusion` option, it must still be
labeled "Image/video gen". Otherwise the file must match through
`matchesHubCapability`, so On Device cannot quietly grow a second,
unchecked set of names.

## Testing

- **This change:** `tests/studio/test_model_picker_contracts.py` gives
201 passed.
- **Before (main `1e2cedde`):** the same file gives 1 failed, 200
passed, the failure CI shows.
- **Each of these makes the test fail:**
  - Relabeling Discover's `image-gen` option "Image to text".
- Renaming `matchesHubCapability` out of `model-type-filter.ts`, so On
Device no longer shares Discover's matcher.
- Adding back an On Device `{ value: "diffusion", label: "Image to text"
}` option.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T14:21:38.697126Z 19832dc 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[bot]

This comment was marked as resolved.

…ning after a quoted opener is not executed

[skip ci]
@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: 85ce154e1d

ℹ️ 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 +2411 to +2413
content = content[end + 1 :]
skipped = True
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 Keep a cursor instead of copying each JSONL suffix

When every leading JSON value quotes a tool marker (the Hermes JSONL input this change handles), this assignment copies the whole remaining response and continue reruns _xml_signal_inside_leading_bare_json, whose _first_foreign_tool_signal performs several whole-suffix searches. The iterative version therefore remains quadratic: on the target tree, 3,200/6,400/12,800 33-byte rows took about 1.2/3.8/13.2 seconds, so a roughly 422 KB answer still blocks the end-of-turn parser for seconds despite avoiding RecursionError. Keep an offset into one buffer and avoid rescanning later signals so each row is consumed once.

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 a correctness bug: base raised RecursionError here; head returns the right answer, about 0.02 s at 400 rows and 1 s at 3,200. A 400 KB JSONL reply taking seconds at end of turn is acceptable, and a cursor would thread offsets through every leading-value helper.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 19832dc637

ℹ️ 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".

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