
Simply match curly brackets
Making LLMs output JSON is a common problem, and making them only output the JSON is a surprisingly large part of it. While some commercial LLMs were
trained on the task, others will say "Here's the output in JSON format:" regardless of your
prompt, sabotaging your efforts to JSON.parse
the output.
The most complicated solution is structured outputs. You could also just use a {
prefill to get most of the structured outputs effect. But a simpler solution is something like
const jsonText = text.slice(text.indexOf("{"), text.lastIndexOf("}") + 1);
This has the benefit of letting the LLM think before responding, since it matches an object anywhere. Of course, you can adapt this to work with array responses by using [
and ]
instead of curly brackets, and you can rewrite this to port it to a different language.
That's the post. Sorry if you expected something longer.