Constraining an LLM output to match a regular expression

Hi! I’m happy to share a new Community Blog Post introducing two algorithms that guarantee that an LLM output will match an arbitrary regex:

It comes with a detailed notebook and a technical appendix with the formal description of the algorithms and their correctness proof.

If you’re looking for a TL;DR, please have a look at this Twitter thread: