Search code examples
pythonjsonopenai-apipretty-print

How do I print each element in an openai ChatCompletion response as JSON on a separate line?


I'm making a simple call to OpenAI using Python asking about where a baseball game was played.

completion = openai.chat.completions.create(
  model="gpt-3.5-turbo",
  messages = [
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "Who won the world series in 2020?"},
  {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
  {"role": "user", "content": "Where was it played?"}
  ]
)

print (completion)

The output shows up like this:

ChatCompletion(id='chatcmpl-9UgP85B0gYBjEAiYMcF3Ryt9Y3fdZ', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='The 2020 World Series was played at Globe Life Field in Arlington, Texas.', role='assistant', function_call=None, tool_calls=None))], created=1717099870, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=17, prompt_tokens=53, total_tokens=70))

But I'd like it to show up like this:

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The 2020 World Series was played in Texas at Globe Life Field in Arlington.",
        "role": "assistant"
      },
      "logprobs": null
    }
  ],
  "created": 1677664795,
  "id": "chatcmpl-7QyqpwdfhqwajicIEznoc6Q47XAyW",
  "model": "gpt-3.5-turbo-0613",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 17,
    "prompt_tokens": 57,
    "total_tokens": 74
  }
}

FWIW, I'm using Python 3.12 and Windows Terminal.


Solution

  • Use .model_dump_json()

    completion = client.chat.completions.create(
      model="gpt-3.5-turbo",
      messages = [
      {"role": "system", "content": "You are a helpful assistant"},
      {"role": "user", "content": "Who won the world series in 2020?"},
      {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
      {"role": "user", "content": "Where was it played?"}
      ]
    )
    
    data = completion.model_dump_json()
    print(data)
    

    Output:

    {"id":"chatcmpl-9VGebGyeosYhcWW19F6f5B6lMFhpl","choices":[{"finish_reason":"stop","index":0,"logprobs":null,"message":{"content":"The World Series in 2020 was played at Globe Life Field in Arlington, Texas.","role":"assistant","function_call":null,"tool_calls":null}}],"created":1717239213,"model":"gpt-3.5-turbo-0125","object":"chat.completion","system_fingerprint":null,"usage":{"completion_tokens":18,"prompt_tokens":52,"total_tokens":70}}
    

    If you need to pretty print, you can just use the ident parameter:

    data = completion.model_dump_json(indent=3)
    print(data)
    

    Output:

    {
       "id": "chatcmpl-9VGphVuZLKh2QuqT9vSAUdWCxoifg",
       "choices": [
          {
             "finish_reason": "stop",
             "index": 0,
             "logprobs": null,
             "message": {
                "content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas, home of the Texas Rangers.",
                "role": "assistant",
                "function_call": null,
                "tool_calls": null
             }
          }
       ],
       "created": 1717239901,
       "model": "gpt-3.5-turbo-0125",
       "object": "chat.completion",
       "system_fingerprint": null,
       "usage": {
          "completion_tokens": 23,
          "prompt_tokens": 52,
          "total_tokens": 75
       }
    }