Library / SDK
NVIDIA-Merlin/Transformers4Rec avatar
NVIDIA-Merlin/Transformers4Rec

Transformers4Rec: Session-Based Recommendation on Top of Hugging Face Transformers

Transformers4Rec is a flexible and efficient library for sequential and session-based recommendation and works with PyTorch.

1,281 stars165 forksPythonApache-2.0

At a glance

What is it?
Transformers4Rec is an Apache-2.0 PyTorch library that turns Hugging Face transformer architectures into sequential and session-based recommenders. It is most useful when your input is a sequence of interactions with tabular features, not just item IDs.
Who is it for?
Adopt Transformers4Rec if your problem is genuinely sequential or session-scoped and your data carries more than item IDs, because the schema-driven feature handling is the part that saves real work. Do not adopt it if you want a stable, frequently released dependency, since the latest tagged release is v23.12.00 from 2024-01-11, or if plain matrix factorization already meets your accuracy target.
Can I use it commercially?
Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 24 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 15, 2026, and from our analysis. They are not legal advice.

DEEP OPEN-SOURCE ANALYSIS

The problem Transformers4Rec targets: sequences, not static user vectors

Traditional recommenders treat a user as a bag of past interactions. The README states the limitation plainly: those algorithms "usually ignore the temporal dynamics and the sequence of interactions when trying to model user behavior." Order matters. A cart containing a phone case and a charger means something different from a cart containing a charger and a phone case, and interest drift means last month's clicks may describe a user who no longer exists.

The audience is narrower than "anyone building a recommender." It is RecSys researchers and industry practitioners who already believe a sequence model is the right answer and want to skip writing the transformer plumbing. The README also singles out session-based recommendation, where only the current session is visible. That happens on e-commerce, news, and media sites when a visitor browses anonymously, either because they are new or because GDPR restrictions limit cookie collection. If you have stable long-lived user IDs and rich collaborative signal, this is not the tool you reach for first.

How the library works: HF Transformers plus a Merlin schema

The architecture is a bridge, not a new model family. Transformers4Rec integrates with Hugging Face Transformers, which means the encoder can be any of the architectures that library provides. The README puts the count at more than 64 transformer architectures usable for a sequential recommendation task, and the repository topics list bert, gpt, xlnet, and seq2seq among them. If you already know how to configure a Hugging Face model, you know most of the encoder side.

The interesting part is the input layer. HF Transformers accept sequences of token IDs because they were designed for text. RecSys datasets are not like that. They carry categorical features, numeric features, and sequence-level context alongside the item sequence. Transformers4Rec accepts that tabular sequential data by reading a schema that describes the features, then creating the layers it needs, embedding tables, projection layers, and output layers, based on the target. Adding a feature is a schema change rather than a code change.

Above the encoder sit modular blocks compatible with standard PyTorch modules. The README describes this as enabling custom architectures with multiple towers, multiple heads or tasks, and multiple losses. So the data flow is: raw interaction log, preprocessing that produces a schema, schema-driven input layers, a transformer encoder, then task heads. The Merlin ecosystem supplies the two ends. NVTabular handles preprocessing and exports the schema that Transformers4Rec consumes, and trained models can be exported to Triton Inference Server so that online feature preprocessing and model inference run in one pipeline.

Installing Transformers4Rec and training a first session-based model

The package is published on PyPI, and the README's badge links to the PyPI project page. The repository also carries a conda directory, so a conda installation path exists in the source tree. The README does not spell out a single install command in the text available here, so the safest instruction is to follow the installation page in the documentation rather than guessing at extras.

The starting point in the repository is the examples directory. It contains examples/getting-started-session-based/, examples/end-to-end-session-based/, examples/tutorial/, and examples/t4rec_paper_experiments/. The getting-started example is the one to open first.

The README describes the high-level training flow as requiring a schema first. That schema is the configuration object that tells the library which columns are item IDs, which are categorical, which are numeric, and what the target is. NVTabular can export it, or you can write it by hand for a small dataset.

bash
pip install transformers4rec

After installation, the practical next step is to run the getting-started notebook from the examples tree against your own or a sample session log, and inspect the schema object it builds before touching the model configuration. If the schema is wrong, everything downstream is wrong.

For a container-based setup, the repository ships a conda/ directory and a requirements/ directory with separate files for base, pytorch, and nvtabular dependencies. Those files are the authoritative list of what must be present. Reading requirements/pytorch.txt before installing into an existing environment is cheaper than discovering a version conflict at training time.

Where Transformers4Rec is the wrong tool

The library assumes you have sequences. If your data is one row per user with aggregate features, there is no sequence to model and the transformer encoder has nothing to attend over. A gradient-boosted model on tabular features will be simpler to train, simpler to explain, and simpler to deploy.

The second boundary is the dependency chain. Transformers4Rec is one component of the Merlin ecosystem, and the README frames the fully GPU-accelerated pipeline as involving NVTabular and Triton Inference Server. You can use the library without those, but the integration is the reason the schema mechanism works as smoothly as it does. A team that wants a single pip install with no NVIDIA-specific tooling is signing up for more glue code than the README implies.

The third boundary is release cadence. The most recent release listed is v23.12.00, tagged on 2024-01-11. The last push to the default branch was on 2026-08-23, so the repository is not abandoned, but a project pinning to tagged releases is working against a package whose last tag is well over a year old. Teams with strict dependency review processes should check whether they can consume the main branch, and the README does not document a rollback or downgrade procedure for schema changes.

Finally, cold start is not solved by this library. A session-based model needs a session. A brand new visitor with one interaction gives the encoder almost nothing to work with, and the README does not describe a fallback path for that case.

Transformers4Rec compared with SASRec-style and ID-only sequence frameworks

The README makes a direct comparison. Other sequence learning frameworks, it says, "only accept sequences of item IDs as input and do not provide a modularized, scalable implementation for production usage." That is the real dividing line. Implementations in the SASRec and BERT4Rec family typically take an item ID sequence, embed it, run self-attention, and predict the next ID. They are compact and easy to reproduce.

Transformers4Rec takes a different approach at the input boundary. Because it sits on Hugging Face Transformers, the encoder is swappable across many architectures rather than fixed. Because it reads a schema, the input can include categorical and numeric features alongside the sequence, and the embedding and projection layers are generated from that schema instead of being written by hand. The cost of that flexibility is a configuration layer that a single-file SASRec implementation does not have.

The second difference is serving. The README positions export to Triton Inference Server as part of the pipeline, with online feature preprocessing included. A research-oriented sequence model usually ends at a checkpoint and a scoring script. If your bottleneck is getting a sequence model into a low-latency serving path, that integration is the differentiator. If your bottleneck is finding out whether attention beats a Markov chain on your data, it is overhead.

Licence and the cost of keeping it current

Transformers4Rec is licensed under Apache-2.0. The LICENSE file sits at the repository root, and setup.py carries the standard Apache header. Apache-2.0 permits commercial use and modification and includes a patent grant, which matters for a library that implements attention mechanisms. It also requires preserving notices. This is a description of the licence text, not legal advice; if you redistribute the library or a derivative, have your own counsel read the terms.

The upgrade cost is dominated by three moving parts: the pinned requirement files, the schema format, and the Hugging Face Transformers version underneath. The repository keeps requirements in separate files for base, pytorch, and nvtabular, and pyproject.toml configures black with a line length of 100 and isort with known_third_party entries for cudf, cupy, dask, numba, rmm, and NVTabular. Those pins tell you the maintainers test against a specific stack. Upgrading Transformers4Rec usually means upgrading that stack with it.

There is also a merlin_standard_lib directory at the repository root alongside transformers4rec/. That separation suggests the schema and standard-library layer evolves on its own schedule, which is worth knowing before you fork or pin either half independently.

Editorial conclusion

Adopt Transformers4Rec if your problem is genuinely sequential or session-scoped and your data carries more than item IDs, because the schema-driven feature handling is the part that saves real work. Do not adopt it if you want a stable, frequently released dependency, since the latest tagged release is v23.12.00 from 2024-01-11, or if plain matrix factorization already meets your accuracy target. Verify first that your sequences and schema can be expressed in the Merlin schema format, that your PyTorch and CUDA versions match the pinned requirements files, and that you can serve the exported model without depending on Triton if you do not already run it.

Frequently asked questions

How do I install Transformers4Rec?

The package is published on PyPI, so pip install transformers4rec is the direct route. The repository also contains a conda directory and separate requirement files for base, pytorch, and nvtabular dependencies if you prefer to build the environment yourself.

What is the Transformers4Rec architecture?

It reads a Merlin schema describing your features, generates input layers such as embedding and projection layers from that schema, and feeds them into a transformer encoder from Hugging Face Transformers. Task heads sit on top, and the README says the blocks support multiple towers, heads, and losses.

Does Transformers4Rec handle cold start sessions?

The README does not describe a cold-start fallback. The library is built for sequential and session-based recommendation, so a visitor with a very short session gives the encoder little to attend over, and no alternative path for that case is documented.

Can I use Transformers4Rec without NVTabular or Triton Inference Server?

Yes. The library works with PyTorch, and NVTabular and Triton are integrations that the README presents as part of a fully GPU-accelerated pipeline. Using them removes preprocessing and serving work, but they are not described as mandatory for training.

Which transformer models can Transformers4Rec use?

The README states that more than 64 transformer architectures from Hugging Face Transformers can be used for sequential and session-based recommendation. The repository topics list bert, gpt, and xlnet among the relevant model families.

Official sources

  1. License: Apache-2.0
  2. NVIDIA-Merlin/Transformers4Rec on GitHub
  3. Project website
  4. README
  5. Releases
Community notes

Community notes