Practical overview
When it fits
Use it when a Python language-generation workflow needs sampling steered by explicit constraints or preference scores, rather than prompt conditioning alone.
Who it is for
- Python developers building language-model generation into applications.
- Researchers or engineers who need generated text guided by formal rules or model-based scoring.
Documented uses
- Guide generated text to match a regular expression.
- Combine prompted language models through a product potential.
- Reweight sampled text using a custom potential such as a sentiment scorer.
- Explore schema-constrained generation, including JSON-schema constraints.
Requirements & limits
- The getting-started example uses Hugging Face GPT-2 and asynchronous SMC sampling.
- Regex constraints are represented with a BoolFSA and used with the AWRS token sampler in the guide.
- Custom potentials implement prefix and completion scoring; PyTorch and Transformers are used in the sentiment example.
- Autobatching can combine concurrent potential-scoring calls during SMC, according to the guide.
Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.
Set up a basic language-model sampling example
Build the documented Python example that samples text from a prompted Hugging Face language model using the library's SMC sampler.
Before you start
- Python environment with genlm-control and its documented dependencies available.
- A Hugging Face model such as openai-community/gpt2.
- An asynchronous context for awaiting the sampler call.
Steps
Import PromptedLLM and direct_token_sampler from genlm.control.
Load the model with PromptedLLM.from_name, set a prompt prefix, and create a direct token sampler.
Call the sampler's smc method with the documented particle count, token limit and ESS threshold.
Inspect the resulting posterior or decoded posterior.
Expected outcome
A posterior over sampled token sequences, including a view of complete UTF-8-decodable sequences.
Guide limitations
- This example has no additional constraint, so it demonstrates basic sampling rather than controlled output.
- The guide characterizes the example as somewhat contrived.
- The source documents usage code but does not establish a successful independent execution.
Sources & review
Source record details
- Source availability
- Active
- Sources checked
- 20 November 2026

