When will LLMs be able to output in a fixed format (JSON?)
"TLDR: This article explores methods for enabling LLMs to output fixed formats (such as JSON), including the universal prompt approach, post-inference processing solutions (regular expressions), and in-inference processing solutions (dynamic constrained decoding). It analyzes the advantages, disadvantages, and applicable scenarios of each method, and points out the limitations of current research."
Based on probabilistic statistical modeling, LLMs have demonstrated extraordinary potential. However, the probabilistic nature of token output also becomes a small stumbling block for LLM deployment in real-world applications. Here, we discuss several common methods for getting LLMs to produce content in a target format, though it's unclear whether the latest research has made breakthroughs in this area.
The Universal Prompt Approach
By explicitly specifying in the prompt—such as "require JSON format, no extra output" or "output format strictly follows {xxx: xxx, yyy: yyy}"—and thanks to the ever-improving instruction-following capabilities of current LLMs, a well-crafted prompt can produce JSON or other specified format outputs with high probability.
Post-Inference Processing: Regular Expressions
Many times, LLMs still mix in unnecessary filler text, such as: "Sure, I will strictly follow the JSON specification and output the result as {xxx: xxx, yyy: yyy}". In such cases, you can use regular expression matching for post-processing to extract all parts of the text that contain JSON content.
In-Inference Processing: Dynamically Constrained Decoding
If you absolutely require 100% JSON compliance with zero tolerance for errors, you can consider dynamically constrained decoding.
When the model outputs each token during inference, dynamically adjust the probabilities of output tokens. For example, if the model's first token is about to output filler text like "Sure, I will follow..." starting with the character "S", we can directly set the probability of the "{" character to 100%, forcing the model to output the first character of JSON. During subsequent decoding, if a sampled token would make the JSON invalid, reduce that token's probability and increase the probability of tokens that keep the JSON valid.
However, it seems that this approach can somewhat degrade the model's intelligence, and it requires self-deploying the LLM for token-level control, which is a bit cumbersome.
Additionally, this approach can also be considered for LLM safety output—if the model's output might produce prohibited words, forcibly modify token probabilities to avoid generating them.