Skip to main content

How to Set the Keras Backend with KERAS_BACKEND

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Keras 3 doesn't do the maths itself: it runs on a backend, such as TensorFlow, JAX or PyTorch, and picks one when you import it. The KERAS_BACKEND environment variable is how you choose. This guide checks which backend Keras 3.15.1 uses, then switches it to JAX for one command and inside a script.

Syntax​

Syntax (macOS / Linux)
KERAS_BACKEND=<backend> <command>
Syntax (Windows PowerShell)
$env:KERAS_BACKEND = "<backend>"
Syntax (inside a Python script)
os.environ["KERAS_BACKEND"] = "<backend>"
PartWhat it means
KERAS_BACKENDThe environment variable Keras reads when it's imported
<backend>The backend's name. The documentation lists "jax", "torch" and "tensorflow"
<command>The command to run with that backend. Written this way, the variable applies to that one command only
$env:KERAS_BACKENDHow PowerShell sets it. It stays set until you close the window or remove it
os.environ[...]Sets it from inside a script. It must come before the line import keras

The Keras documentation gives both ways to choose: "You can export the environment variable KERAS_BACKEND or you can edit your local config file at ~/.keras/keras.json to configure your backend." And one rule: "The backend must be configured before importing Keras, and the backend cannot be changed after the package has been imported."

The Example​

A uv project, keras-backend-demo, with TensorFlow added by uv add tensorflow. It checks which backend Keras picked, adds JAX, and switches to it.

Prerequisites​

  • A uv project with Keras and TensorFlow. uv add tensorflow installs both. This guide uses Keras 3.15.1 and TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Check Which Backend Keras Uses​

Terminal
uv run python -c "import keras; print(keras.config.backend())"
Output
tensorflow

The terminal after the backend check: tensorflow.

keras.config.backend() returns the name of the backend Keras is using. Nothing has chosen one yet, so Keras used its default: TensorFlow. The command works unchanged in PowerShell, like every uv run command on this page.

Step 2: See Where the Default Is Saved​

Terminal
cat ~/.keras/keras.json
Output
{
"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}

The terminal after printing ~/.keras/keras.json: floatx float32, epsilon 1e-07, backend tensorflow and image_data_format channels_last.

The first time Keras is imported, it writes this file with its settings if the file doesn't exist yet. "backend": "tensorflow" is the default for every project on your computer that doesn't set KERAS_BACKEND.

Step 3: Add a Second Backend​

Keras can only switch to a backend that's installed. Add JAX:

Terminal
uv add jax
Output
Resolved 35 packages in 1.92s
Built keras-backend-demo @ file:///Users/you/source/keras-backend-demo
Prepared 4 packages in 21.89s
Uninstalled 1 package in 2ms
Installed 4 packages in 27ms
+ jax==0.12.1
+ jaxlib==0.12.1
~ keras-backend-demo==0.1.0 (from file:///Users/you/source/keras-backend-demo)
+ scipy==1.18.1

The terminal after uv add jax: 35 packages resolved, 4 prepared, and jax 0.12.1, jaxlib 0.12.1 and scipy 1.18.1 installed, with the project itself reinstalled.

uv added jax 0.12.1 and the two packages it requires that the project didn't have yet, jaxlib and scipy. The line with ~ is the project itself, which uv uninstalled and installed again.

Step 4: Switch the Backend for One Command​

Terminal
KERAS_BACKEND=jax uv run python -c "import keras; print(keras.config.backend())"
Output
jax

The terminal after KERAS_BACKEND=jax uv run python with the backend check: jax.

Keras ran on JAX, while keras.json still says "tensorflow": printing the file again showed it unchanged. The variable wins over the file, and only for as long as it's set:

  • macOS and Linux: KERAS_BACKEND=jax in front of a command sets it for that command only. The next command runs on TensorFlow again.
  • PowerShell: $env:KERAS_BACKEND stays set until you close the window. Remove-Item Env:KERAS_BACKEND removes it, and the check prints tensorflow again.

This first run on JAX took 25 seconds. The JAX runs after it took 1 to 2 seconds.

Step 5: Set the Backend Inside a Script​

When a script should always run on one backend, set the variable in the script itself, before it imports Keras:

in_code.py
import os

os.environ["KERAS_BACKEND"] = "jax"

import keras

print(keras.config.backend())

The in_code.py script in a macOS-style code window: it imports os, sets os.environ["KERAS_BACKEND"] to "jax", then imports keras and prints keras.config.backend().

os.environ holds the running program's environment variables, so this line sets KERAS_BACKEND for the script, and import keras on the next line reads it.

Terminal
uv run python in_code.py
Output
jax

The terminal after uv run python in_code.py: jax.

JAX again, with nothing set in the terminal. The order matters: the import keras line has to come after the line that sets the variable.

Common Mistakes​

MistakeWhat happensDo this instead
Choosing a backend that isn't installedBefore JAX was added, KERAS_BACKEND=jax stopped at ModuleNotFoundError: No module named 'jax'Add it first: uv add jax
Setting os.environ["KERAS_BACKEND"] after import kerasKeras keeps the backend it already picked: the script printed tensorflowSet it before import keras
Writing the name with a capital letter or a typoKERAS_BACKEND=Jax stopped at ValueError: Unable to import backend : JaxWrite it in lowercase, as the documentation does: jax, torch or tensorflow

FAQ​

Can I change the default for every project?​

Yes: change "backend" in ~/.keras/keras.json. With "backend": "jax" in the file, the check from Step 1 printed jax with no variable set. It changes every project on your computer that doesn't set KERAS_BACKEND, so the variable is the safer choice for one project.

Can I change the backend after importing Keras?​

The documentation says no: "the backend cannot be changed after the package has been imported". Keras does have keras.config.set_backend(), but its documentation warns that "Using this function is dangerous and should be done carefully", and here keras.config.backend() still printed tensorflow straight after keras.config.set_backend("jax"). Set KERAS_BACKEND before import keras instead.

Conclusion​

Keras 3.15.1 ran on TensorFlow, its default, until something chose otherwise. KERAS_BACKEND=jax switched it to JAX for one command, os.environ switched it inside a script before import keras, and ~/.keras/keras.json holds the default for everything else.

Additional Resources​