GPT-4 in json mode intermittently returning answer twice

Hi,

we are seeing an interesting issue where gpt-4 in json modes seems to return same json twice (separated by a newline). This is obviously not a valid json, so the json parser fails on the response

Here is the code:

import os
from openai import OpenAI
import json
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

with open('tools.json', 'r') as f:
    tools = json.load(f)
with open('messages.json', 'r') as f:
    messages = json.load(f)

response = client.chat.completions.create(
    model="gpt-4-0125-preview",
    response_format={
        "type": "json_object",
    },
    temperature=0,
    top_p=0.1,
    tools=tools,
    messages=messages
)
data = response.choices[0].message.content
data

And here is the response (notice the duplication):

'{"question":{"question":"Oh no, TV troubles are the worst! 😟 Let\'s find you a new one. What size was your old TV?","range":["24","85","inches"]}}\n{"question":{"question":"Oh no, TV troubles are the worst! 😟 Let\'s find you a new one. What size was your old TV?","range":["24","85","inches"]}}'

The prompt and tools sections are fairly long, happy to add them here if it is relevant. However, I expect json_mode to guarantee a valid json, regardless of prompt. Issue is not deterministic - it happens in like 50% of the calls.

Any ideas what could cause this?

Thanks!

The same is happening with me. My project is based on the gpt-3.5-turbo-1106 model.

I see this behaviour as well, through the Assistants API. Model independent.