Skip to main content

Issue

When deploying a model as a Model API, the build completes successfully but the model fails to start, with the following error:
The model’s Python code uses the Transformers package. By default, Transformers sets its cache directory to /.cache, which may not be writable in the Model API environment.

Solution

Override the default cache path by setting TRANSFORMERS_CACHE to a writable directory. Python:
Bash:
Replace /path/to/writable/cache/ with a directory your model environment can write to, such as a path under your project or home directory.

Set HF_HOME for broader cache coverage

The TRANSFORMERS_CACHE variable controls only the Transformers cache. The Hugging Face (HF) HF_HOME variable controls a broader set of caches, including the datasets cache. Set HF_HOME to cover both:
To also protect against cases where the environment variable isn’t set, create a symlink from the default cache location to your writable directory:

How the default cache directory is determined

The Transformers documentation describes cache setup and the order of priority for environment variables:
Pretrained models are downloaded and locally cached at: ~/.cache/huggingface/transformers/. This is the default directory given by the shell environment variable TRANSFORMERS_CACHE. On Windows, the default directory is given by C:\Users\username\.cache\huggingface\transformers. You can change the shell environment variables shown below — in order of priority — to specify a different cache directory:
  1. Shell environment variable (default): TRANSFORMERS_CACHE
  2. Shell environment variable: HF_HOME + transformers/
  3. Shell environment variable: XDG_CACHE_HOME + /huggingface/transformers
HF_HOME defaults to $XDG_CACHE_HOME/huggingface. If XDG_CACHE_HOME is not set, it defaults to $HOME/.cache, making the usual default $HOME/.cache/huggingface.
Last modified on October 12, 2026