DEV Community

오병진
오병진

Posted on

10 5 4 4 3

How to Force an LLM to Output JSON

How does an LLM reliably produce responses that strictly follow JSON syntax when using features like "json_mode" or "function calling"?

These options actually provide an answer to the question: "How can we get an LLM to generate responses exactly the way we want?"

You're probably familiar with the fact that LLMs generate responses token by token, step-by-step.

But what's not commonly known, especially outside technical circles, is that each token is generated probabilistically.

Then, what if we could influence or adjust the probabilities, discouraging tokens that don't match our desired format?

Wouldn't that let us reliably produce code snippets in JSON, CSV, or Python scripts with correct syntax?

Surprisingly, this approach is widely used in practice—known under names like json_mode, structured output, or function calling.

Given that tokens are selected probabilistically, couldn't we intentionally manipulate these probabilities?

Here's an example from llama.cpp:
https://github.com/ggml-org/llama.cpp/blob/master/grammars/json.gbnf

By artificially setting the probabilities of grammatically incorrect tokens to zero, we can ensure the LLM strictly adheres to the desired syntax.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay