transformers - ✅(Solved) Fix Some ANSI codes are generated by utils/loading_report even when not connected to terminal [3 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44336Fetched 2026-04-08 00:29:07
View on GitHub
Comments
1
Participants
2
Timeline
12
Reactions
0
Author
Participants
Timeline (top)
cross-referenced ×3subscribed ×3mentioned ×2closed ×1

Fix Action

Fixed

PR fix notes

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

Description (problem / solution / changelog)

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

Changed files

  • src/transformers/utils/loading_report.py (modified, +12/-10)

PR #44400: fix: interactive terminal checks for formatting in loading_report.py

Description (problem / solution / changelog)

What does this PR do?

As per title, confirms interactive terminal before adding formatting to loading_report output.

<!-- Congratulations! You've made it this far! You're not quite done yet though. Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution. Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change. Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost. --> <!-- Remove if not applicable -->

Fixes #44336

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @ If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**. Please tag fewer than 3 people. Models: - text models: @ArthurZucker @Cyrilvallez - vision models: @yonigozlan @molbap - audio models: @eustlb @ebezzam @vasqu - multimodal models: @zucchini-nlp - graph models: @clefourrier Library: - generate: @zucchini-nlp (visual-language models) or @gante (all others) - continuous batching: @remi-or @ArthurZucker @McPatate - pipelines: @Rocketknight1 - tokenizers: @ArthurZucker and @itazap - trainer: @SunMarc - attention: @vasqu @ArthurZucker @CyrilVallez - model loading (from pretrained, etc): @CyrilVallez - distributed: @3outeille @ArthurZucker - CIs: @ydshieh Integrations: - ray/raytune: @richardliaw, @amogkam - Big Model Inference: @SunMarc - quantization: @SunMarc @MekkCyber - kernels: @MekkCyber @drbh - peft: @BenjaminBossan @githubnemo Devices/Backends: - AMD ROCm: @ivarflakstad - Intel XPU: @IlyasMoutawwakil - Ascend NPU: @ivarflakstad Documentation: @stevhliu Research projects are not maintained and should be taken as is. -->

Changed files

  • src/transformers/utils/loading_report.py (modified, +14/-6)

PR #44544: Fix ansi codes in loading reports when not connected to terminal

Description (problem / solution / changelog)

What does this PR do?

As per the title. Fixes https://github.com/huggingface/transformers/issues/44336

Changed files

  • src/transformers/utils/loading_report.py (modified, +18/-18)
RAW_BUFFERClick to expand / collapse

System Info

The bug does not depend on system info, it is obvious in sources.

Who can help?

@Cyrilvallez

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Hi @Cyrilvallez ,

the 0fa2c2f changed ANSI handling in utils/loading_report.py from ANSI class to PALETTE plus _color. However, the bold and italic are now used without any check that we are connected to terminal, notably:

https://github.com/huggingface/transformers/blob/46d09b53ec4f38ff3e6d893c802e5f9af98fba68/src/transformers/utils/loading_report.py#L266

https://github.com/huggingface/transformers/blob/46d09b53ec4f38ff3e6d893c802e5f9af98fba68/src/transformers/utils/loading_report.py#L182

https://github.com/huggingface/transformers/blob/46d09b53ec4f38ff3e6d893c802e5f9af98fba68/src/transformers/utils/loading_report.py#L191

https://github.com/huggingface/transformers/blob/46d09b53ec4f38ff3e6d893c802e5f9af98fba68/src/transformers/utils/loading_report.py#L200

https://github.com/huggingface/transformers/blob/46d09b53ec4f38ff3e6d893c802e5f9af98fba68/src/transformers/utils/loading_report.py#L214

https://github.com/huggingface/transformers/blob/46d09b53ec4f38ff3e6d893c802e5f9af98fba68/src/transformers/utils/loading_report.py#L230

so these are now printed even when redirecting the output.

Expected behavior

Do not print ANSI codes for bold and italics when sys.stdout.isatty() is false, probably by using _color. But _color always also prints the ANSI reset at the end, and it is not clear to me when should some of the _PALETTE['italics'] end.

extent analysis

Fix Plan

Fix Name

Fix ANSI handling in utils/loading_report.py to prevent printing ANSI codes when output is redirected.

Steps

  1. Check if output is a terminal: Before printing ANSI codes, check if sys.stdout.isatty() is True. If not, use the _color function without ANSI codes.
  2. Modify utils/loading_report.py:

import sys

...

if sys.stdout.isatty(): # Use ANSI codes for bold and italics print(f"\033[1m{bold}\033[0m") # Bold print(f"\033[3m{italic}\033[0m") # Italic else: # Use _color function without ANSI codes print(_color(bold)) print(_color(italic))

3. **Update `_color` function**: To prevent printing ANSI reset at the end, modify the `_color` function to not include the ANSI reset code (`\033[0m`) when not printing to a terminal.
   ```python
def _color(text):
    if sys.stdout.isatty():
        return f"\033[1m{text}\033[0m"  # Bold
    else:
        return text
  1. Test the fix: Run the script with output redirected to a file or pipe, and verify that ANSI codes are not printed.

Verification

  • Run the script with output redirected to a file or pipe.
  • Check the output file or pipe to ensure that ANSI codes are not printed.
  • Verify that the script behaves as expected when printing to a terminal.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Do not print ANSI codes for bold and italics when sys.stdout.isatty() is false, probably by using _color. But _color always also prints the ANSI reset at the end, and it is not clear to me when should some of the _PALETTE['italics'] end.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

transformers - ✅(Solved) Fix Some ANSI codes are generated by utils/loading_report even when not connected to terminal [3 pull requests, 1 comments, 2 participants]