{ "line": "Unexpected error: 'ChatCompletionMessage' object is not subscriptable" }

 try:
        completion = client.chat.completions.create(
            model="gpt-4o-mini",  # Or "gpt-4" if your key supports it
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt},
            ],
            max_tokens=100,
            temperature=0.8
        )
        
        print(completion.choices[0].message["content"].strip())
    except OpenAIError as e:
        return f"OpenAI API error: {e}"
    except Exception as e:
        return f"Unexpected error: {e}"

i dont see where the problem is i looked at someone else having the same problem but using print didnt work i dont really need anyone to tell me exactly how to do it but whether or now the problem would lye in here and what call i would use if thats the case or what print i would use im setting it up for of course a response to the users input… any help?

``print(completion.choices[0].message)`

when i changed it to this the error i got just said null

i figured it out with this get_completion(prompt, client) # call your function
but now it prints to the terminal nonstop im assuming because i still had the print command in there lmbo

You should change:

print(completion.choices[0].message["content"].strip())

to:

print(completion.choices[0].message.content.strip())
1 Like

it worked but now it says {
“line”: null
}

ok so its printing to the terminal nonstop is that what its supposed to do

return completion.choices[0].message.content.strip()

thank you for your help i just used return instead of print and it worked perfectly

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.