Skip to content

feat: improve Unsloth Studio chat title generation quality - #6697

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
mvanhorn:feat/6322-improve-chat-title-generation
Jun 26, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
mvanhorn:feat/6322-improve-chat-title-generation

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

Auto-generated chat titles in Unsloth Studio were derived from the first user message alone, with a prompt that asked the model to title "the user's message." For conversational threads that produced titles echoing the user's exact phrasing rather than naming what the conversation was actually about. This improves the quality of those titles in two ways: the title model now receives the first assistant reply as additional context, and the system prompt asks it to summarize the conversation topic rather than the user's wording.

Why this matters

A chat title is the only thing distinguishing threads in the sidebar, so a title that parrots the opening message ("how do i reset") is much less useful than one that names the topic ("Password reset flow"). Issue #6322 asks for better title generation; feeding the assistant's response gives the model the context it needs to summarize the exchange instead of restating the prompt.

Changes

The title-generation path now locates the first user message and the first assistant reply that follows it, clips both, and passes them to the model under explicit User: / Assistant: labels. Because labeling the payload makes it possible for the model to echo a User: / Assistant: prefix back into the title, the existing echo fail-safe in normalizeTitle now runs against the raw title before punctuation is stripped (the punctuation pass would otherwise erase the : that the guard keys on), so an echoed label falls back to the deterministic user-text title instead of being persisted.

Testing

Added tests/studio/test_chat_title_generation.py, a source-level regression suite (the Studio frontend has no JS test harness) covering: the prompt targeting the conversation topic, the payload carrying the optional assistant reply, the adapter selecting the first assistant reply after the first user message, the auto-title-disabled fallback, the model-failure fallback, and the normalizer's output constraints including the role-label guard. All 6 pass under pytest; ruff check on the new file is clean.

Fixes #6322

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request enhances the chat title generation logic by incorporating the assistant's first response as context, updating the model prompt to focus on summarizing the conversation topic, and adding a fail-safe check to catch echoed labels. It also introduces a Python regression test suite to verify these changes. The review feedback highlights opportunities to expand the fail-safe regex to eliminate dead code, optimize message retrieval by avoiding array slicing, and improve the robustness of the brace-counting helper in the test suite.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +449 to +451
if (/^\s*(user|assistant)\s*:/i.test(title)) {
return null;
}

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.

medium

The new check /^\s*(user|assistant)\s*:/i successfully catches echoed labels before punctuation stripping. However, the subsequent check /\b(user|base|lora|assistant)\s*:/i.test(title) (on line 457) is now dead code because the preceding title.replace(/[.!?:;,]+/g, " ") replaces all colons with spaces, meaning a colon will never be matched there.

To handle all potential echoed labels (including base and lora) and clean up the dead code, we should expand this regex to include them, and then the redundant post-punctuation check can be safely removed.

Suggested change
if (/^\s*(user|assistant)\s*:/i.test(title)) {
return null;
}
if (/^\s*(user|assistant|base|lora)\s*:/i.test(title)) {
return null;
}

Comment on lines +747 to +752
const firstAssistant =
firstUserIndex === -1
? undefined
: messages
.slice(firstUserIndex + 1)
.find((m) => m.role === "assistant");

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.

medium

Instead of slicing the messages array to find the first assistant message after the user message, you can use the index argument of the find callback. This avoids allocating a new shallow copy of the array on every title generation run.

      const firstAssistant =
        firstUserIndex === -1
          ? undefined
          : messages.find((m, i) => m.role === "assistant" && i > firstUserIndex);

Comment on lines +23 to +38
def _balanced_block(src: str, anchor: str) -> str:
start = src.find(anchor)
assert start != -1, f"anchor {anchor!r} not found"
body_start = src.find("{", start)
assert body_start != -1, f"body opener after {anchor!r} not found"

depth = 0
for index in range(body_start, len(src)):
char = src[index]
if char == "{":
depth += 1
elif char == "}":
depth -= 1
if depth == 0:
return src[start : index + 1]
raise AssertionError(f"unbalanced block after {anchor!r}")

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.

medium

The _balanced_block helper relies on a simple brace-counting parser. This is highly fragile and will fail if the TSX file contains braces inside string literals, template literals (e.g., ${variable}), comments, or regular expressions (e.g., /[a-z]{1,3}/).

While a full AST parser in Python is complex, you should at least document this limitation or use a more robust regex-based block extractor to prevent future formatting or syntax changes from unexpectedly breaking the test suite.

Consolidate the echo guard into a single leading-label check (now also
covering base and lora) and drop the post-punctuation duplicate that
could never match a colon once punctuation is stripped. Swap the
slice-based first-assistant lookup for an indexed find to avoid copying
the messages array, and note the brace counter's assumptions in the
test helper.
@danielhanchen

Copy link
Copy Markdown
Member

Pushed a small follow-up addressing the review notes:

  • Folded the echo guard into a single leading-label check and added base and lora to it. The old post-punctuation check could never fire, since the colon is already stripped by replace(/[.!?:;,]+/g, " ") before it runs, so I removed it.
  • Replaced the slice(...).find(...) first-assistant lookup with messages.find((m, i) => m.role === "assistant" && i > firstUserIndex) to avoid copying the messages array.
  • Added a one-line note on _balanced_block documenting that it assumes no unbalanced braces inside strings, regexes, or comments.

Typecheck (tsc -b) and the test suite both pass. Thanks for the clean PR.

@Imagineer99 Imagineer99 self-assigned this Jun 26, 2026
@danielhanchen
danielhanchen merged commit e9c6364 into unslothai:main Jun 26, 2026
35 of 36 checks passed
@danielhanchen

Copy link
Copy Markdown
Member

Thanks!

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.

[Feature] Better chat title generation (probably better than ChatGPT too!)

5 participants