Skip to content

feat/fix: address studio UI contrast, Windows encoding crashes, download confirmation, and DPOConfig deprecations - #6543

Closed
TheJagStudio wants to merge 6 commits into
unslothai:mainfrom
TheJagStudio:main
Closed

TheJagStudio wants to merge 6 commits into
unslothai:mainfrom
TheJagStudio:main

Conversation

@TheJagStudio

Copy link
Copy Markdown
Contributor

Overview

This pull request introduces a cohesive set of fixes and feature improvements across the Unsloth backend, CLI, and Studio frontend. It comprehensively addresses recent community-reported issues ranging from environment-specific crashes to user experience enhancements in the local Studio interface.

Changes Breakdown

1. 🧯 Fix Windows UnicodeEncodeError crashes (Closes #6489)

  • CLI & Startup: Configured sys.stdout and sys.stderr streams to force utf-8 handling (with replacement fallbacks) when running on Windows platforms. This prevents the startup banner (and the 🦥 emoji) from crashing on default cp1252 terminals.
  • File I/O: Explicitly specified encoding="utf-8" across file-read operations within trainer.py, chat.py, and authentication utility writers.

2. 🛡️ Add Confirmation Gate for First-Time Model Downloads (Closes #5125)

  • Added a confirmed flag field to DownloadModelRequest and a corresponding needs_confirmation property to the response schemas.
  • Integrated a local cache scanning check using hf_cache_scan. If a model isn't present locally and the request hasn't been explicitly pre-confirmed, the server safely responds with a confirmation_required state, allowing the frontend to present a storage-warning dialog before proceeding.

3. 🎨 Improve Code Block Contrast & Readability in Studio UI (Closes #6469)

  • Updated index.css to implement a softer gray surface background for inline and fenced code styles in light mode, visually separating code snippets from standard chat bubbles.
  • Added a padding-right: 5rem; buffer on code-block headers to prevent language labels from sliding beneath the absolute-positioned copy/download action buttons on narrower layouts.

4. 📦 Update DPO Trainer to support DPOConfig (Closes #4155)

  • Patched TRL's DPOTrainer.__init__ to gracefully accept native trl.DPOConfig arguments alongside a backward-compatible warning shim for legacy TrainingArguments.
  • Introduced UnslothDPOConfig within unsloth/trainer.py as a robust drop-in replacement that defaults to standard TrainingArguments if an older TRL version is installed.

Verification & Testing

  • Verified that file modifications compile and pass basic lint constraints.
  • Confirmed that utf-8 stream reconfiguration correctly guards against UnicodeEncodeError on Windows terminal contexts.
  • Ensured response schemas remain fully backwards-compatible for clients not leveraging the download confirmation flow.

claude and others added 2 commits June 21, 2026 13:59
…nslothai#5125

Issue unslothai#4155 — DPO: add UnslothDPOConfig and update PatchDPOTrainer
- unsloth/trainer.py: import trl.DPOConfig, expose UnslothDPOConfig
  subclass (falls back to TrainingArguments when trl<0.7); export via __all__
- unsloth/models/dpo.py: replace no-op PatchDPOTrainer with a patch that
  monkey-patches DPOTrainer.__init__ to emit a DeprecationWarning when legacy
  TrainingArguments is passed instead of DPOConfig; includes docstring example

Issue unslothai#6469 — Code block readability in Unsloth Studio UI
- studio/frontend/src/index.css: add #f3f4f6 background + subtle border for
  fenced code blocks in light mode (dark mode keeps #181818); add
  padding-right to code-block-header so the language label is never obscured
  by the absolute-positioned copy/download action buttons; add matching inline
  code surface for <code> spans

Issue unslothai#6489 — UnicodeEncodeError (charmap codec) on Windows
- studio/backend/main.py: reconfigure sys.stdout/stderr to utf-8 with
  errors=replace at process start, before any print (Windows only)
- studio/backend/startup_banner.py: introduce _safe_print() that falls back
  to encode/replace on UnicodeEncodeError; use it for all banner output
- studio/backend/core/training/trainer.py: add encoding='utf-8' to both
  open() calls on adapter_config.json
- unsloth_cli/commands/chat.py: add encoding='utf-8' to adapter_config open
- unsloth_cli/commands/studio.py: pass PYTHONIOENCODING=utf-8:replace in the
  child-process environment when spawning the backend on Windows; also add
  encoding='utf-8' to the auth-secret fdopen call

Issue unslothai#5125 — Request permission before downloading model on first run
- studio/backend/hub/schemas/downloads.py: add confirmed:bool=False field to
  DownloadModelRequest; add needs_confirmation:bool=False to DownloadStartResponse
- studio/backend/hub/services/models/downloads.py: add _is_model_in_local_cache()
  helper that checks the HF cache; when confirmed=False and model is absent from
  cache, return state='confirmation_required' / needs_confirmation=True before
  spawning any download worker — the frontend can then show a dialog and
  re-POST with confirmed=True

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_016EuHUXBUpgszgrjtjL7ghp
Copilot AI review requested due to automatic review settings June 21, 2026 14:19

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 introduces several improvements, including forcing UTF-8 encoding on Windows streams and file operations to prevent UnicodeEncodeErrors, implementing a download confirmation gate for non-cached models, adding frontend styling for code blocks, and patching TRL's DPOTrainer to support DPOConfig with backward compatibility. The reviewer recommended enhancing the DPOTrainer patch to detect deprecated TrainingArguments passed as positional arguments and to dynamically generate the warning message.

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 thread unsloth/models/dpo.py Resolved
TheJagStudio and others added 3 commits June 21, 2026 10:22
@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: 50e9b4af72

ℹ️ 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 +133 to +136
"state": "confirmation_required",
"accepted": False,
"generation": 0,
"needs_confirmation": True,

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 Add the client confirmation retry before rejecting downloads

The Studio client path still never sends the new confirmed field (startModelDownload only serializes the existing repo/variant/transport fields), and startJob treats any accepted: false response as a terminal error. In that current UI flow, every first-time model download reaches this confirmation_required response but no dialog/retry can ever be issued, so users just see a failed download and cannot start it from Studio.

Useful? React with 👍 / 👎.

"""Return True if *repo_id* (and optional GGUF *variant*) is already present
in the local HuggingFace cache so no network download is needed."""
try:
scan = hf_cache_scan.scan_cache_dir()

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 Use the repo cache scanner API for the cache bypass

Here hf_cache_scan is the local hub.utils.inventory_scan module, which exposes all_hf_cache_scans() rather than scan_cache_dir(). This call raises AttributeError and is swallowed by the broad except, so _is_model_in_local_cache() always returns False; even models that are already cached are reported as needing confirmation, defeating the intended local-cache bypass.

Useful? React with 👍 / 👎.

Imagineer99 added a commit to Imagineer99/unsloth that referenced this pull request Jun 23, 2026
Extracted and narrowed from unslothai#6543 by @TheJagStudio.

This keeps the startup/banner and text file encoding hardening separate from the already-merged Python code-exec UTF-8 fix in unslothai#6548.

Co-authored-by: Jagrat Patel <[email protected]>
Imagineer99 added a commit that referenced this pull request Jul 1, 2026
Extracted and narrowed from #6543 by @TheJagStudio.

This keeps the startup/banner and text file encoding hardening separate from the already-merged Python code-exec UTF-8 fix in #6548.

Co-authored-by: Jagrat Patel <[email protected]>
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 19, 2026
@danielhanchen

Copy link
Copy Markdown
Member

The utf-8 encoding fixes and the code block contrast change look right, but the download confirmation gate lands backend-only: hub/utils/inventory_scan.py exposes no module-level scan_cache_dir, so _is_model_in_local_cache always falls into its except and returns False, and nothing in the frontend sends confirmed=true. As it stands every Hub download would come back confirmation_required and never start. Could you split this into separate PRs, fix that gate, and drop the DPOTrainer init monkeypatch which only adds a warning?

@danielhanchen

Copy link
Copy Markdown
Member

Main now carries the Windows UTF-8 work in studio/backend/main.py and startup_banner.py via #6614, and the download gate still calls hf_cache_scan.scan_cache_dir, which hub/utils/inventory_scan.py does not define, so _is_model_in_local_cache always returns False and every Hub download would come back confirmation_required with no frontend sending confirmed=true. Could you rebase and narrow this to the index.css contrast fix, either dropping the download gate and the warning-only DPOTrainer init patch or moving them to their own PR?

@danielhanchen danielhanchen self-assigned this Oct 4, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR and for collecting these reports in one place! Closing this since every part is either already on main or no longer needed:

  1. Windows UnicodeEncodeError (UnicodeEncodeError (charmap codec) on Windows in Unsloth Studio / DiffusionGemma #6489): the stdout/stderr UTF-8 reconfigure in studio/backend/main.py, _safe_print in startup_banner.py, the CLI entry point stream reconfigure, and the explicit encoding="utf-8" on the adapter_config.json reads/writes and the auth secret writer all landed on main via Use UTF-8 for Python code-execution subprocess I/O (#6489 class) #6548, Studio: name utf-8 explicitly on the remaining text I/O, with an AST guard #7465 and Studio: read model text as utf-8 so umlauts survive on Windows #7467 (the last adds an AST guard for unnamed text I/O). The hunks here are now duplicates.
  2. Download confirmation ([Feature] Request permission before downloading model on first run #5125): this was already resolved before the PR was opened. The server-side gate here would also be a regression: confirmed defaults to False and the frontend never sends it, so every first-time download started from the Studio UI would return confirmation_required / accepted: false and silently do nothing. Three existing tests in hub/tests/test_model_services.py fail on this branch for the same reason.
  3. Code block contrast ([Bug] Output code block is hard to read #6469): fixed by Studio: keep code block scrollbars off one-line code #6474, and the code-block styling in index.css has been reworked since, so these rules no longer apply cleanly.
  4. DPOConfig ([Docs] Update DPO example to use DPOConfig instead of TrainingArguments #4155): that issue was about the docs example, which lives outside this repo. UnslothDPOConfig is an empty subclass of trl.DPOConfig, and the DPOTrainer.__init__ wrapper only emits a DeprecationWarning; TRL config compatibility is now handled in unsloth/models/rl_config_compat.py.

If you still see the remaining #6489 report (the charmap error in Data Recipe dataset generation), a focused PR for that path would be very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants