# Guiding Text Generation with Constrained Beam Search in 🤗 Transformers

huggingface.co · Chan Woo Kim · Mar 11, 2022

**Type:** Explainer

## Summary

Constrained beam search introduces direct control over generated text in Hugging Face Transformers. Standard beam search operates token-by-token without knowing the optimal step to force specific words or phrases, making it difficult to enforce mandatory vocabulary or choose between alternative expressions. To resolve this, the generate interface accepts constraints through arguments such as force\_words\_ids and a list of Constraint subclasses like PhrasalConstraint. This mechanism enables disjunctive constraints, where generation must include at least one phrase from a provided set, alongside strictly required sequences. Consequently, practitioners can inject prior knowledge or formatting requirements directly at generation time rather than filtering candidate outputs afterward.

## Context

Traditional beam search operates token-by-token without an easy mechanism to force specific words or phrases into the output at generation time, complicating tasks like enforcing formality or dictionary lookups in neural machine translation.

## Approach / What changed

Hugging Face Transformers added constrained beam search support to model.generate() via the force\_words\_ids parameter and a modular constraints argument that accepts custom Constraint subclasses like PhrasalConstraint.

## Takeaways

- Constrained beam search allows injecting prior requirements directly during generation instead of generating and filtering multiple outputs.
- Disjunctive constraints allow users to supply a list of alternative words or forms, requiring the model to include at least one in the final text.
- Custom constraints can be constructed by subclassing the Constraint abstract interface class and passing instances to model.generate().

**Tags:** [Machine Learning](https://yomu.fyi/topic/machine-learning), [Open Source](https://yomu.fyi/topic/open-source), [Python](https://yomu.fyi/topic/python)

[Read original post](https://huggingface.co/blog/constrained-beam-search)
