AttributeError in my code #174977
-
Bodywhen i run the code to generate completion this error message appear what can i do to fix this problem? venv) @ossama-122 ➜ /workspaces/generative-ai-for-beginners (main) $ /workspaces/generative-ai-for-beginners/.venv/bin/python /workspaces/generative-ai-for-beginners/06-text-generation-apps/python/oai-app.py Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
The error happens because the response you’re getting from the API is a plain string, not the full object that contains .choices. That usually means you are using the wrong method or not parsing the response correctly. In the latest OpenAI Python library, you should use .chat.completions.create() instead of .Completion.create(). Also, the response is a dictionary-like object, so you can access it safely like this: client = OpenAI() completion = client.chat.completions.create( print(completion.choices[0].message.content) Key points to fix your error: Make sure you’re using the new API client (chat.completions.create). Don’t treat the response as a string — it’s an object with choices. If you still see a string, check if you accidentally wrapped the response in str() somewhere. |
Beta Was this translation helpful? Give feedback.
-
|
You are just getting a string back. switch to client.chat.completions.create(...) and print completion.choices[0].message.content |
Beta Was this translation helpful? Give feedback.
-
|
The error occurs because the response you’re getting from the API is just a plain string, not the full object that contains choices. This usually means you’re either using the wrong method or not parsing the response correctly. In the latest OpenAI Python library, you should use .chat.completions.create() instead of.Completion.create(). Also, the returned response is a dictionary-like object, so you can safely access it like this: from openai import OpenAI client = OpenAI() completion = client.chat.completions.create( print(completion.choices[0].message.content) To fix your error: |
Beta Was this translation helpful? Give feedback.
The error occurs because the response you’re getting from the API is just a plain string, not the full object that contains choices. This usually means you’re either using the wrong method or not parsing the response correctly.
In the latest OpenAI Python library, you should use .chat.completions.create() instead of.Completion.create(). Also, the returned response is a dictionary-like object, so you can safely access it like this:
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Write a short verse explanation from https://www.alim.org/quran/."}
]
)
print(completion.choices[0].message.cont…