transformers-stream-generator

A tiny add-on that makes Hugging Face Transformers' generate() stream tokens one at a time as a Python generator.

Library
PyPI
v0.0.5
95stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
27/100Needs Attention
Development Activity0
Maintenance0
Community36
Maturity52
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
69/100Good
Architecture60
Code Quality62
Innovation70
Learning Curve85

transformers-stream-generator is a small utility that adds real-time token streaming to Hugging Face Transformers text generation. Normally model.generate() returns the whole output at once; this library patches generation so that, with a single flag, generate() instead returns a Python generator that yields each token as the model produces it.

Enabling it takes two lines of setup, after which you pass do_stream=True (with sampling enabled) to generate() and iterate over the results, decoding tokens as they arrive. That makes it easy to build responsive, ChatGPT-style streaming output on top of existing Transformers models without changing your model loading or moving to a different inference stack.

What You Get

  • A one-call init_stream_support() that enables streaming generation
  • A do_stream flag on model.generate() that returns a token generator
  • Real-time, token-by-token output you can decode and display as it arrives
  • Compatibility with existing Hugging Face Transformers models and tokenizers
  • Example scripts for both script-based and web-based streaming demos

Common Use Cases

  • Streaming chatbot responses from a Transformers model token by token
  • Adding live, incremental output to a text-generation demo or app
  • Prototyping ChatGPT-style typing effects on top of open models

Under The Hood

Architecture The library is a thin patch over Transformers’ generation code. Calling init_stream_support() installs a streaming-capable variant of the generation logic (in transformers_stream_generator/main.py) so that when generate() is called with do_stream=True it drives the sampling loop as a Python generator, yielding each newly sampled token id instead of accumulating and returning the full tensor at the end.

Tech Stack A very small pure-Python package built directly on Hugging Face Transformers and PyTorch, packaged with setup.py. It carries no heavy dependencies of its own beyond the Transformers stack it augments.

Code Quality The codebase is intentionally minimal, essentially two modules, with a single example-oriented test client rather than a broad automated suite. It works by monkeypatching Transformers internals, which is effective but couples it to the versions of Transformers whose generation API it mirrors.

API Design From the caller’s perspective the ergonomics are excellent: two lines of setup and one extra keyword argument turn any compatible generate() call into a token stream you can simply iterate. The trade-off for that simplicity is the reliance on patching, which users should keep in mind when upgrading Transformers.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search