Conflicting Keras 3 mitigations
Author: mistersmee
Pull request: no
Labels: name: bug

### System Info

- `transformers` version: 4.49.0
- Platform: Linux-6.13.4-zen1-1-zen-x86_64-with-glibc2.41
- Python version: 3.13.2
- Huggingface_hub version: 0.29.1
- Safetensors version: 0.5.2
- Accelerate version: 1.4.0
- Accelerate config: 	not found
- DeepSpeed version: not installed
- PyTorch version (GPU?): 2.6.0 (False)
- Tensorflow version (GPU?): 2.18.0 (False)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: No

### Who can help?

I was attempting to create a BART pipeline but it failed with the errors: 

```
Traceback (most recent call last):
  File "/usr/lib/python3.13/site-packages/transformers/activations_tf.py", line 22, in <module>
    import tf_keras as keras
ModuleNotFoundError: No module named 'tf_keras'
```

```  
File "/usr/lib/python3.13/site-packages/transformers/models/bart/modeling_tf_bart.py", line 25, in <module>
    from ...activations_tf import get_tf_activation
  File "/usr/lib/python3.13/site-packages/transformers/activations_tf.py", line 27, in <module>
    raise ValueError(
    ...<3 lines>...
    )
ValueError: Your currently installed version of Keras is Keras 3, but this is not yet supported in Transformers. Please install the backwards-compatible tf-keras package with `pip install tf-keras`.
```

```
File "/usr/lib/python3.13/site-packages/transformers/utils/import_utils.py", line 1865, in _get_module
    raise RuntimeError(
    ...<2 lines>...
    ) from e
RuntimeError: Failed to import transformers.models.bart.modeling_tf_bart because of the following error (look up to see its traceback):
Your currently installed version of Keras is Keras 3, but this is not yet supported in Transformers. Please install the backwards-compatible tf-keras package with `pip install tf-keras`.
```

Now, I did install the relevant package and it fixed it and that's fine. But, the problem I wished to highlight is that there are two different methods of dealing with Keras 3 implemented: in PRs #28588 and #29598.

And theoretically, setting the environment variable `TF_USE_LEGACY_KERAS=1` should force tensorflow to use Keras 2 only and fix the issue without needing the `tf_keras` package. Unless I'm misunderstanding something.

If so, then the `try: import tf_keras` block should be inside the `elif os.environ["TF_USE_LEGACY_KERAS"] != "1":` block, I think, shouldn't it? Or a refactor of the whole thing.

### 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

Steps to reproduce the behaviour (hopefully):

1. Set `export TF_USE_LEGACY_KERAS=1`
2. Don't have `tf_keras` installed.
3. Try to set up a BART pipeline like so:
```
from transformers import pipeline

# 1. Set up the Hugging Face summarization pipeline using BART model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
```
or in any other suitable manner.

I understand my Step 2 fixes the problem, but it's not the problem I'm reporting, rather the approach to mitigating usage of Keras 3.


### Expected behavior

I expect that just setting the environment variable would force the use of Keras 2, and `tf_keras` not be needed.

Comments:
Hi @mistersmee, `TF_USE_LEGACY_KERAS` doesn't mean there's no longer any need for the `tf-keras` package! Instead, it reroutes `tf.keras` to point to `tf-keras` instead of the independent Keras 3 `keras` package. Without `tf-keras` installed, I'm not sure what `TF_USE_LEGACY_KERAS` does, but I suspect it has no effect.

Ah, I see. I did misunderstand something, then, I didn't read the relevant section in the Keras 3 announcement fully. Thanks for clearing it up!