How do I use the new JSON mode?

import os
from openai import OpenAI
from google.colab import userdata

messages = [
  {"role": "system", "content": "List of months that have 30 days in json"},
]

client = OpenAI(api_key=userdata.get('OPENAI_API_KEY'))

response = completion = client.chat.completions.create(
  model="gpt-4-1106-preview",
  messages=messages,
  response_format= { "type":"json_object" }
)

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

You have to have the response format AND you have to put the word json in your prompt

2 Likes