LL

LLMs can generate valid JSON 100% of the time

Hacker News

LLMs can generate valid JSON 100% of the time

Outlines is a Python library that focuses on text generation with large language models. Brandon and I are not LLM experts and started the project a few months ago because we wanted to understand better how the generation process works. Our original background is probabilistic, relational and symbolic programming. Recently we came up with a fast way to generate text that matches a regex ( https://blog.normalcomputing.ai/posts/2023-07-27-regex-guide... ). The basic idea is simple: regular expressions have an equivalent Deterministic-Finite Automaton (DFA) representation. We can transform this DFA into a generative model: in each state we get a list of symbols which correspond to completions that partially match the regular expression. We mask the other symbols in the logits returned by a large language model, sample a new symbol and move to the next state. The subtelty is that language models work with tokens, not symbols, so we derive a new FSM whose alphabet is the model's vocabulary. We can do this in only one pass over the vocabulary. Generating the token masks thus only requires a dictionary lookup at each state. Our method blows other libraries like Microsoft's guidance out of the water. From there it was only a small leap to be able to generate text that follows a JSON schema ( https://json-schema.org/ ), or is parseable into a Pydantic model ( https://docs.pydantic.dev/latest/usage/models/ ). The method works with union types, optional types, nested schemas, arrays, everything. It is guaranteed that the output is parseable. I think it's cool, and I've spent a lot of time watching even tiny models output valid JSON over the weekend. Hope you will too. I look forward to feedback, bug reports, feature requests and discussions! Edit: Link to our pre-print explaining the method and how this can be extended to generate text that follows a Context-Free Grammar https://arxiv.org/abs/2307.09702

Share card

Actual performance

854points
303comments
Made the leaderboard

Launch Intel predictions

Analyze your own launch →
Product HuntOn track for Day 1 leaderboard · Strong signals: model, new, models · Missing: mac, agents, macos
86%86% predicted probability of success on Product Hunt, based on ML models trained on real launch data.
best fitHighest predicted score across all platforms for this description.
Indie HackersFits the IH revenue-focused audience · Strong signals: started · Missing: supports, reddit linkedin, podcasting
82%82% predicted probability of success on Indie Hackers, based on ML models trained on real launch data.
Hacker NewsStrong engagement from HN community · Strong signals: ide, io · Missing: https docs, excited, just released
54%54% predicted probability of success on Hacker News, based on ML models trained on real launch data.
nativeThis product was originally launched on this platform.
TrustMRRLess likely to generate early MRR · Strong signals: month, way · Missing: mobile apps, ios, personal
43%43% predicted probability of success on TrustMRR, based on ML models trained on real launch data.
AppSumoMay struggle as an AppSumo deal · Missing: plus, platform, intuitive
37%37% predicted probability of success on AppSumo, based on ML models trained on real launch data.
Acquire.comPre-revenue stage for this audience · Strong signals: arr · Missing: mrr, revenue, profit
17%17% predicted probability of success on Acquire.com, based on ML models trained on real launch data.
BetaListMay not resonate with beta-testers · Missing: web3, chat, crypto
0%0% predicted probability of success on BetaList, based on ML models trained on real launch data.

Correct prediction on native model

Similar products

Pd
Pdfcpu – Generate PDF from JSON46%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Pdfcpu – Generate PDF from JSON

Hacker News2
JS
JSON.stringify (without circular deps) for AngularJS 1.x41%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

JSON.stringify (without circular deps) for AngularJS 1.x

Hacker News3
JS
JSON.is47%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

JSON.is

Hacker News100
Pa
Parse JSON with Flex, Bison, and Ruby38%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Parse JSON with Flex, Bison, and Ruby

Hacker News3
Pr
Protozoa-tsx, a JSX-to-JSON adapter for protozoa36%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Protozoa-tsx, a JSX-to-JSON adapter for protozoa

Hacker News5
Js
Json-viewer2.0 released48%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Json-viewer2.0 released

Hacker News4
Go
Gojay – Performant JSON encoder/decoder for Golang47%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Gojay – Performant JSON encoder/decoder for Golang

Hacker News153
JS
JSON-Syntax49%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

JSON-Syntax

Hacker News1
Ho
Homogenius – JSON packer/unpacker47%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Homogenius – JSON packer/unpacker

Hacker News1
js
json.is47%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

json.is

Hacker News10