# How do I use the new JSON mode?

**URL:** <https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890>\
**Category:** API\
**Tags:** api\
**Created:** [November 6, 2023, 10:02pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890 "2023-11-06T22:02:51Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![tiffiana](https://avatars.discourse-cdn.com/v4/letter/t/e19b73/32.png) [@tiffiana](https://community.openai.com/u/tiffiana)\
**Post date:** [November 8, 2023, 1:34am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/43 "2023-11-08T01:34:57Z")

</div>

Ah, now they changed the API docs:  
From “when calling gpt-4-vision-preview or gpt-3.5-turbo,”  
to: "“when calling gpt-4-1106-preview or gpt-3.5-turbo-1106,”  
“you can set response\_format to { type: “json\_object” } to enable JSON mode.”  
So, gpt-4-vision-preview isn’t supposed to support JSON mode.

---

<div class="post-metadata">

**Author:** ![bleugreen](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bleugreen](https://community.openai.com/u/bleugreen)\
**Post date:** [November 8, 2023, 2:37am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/44 "2023-11-08T02:37:25Z")

</div>

It would be cool if instead of `json_object`, we could use the same json schema as functions. Then, the presence of “response\_format” would trigger json mode and the content could be used as an automatic system prompt. That way the Assistant system prompts are cleaner, and we wouldn’t have to use awkward function calls to specify reliable output formats.

---

<div class="post-metadata">

**Author:** ![thanhnamitit](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@thanhnamitit](https://community.openai.com/u/thanhnamitit)\
**Post date:** [November 8, 2023, 9:52am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/45 "2023-11-08T09:52:56Z")

</div>

```auto
def chat():
    completion = openai.chat.completions.create(
        model="gpt-4-1106-preview",
        response_format={"type": "json_object"},
        messages=[
            {"role": "user", "content": "translate this message to vietnamese, thailand, lao, cambodia: Hello, nice to meet you, reply in json object with key is the language code"}
        ]
    )

    print(completion.choices[0].message)

```

this function work well for me

response:

```auto
      {
        "vi": "Xin chào, rất vui được gặp bạn",
        "th": "สวัสดีครับ/ค่ะ ยินดีที่ได้พบคุณ",
        "lo": "ສະບາຍດີ, ດີ​ໃຈ​ທີ່​ໄດ້​ຮູ້​ຈັກ",
        "km": "ជំរាបសួរ, រីករាយដែលបានជួបអ្នក"
      }

```

---

<div class="post-metadata">

**Author:** ![nesse.erik](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/nesse.erik/32/241641_2.png) [@nesse.erik](https://community.openai.com/u/nesse.erik)\
**Post date:** [November 9, 2023, 1:24pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/46 "2023-11-09T13:24:07Z")

</div>

My current workaround is chaining two APIs together. First I fire off a request to the `gpt-4-vision-preview` with clear instructions on which details to extract in the image, but I don’t specify a JSON structure. Using that response I make a new request to the `gpt-4-1106-preview` with the `"response_format": { "type": "json_object" }` and a clearly defined JSON structure in the `"role": "system"`. It isn’t bulletproof, some things _might_ be lost between interpretations but it hasn’t failed me yet. I get parsable JSON with the expected content.

---

<div class="post-metadata">

**Author:** ![callum.holt](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/callum.holt/32/148858_2.png) [@callum.holt](https://community.openai.com/u/callum.holt)\
**Post date:** [November 9, 2023, 7:44pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/47 "2023-11-09T19:44:19Z")

</div>

This thread really helped me get my ruby code working, so i thought i would contribute my code incase it helps others.

```auto
url = URI('https://api.openai.com/v1/chat/completions')
    api_key = ENV['OPEN_AI_KEY']
    
    
    puts "API Key: #{api_key}"

    headers = {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{api_key}",
    }
    
    # Define the message parameters
    messages = [
      {
        "role": "system",
        "content": "Return the output as JSON for a workout plan for a user based on the details provided by user......"
      },
      {
        "role": "user",
        "content": "Please create a 30 day workout designed for the user details below..."
      },
    ]

    
  
    request = Net::HTTP::Post.new(url)

    puts "request sent"
    
    request.body = {
      model: 'gpt-4-1106-preview',
      messages: messages,
      temperature: 1,
      max_tokens: 3000,
      top_p: 1,
      frequency_penalty: 0,
      presence_penalty: 0,
      response_format: { "type":"json_object" }

      }.to_json
    puts "this is the request: #{request.body}"
    request.initialize_http_header(headers)

    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.read_timeout = nil
    
    # Send the request
    begin
      response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
        http.request(request)
      end
    rescue StandardError => e
      puts "Error during HTTP request: #{e.message}"
    end
    
   
    # Print the response

    if response
      puts response.inspect
      if response.is_a?(Net::HTTPSuccess)
        # Process response
        puts response
      else
        puts "Error: #{response.code} - #{response.message}"
      end
    else
      puts "No response received"
    end

```

---

<div class="post-metadata">

**Author:** ![akshat.agarwal](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/akshat.agarwal/32/146186_2.png) [@akshat.agarwal](https://community.openai.com/u/akshat.agarwal)\
**Post date:** [November 10, 2023, 5:15am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/48 "2023-11-10T05:15:02Z")

</div>

Can I fine tune a model with “assitant” prompt as JSON Object, if yes, how?

---

<div class="post-metadata">

**Author:** ![tytung2020](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/tytung2020/32/62918_2.png) [@tytung2020](https://community.openai.com/u/tytung2020)\
**Post date:** [November 10, 2023, 6:00am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/49 "2023-11-10T06:00:23Z")

</div>

you just need to mention json in some form in your prompt, like: “format your output in a json format.”

---

<div class="post-metadata">

**Author:** ![andkulak](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/andkulak/32/245080_2.png) [@andkulak](https://community.openai.com/u/andkulak)\
**Post date:** [November 11, 2023, 8:16pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/50 "2023-11-11T20:16:21Z")

</div>

Yes this seems like the most reliable option until they add the json\_object into image api. thanks for posting your thoughts here which confirm mine!

---

<div class="post-metadata">

**Author:** ![rokbenko](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/rokbenko/32/477304_2.png) [@rokbenko](https://community.openai.com/u/rokbenko)\
**Post date:** [November 12, 2023, 4:26pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/51 "2023-11-12T16:26:40Z")

</div>

I just released a new [YouTube tutorial on how to get a response in JSON format](https://www.youtube.com/watch?v=o4q2qsGKVkE).

Also, see my [GitHub repository](https://github.com/rokbenko/ai-playground) with [full code](https://github.com/rokbenko/ai-playground/tree/main/openai-tutorials/1-Get_response_in_JSON_format) for the tutorial.

**Python:**

```auto
import os
from openai import OpenAI
client = OpenAI()
OpenAI.api_key = os.getenv('OPENAI_API_KEY')

completion = client.chat.completions.create(
  model="gpt-4-1106-preview",
  messages=[
    {"role": "system", "content": "You are a helpful assistant. Your response should be in JSON format."},
    {"role": "user", "content": "Hello!"}
  ],
  response_format={"type": "json_object"}
)

print(completion.choices[0].message.content)

```

**Node:**

```auto
const OpenAI = require("openai");
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: "gpt-4-1106-preview",
    messages: [
      {
        role: "system",
        content:
          "You are a helpful assistant. Your response should be in JSON format.",
      },
      { role: "user", content: "Hello!" },
    ],
    response_format: { type: "json_object" },
  });

  console.log(completion.choices[0].message.content);

```

---

<div class="post-metadata">

**Author:** ![patrickpy](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/patrickpy/32/243475_2.png) [@patrickpy](https://community.openai.com/u/patrickpy)\
**Post date:** [November 13, 2023, 1:34pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/52 "2023-11-13T13:34:05Z")

</div>

Hey, I’m wondering if can I use the json mode with a finetuned model?

---

<div class="post-metadata">

**Author:** ![splattne](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/splattne/32/145824_2.png) [@splattne](https://community.openai.com/u/splattne)\
**Post date:** [November 16, 2023, 12:20pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/54 "2023-11-16T12:20:05Z")

</div>

I’m trying to use the `response_format` in C# (Semantic Kernel library). This doesn’t work for me:

```auto
var requestSettings = new AIRequestSettings
{
	ExtensionData = {
		{ "max_tokens", completionMaxTokenCount },
		{ "temperature", (double)temperature },
		{ "top_p", topP },
		{
			"response_format", new Dictionary<string, object>
			{
				{ "type", "json_object" }
			}
		}
	}
};

var textClassificationFunction = kernel.CreateSemanticFunction(prompt);
var result = await textClassificationFunction.InvokeAsync(text, kernel, requestSettings: requestSettings);

```

I’m using GPT-4-Turbo (1106 preview)

Any idea why this doesn’t work?

---

<div class="post-metadata">

**Author:** ![kjordan](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/kjordan/32/157616_2.png) [@kjordan](https://community.openai.com/u/kjordan)\
**Post date:** [November 16, 2023, 12:26pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/55 "2023-11-16T12:26:55Z")

</div>

@splattne Did you get any error? Also, make sure to have the word ‘JSON’ in your system prompt.

---

<div class="post-metadata">

**Author:** ![splattne](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/splattne/32/145824_2.png) [@splattne](https://community.openai.com/u/splattne)\
**Post date:** [November 16, 2023, 12:45pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/56 "2023-11-16T12:45:04Z")

</div>

@kjordan No error, the response is JSON, but delimited by backtick backtick backtick JSON.

I have the word JSON in the prompt, even with an example at the end of prompt.

---

<div class="post-metadata">

**Author:** ![dariotm](https://avatars.discourse-cdn.com/v4/letter/d/ba9def/32.png) [@dariotm](https://community.openai.com/u/dariotm)\
**Post date:** [November 16, 2023, 1:58pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/57 "2023-11-16T13:58:49Z")

</div>

Is there a way to have an assistant return a JSON using the `response_format` parameter? I understand how to do it with a chat completion GPT (parameter in the client.chat.completions.create method) but the same parameter is not available in the `client.beta.assistants.create` method.  
How can I do that?

---

<div class="post-metadata">

**Author:** ![Foxalabs](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/foxalabs/32/738355_2.png) [@Foxalabs](https://community.openai.com/u/Foxalabs)\
**Post date:** [November 16, 2023, 3:30pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/58 "2023-11-16T15:30:23Z")

</div>

Hi and welcome to the Developer Forum!

I do not think this is a feature of the assistants system at the moment.

---

<div class="post-metadata">

**Author:** ![anythxwdo](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@anythxwdo](https://community.openai.com/u/anythxwdo)\
**Post date:** [November 25, 2023, 2:30pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/59 "2023-11-25T14:30:25Z")

</div>

could you take a look at the following thread about the same issue with the BETA Assistants?  
[new `response_format` field not permitted with `gpt-4-vision-preview` · Issue #469 · openai/openai-node] ( github dot com/openai/openai-node/issues/469 )

---

<div class="post-metadata">

**Author:** ![przemek.smyrdek](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/przemek.smyrdek/32/184323_2.png) [@przemek.smyrdek](https://community.openai.com/u/przemek.smyrdek)\
**Post date:** [November 30, 2023, 5:38pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/60 "2023-11-30T17:38:15Z")

</div>

Guys, may I ask what’s the impact of json\_mode on techniques such as c-o-t and similar? Is this added as another layer on top of already generated, context-rich prompts, or it actually reduces model’s “working memory”?

---

<div class="post-metadata">

**Author:** ![nkhatri](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@nkhatri](https://community.openai.com/u/nkhatri)\
**Post date:** [December 2, 2023, 4:24pm UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/61 "2023-12-02T16:24:16Z")

</div>

Thanks, this worked after looking for a resolution for hours!

---

<div class="post-metadata">

**Author:** ![sarthak.srivastava](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sarthak.srivastava/32/19987_2.png) [@sarthak.srivastava](https://community.openai.com/u/sarthak.srivastava)\
**Post date:** [December 13, 2023, 6:18am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/62 "2023-12-13T06:18:34Z")

</div>

how can i add more parameter like i have nested list and arrays if i am using jsut openai json mode api,  
will it not hallucinate anywhere, it will alyws return json , like i instucted ?

---

<div class="post-metadata">

**Author:** ![RitikDutta](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/ritikdutta/32/273004_2.png) [@RitikDutta](https://community.openai.com/u/RitikDutta)\
**Post date:** [December 13, 2023, 8:15am UTC](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/63 "2023-12-13T08:15:18Z")

</div>

i got a good way doing that  
getting response as structured format with new assistant creation(not the chat completion)

first u need to create the usual model, any model work fine i think i am using gpt3.5.1106  
u can add custom instruction on running new thread as well

here’s a code example

```auto
assistant = client.beta.assistants.create(
    name="weather assistant",
    instructions=instruction,
    model="gpt-3.5-turbo-1106",
)

thread = client.beta.threads.create()

message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="hey"
)

run = client.beta.threads.runs.create(
  thread_id=thread.id,
  assistant_id=assistant.id,
  instructions=instruction2
)

run = client.beta.threads.runs.retrieve(
  thread_id=thread.id,
  run_id=run.id
)

# run it after 5 seconds 
messages = client.beta.threads.messages.list(
  thread_id=thread.id
)

print(messages)

```

this is the basic code provided by openai  
in instruction1 u can add any instruction u want the assistant to behave  
in instruction 2 u can add this prompt

instruction2 = "you are a fucction that only respond as json and only return 3 variable in json, u cannot say anything else, u cannot chat with user directly, don’t say anything other that the json text requested

make that text in structured json format exactly like this

{‘weather’: ‘weather in the text’, ‘city’: ‘city in the text’, ‘suggestion’ : ‘any suggestion given by assistant’}  
"

please give feedback if its working in ur case as well  
it’s working in every case i have tested in

[Previous page](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890.md?page=2)

[Next page](https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890.md?page=4)
