# Make JSON output more likely

**URL:** <https://community.openai.com/t/make-json-output-more-likely/718590>\
**Category:** API\
**Created:** [April 15, 2024, 9:04pm UTC](https://community.openai.com/t/make-json-output-more-likely/718590 "2024-04-15T21:04:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lesswire1](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/lesswire1/32/20717_2.png) [@lesswire1](https://community.openai.com/u/lesswire1)\
**Post date:** [April 15, 2024, 9:04pm UTC](https://community.openai.com/t/make-json-output-more-likely/718590/1 "2024-04-15T21:04:14Z")

</div>

Often you need the output of the LLM to be in json format. Even when you include phrases such as “the output must be in JSON format” or variations thereof, the LLM sometimes outputs something like “here is the output in json” or “```json”.  
This makes the task of downstream parsing of the output of LLM fail (ie, not valid json).

A simple trick to increase the likelihood of LLM creating a pure json as output is to use the “logic\_bias” parameter of openAI API. This parameter is a dictionary that specifies a bias (-100 means not likely and 100 means very likely) a token\_id associated with characters to which you want to make more or less likely to appear in the output.  
For example, the following increases the probability of “{” and “}” and decrease the probability of ``` or ‘’’ in the output.

````auto
  logit_bias: {
    "90": 10, // token ID for "{"
    "92": 10, // token ID for "}"
    "19317": -10, // token ID for "'''"
    "19317": -10, // token ID for "'''"
    "74694": -10 // token ID for "```"   
  }
````

---

<div class="post-metadata">

**Author:** ![LinqLover](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/linqlover/32/448698_2.png) [@LinqLover](https://community.openai.com/u/LinqLover)\
**Post date:** [April 16, 2024, 12:04am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/2 "2024-04-16T00:04:04Z")

</div>

Did you use JSON mode?

[https://platform.openai.com/docs/guides/text-generation/json-mode](https://platform.openai.com/docs/guides/text-generation/json-mode)

---

<div class="post-metadata">

**Author:** ![Diet](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/diet/32/562749_2.png) [@Diet](https://community.openai.com/u/Diet)\
**Post date:** [April 16, 2024, 12:22am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/3 "2024-04-16T00:22:10Z")

</div>

I wouldn’t muck about with logit bias for this, because it can taint the rest of your generation. (depending on what you’re doing, ofc)

In detail:

> `"90": 10, // token ID for "{"` could motivate the model to open more sub objects than expected in certain contexts

> ` "92": 10, // token ID for "}"` could motivate the model to prematurely close objects in certain contexts

I’d rather lower the temp and tell the model how to start. once you’re over the initial { hump, there’s not much that can go wrong 🤔

That said, logit bias is still a good tool to have under one’s belt!

---

<div class="post-metadata">

**Author:** ![jr.2509](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jr.2509](https://community.openai.com/u/jr.2509)\
**Post date:** [April 16, 2024, 4:01am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/4 "2024-04-16T04:01:38Z")

</div>

We had a similar thread recently. Can confirm that using JSON mode overcomes the problem with the unwanted prefix, e.g. ```json.

> [@API with 4Turbo modal not providing a clean JSON](https://community.openai.com/t/api-with-4turbo-modal-not-providing-a-clean-json/710027):
>
> Hello, Maybe someone here can help I have a prompt that worked well with GPT 4 and returned the response in JSON format, but now as I’m testing 4 Turbo modal its not working as expected. The repones has the following in the begging it: “``json” And that is something that is ‘messing up’ my system that is expecting a JSON formatted response.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [April 16, 2024, 4:11am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/5 "2024-04-16T04:11:01Z")

</div>

> [@Diet](#):
>
> once you’re over the initial

Hump? You can be explicit in your instructions:

* * *

`// AI Response from assistant`

`MANDATORY: Every single response produced after the "assistant" prompt must begin with the exact characters shown within the single-quotes: '{"function_name": "'`

`MANDATORY: Every single response then continues with valid JSON output complying to the included JSON schema, and will be validated, allowing no deviation.`

`Note: There is no user to communicate with directly. AI JSON output response is provided directly to an external API interface backend.`

`// JSON Schema for assistant response`

(real json schema as you’d validate AI output with…)

* * *

Also, JSON can be an array starting with [

---

<div class="post-metadata">

**Author:** ![Diet](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/diet/32/562749_2.png) [@Diet](https://community.openai.com/u/Diet)\
**Post date:** [April 16, 2024, 9:09am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/6 "2024-04-16T09:09:45Z")

</div>

I believe you came up with this

> `Start your response with {`

(or [, depending ofc)

Using that as the very last line the chat model sees tends to yield pretty good results. After that, it’s mostly smooth sailing (apart from the apostrophe issue, which can be overcome by using the right schema hint)

Now, of course, if our dear AI providers allowed us to take off the training wheels with these chat models, we’d have a lot more latitude.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/766292_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [April 16, 2024, 9:27am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/7 "2024-04-16T09:27:59Z")

</div>

> [@Diet](#):
>
> if our dear AI providers allowed us to take off the training wheels

Yes, other competitive AI (ahem) allows assistant prompt completion, where you could place that JSON opening and a key directly after the internal “assistant” that is automatically added. It works a treat to write the start of the JSON for `-instruct` on OpenAI completions, set the additional stop sequence to the end of JSON, and pay output rate for only the varying internal content. Just need someone at OpenAI go give ChatML that, and to _not_ treat the developer as an untrustworthy child like the assistants endpoint does.

---

<div class="post-metadata">

**Author:** ![chiheb.design](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/chiheb.design/32/321388_2.png) [@chiheb.design](https://community.openai.com/u/chiheb.design)\
**Post date:** [April 17, 2024, 8:00am UTC](https://community.openai.com/t/make-json-output-more-likely/718590/8 "2024-04-17T08:00:28Z")

</div>

This post was flagged by the community and is temporarily hidden.
