News

Transformers 5.22.2 hardens chat templates against sandbox escapes

Email

Hugging Face released Transformers 5.22.2 on November 6 with a security-focused change to the Jinja sandbox used for model chat templates.[1][3] If you run models through Python Transformers, this is an update to install rather than admire from a safe distance.

The patch matters because a chat template is more than punctuation around a prompt. Transformers uses it to turn a conversation into the control tokens and message format a particular model expects.[4] The template may come from a model repository, and Transformers’ own patch describes Hub templates as untrusted code.[2] A model can have perfectly ordinary weights while the formatting step around them creates a separate attack surface.

What Hugging Face changed

The immediate issue is unusually low-level. Hugging Face says a CPython bug made it possible to trigger an array overflow through the real `dict` object that Transformers exposed inside its Jinja sandbox.[2] The pull request says the underlying Python bug has been fixed in Python 3.13 through 3.15, but probably will not be fixed in earlier versions.[2]

Transformers now gives templates a deliberately limited replacement for `dict`.[2] It also blocks underscore-prefixed attributes on Jinja namespaces, which cuts off routes toward Python internals, and converts rich message inputs into plain data before a template can inspect them.[2] Images, arrays and other complex objects become inert placeholders for the formatting pass instead of arriving with callable methods attached.[2]

That last change is easy to miss and may be the most sensible part of the patch. A multimodal application can pass decoded images, NumPy arrays, tool descriptions and document objects while building a prompt.[2] Some of those objects have methods that read or write files.[2] A sandbox cannot reliably forbid dangerous attributes on every object from every Python library, so the new code stops handing those objects to the template in the first place.[2]

The release also raises the chat-template dependency from Jinja 3.1.0 to at least 3.1.6.[2] That closes a separate, documented Jinja sandbox escape: CVE-2025-27516 allowed an attacker who controlled a template to reach arbitrary Python code through the `|attr` filter.[5][6] GitHub rates that older issue moderate, with affected Jinja versions through 3.1.5.[6]

These are related defenses, not proof of one demonstrated attack chain. Hugging Face has not published a CVE, severity score or affected-version range for the new CPython-driven route in the 5.22.2 notes or its patch discussion.[1][2] The company documents the dangerous primitives and the fix, but it does not say that anyone used them against a real model download. Treat this as a credible preventive security update, not evidence that the Hub has been compromised.

Who needs to update

The clearest target is anyone who directly uses `AutoTokenizer`, `apply_chat_template`, a Transformers text-generation pipeline, or an application built on top of those pieces.[2][4] The exposure is in template rendering.[2] Merely storing a `.safetensors` or GGUF weight file does not invoke this Python path.

That distinction also keeps the warning from becoming needlessly broad. A pure llama.cpp or Ollama workflow does not become affected simply because the model originated on Hugging Face; the patch changes Python Transformers.[1][2] It matters if the surrounding application also imports Python Transformers to load the tokenizer or render the model’s chat template. Desktop apps and web interfaces often hide their Python environment, so check the app’s release notes before trying to replace a bundled package by hand.

For a normal Python environment, upgrade to Transformers 5.22.2 and Jinja 3.1.6 or newer with the same Python interpreter that runs the model.[1][2] Then print both package versions from that interpreter to confirm the environment changed.

PyPI lists 5.22.2 as released and provides both a wheel and source archive with provenance metadata.[3] HighVRAM downloaded the published wheel, matched its SHA-256 checksum to PyPI, and inspected the installed metadata and chat-template code. The wheel contains the restricted dictionary, plain-data conversion and Jinja 3.1.6 requirement described in the patch.[2][3]

There is one compatibility catch. Transformers 5.22.2 requires Python 3.11 or newer.[3] An older Python environment may report that no matching release exists rather than offering the fix.[3] Do not force a major package upgrade into an appliance or app that pins its own dependencies; update the app, move the environment to a supported Python version, or ask its maintainer for a backport.

No new GPU required

This update changes no model weights, quantization, CUDA path or memory requirement; the release is a software security and distributed-runtime patch.[1][2] It will not make a 70B model fit a 12GB GPU, and it should not change token generation speed. The relevant hardware is whatever already runs the model; the relevant software is the Python process that prepares the prompt.

That makes the buying advice pleasantly short: buy nothing. Patch the software.

Home AI depends on downloading other people’s models, tokenizers and configuration files while keeping the actual conversation on hardware you control. Local execution protects prompts from a cloud provider, but it does not make every downloaded artifact trustworthy. Transformers 5.22.2 narrows one of those trust boundaries without asking for more VRAM or a new runtime.

Direct Transformers users on Python 3.11 or newer should update now. Users of packaged apps should wait for a vendor release rather than breaking the bundle, and avoid newly uploaded or unfamiliar model repositories if the app still renders their Jinja templates with an older Transformers stack. A CVE, a backport for older Python environments, or a fuller affected-version statement from Hugging Face would sharpen the risk; until then, the fixed release is the practical line to follow.

Sources

  1. [1] Transformers v5.22.2 release notes
  2. [2] Hugging Face PR 50431: Harden our Jinja sandboxing
  3. [3] Transformers 5.22.2 on PyPI
  4. [4] Hugging Face chat template documentation
  5. [5] Jinja 3.1.6 changelog
  6. [6] Jinja sandbox breakout advisory GHSA-cpwx-vrp4-4pq7

Share this article

Email