Hi im kinda new i have a question about api response


i have a really long response like its respond in long straight, instead of json response like the picture here, i try to make response in json format like another i already use json mode but it only print out content in json format how can i make api response like that in the picture? thank you

Hello! Welcome to the forum :slight_smile:

If you look at the screenshot closely, the actual message is nested within. And to get the message to be a valid json, you can either prompt the model to do so and/or use json_mode.

Here’s an example in python.

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4-1106-preview",
    messages=[{"role": "system", "content": "You are a helpful assistant"},
                              {"role": "user", "content":  "who played in the world series in 2020? reply in json"]
    response_format={"type":"json_object"} # Point 1: requesting the model to return a valid json
)

output = response.choices[0].message.content # Point 2: accessing the message content

print(output)
2 Likes

thank you cyzgab for advice :innocent:

1 Like