Skip to content

Fix and optimize ANSI color handling in loading report for interactive terminals#44341

Closed
Kokonico wants to merge 1 commit into
huggingface:mainfrom
Kokonico:terminal-ansi-fix
Closed

Fix and optimize ANSI color handling in loading report for interactive terminals#44341
Kokonico wants to merge 1 commit into
huggingface:mainfrom
Kokonico:terminal-ansi-fix

Conversation

@Kokonico
Copy link
Copy Markdown

@Kokonico Kokonico commented Feb 27, 2026

Fixes #44336

Changes

  • Added a new _palette function to return the ANSI code for a given color or format only if sys.stdout is interactive. (src/transformers/utils/loading_report.py)
  • Updated all usages of PALETTE[<format>] in the loading report to use _palette(<format>), ensuring formatting is only applied in interactive terminals. (src/transformers/utils/loading_report.py)
  • Shortened the _color function to a singular line.

Who can help

@Cyrilvallez

Copilot AI review requested due to automatic review settings February 27, 2026 19:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to ensure ANSI formatting codes in the state-dict loading report are only emitted when running in an interactive terminal, avoiding escape sequences in redirected/logged output.

Changes:

  • Added a _palette() helper to gate raw ANSI palette codes on sys.stdout.isatty().
  • Updated several in-report uses of PALETTE['italic'] to _palette('italic') so formatting is skipped in non-interactive contexts.

Comment thread src/transformers/utils/loading_report.py Outdated
@Kokonico Kokonico changed the title Fix ANSI color handling in loading report for interactive terminals (#44336) Fix and optimize ANSI color handling in loading report for interactive terminals (#44336) Mar 1, 2026
@Kokonico Kokonico deleted the terminal-ansi-fix branch March 2, 2026 17:41
@Kokonico Kokonico changed the title Fix and optimize ANSI color handling in loading report for interactive terminals (#44336) Fix and optimize ANSI color handling in loading report for interactive terminals Mar 2, 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.

Some ANSI codes are generated by utils/loading_report even when not connected to terminal

4 participants