JSON returning with ```json

Pretty sure this wasn’t happening before, but the response from Assistants API when asked to return JSON output, always prefixes it with:

"```json" and ends with "```". This a new bug?

This is markdown. It’s hypothesized that the model has been increasingly trained on it and is more likely to produce it, as (personal opinion) ChatGPT seems to be their main product now.

Personally, I ask the model to produce the JSON in markdown and just splice it by these markers

1 Like

As of Jan 13, JSON mode is not supported in assistants API. you can use a completiton API to parse the results as JSON.

Wait was JSON mode ever available for Assistants? I genuinely wouldn’t know, never used it besides for some initial testing

It was never available for assistants API. It’s only available for ChatCompletion.

json_data = json_data.strip('` \n')

if json_data.startswith('json'):
    json_data = json_data[4:]  # Remove the first 4 characters 'json'

This has been my quick-fix for this issue. Hasn’t failed yet!

1 Like

do not use other peoples’ quick fixes.

The quickest fix is to include in your prompt something along the lines of:

“Do not wrap the json codes in JSON markers”

everything has a simple solution. Use caps lock if you want to attach higher token values and a greater emphasis on the directive. It will almost never use the wrappers again.

edit: you can also say, “your entire response/output is going to consist of a single JSON object {}, and you will NOT wrap it within JSON md markers”

1 Like

This is the json header and footer markdown remove I use.

def clean_json_string(json_string):
    pattern = r'^```json\s*(.*?)\s*```$'
    cleaned_string = re.sub(pattern, r'\1', json_string, flags=re.DOTALL)
    return cleaned_string.strip()

Thanks and yeah that’s what I ended up doing.