Chat Completions Response Needs To Be Updated in Documenation

Chat Completions Documentation Highlight

It should be updated for Python SDK v1.2:

response.choices[0].message.content

Upgrading to Python SDK v1.2 and doing:

response['choices'][0]['message']['content']

results in:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-65-3b1faf4f37d1> in <cell line: 99>()
    158         
    159         # Extracting the response content
--> 160         response_content = response['choices'][0]['message']['content']
    161 
    162         

TypeError: 'ChatCompletion' object is not subscriptable
1 Like

Unless you still want the original.

from openai import OpenAI
client = OpenAI()
system = [{"role": "system",
           "content": """You are an administrative assistant."""}]
user = [{"role": "user", "content": "Happy assistant day."}]
model = client.chat.completions.create(
  messages = system + user,
  model="gpt-3.5-turbo")
response = model.model_dump()
print(response['choices'][0]['message']['content'])

“Thank you! I appreciate the recognition.”

I don’t think that’s the intention of the documentation.

The documentation says

The assistant’s reply can be extracted with:

and it is referring to the code above it:

from openai import OpenAI
client = OpenAI()

response = 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?"}
  ]
)

Running the example code, it is not the case that the assistant’s reply can be extracted with:

response['choices'][0]['message']['content']

See example Colab Notebook:

colab[dot]research[dot]google[dot]com/drive/1oVxNM-tKL7UUULRpPG6NNDUEGhAuhzx-?usp=sharing

2 Likes