Not working with any API-Key?

Hello - i am using a simple assistant code - see attached

For some reason this code is not working with any API-Key.
(i have API-keys from different customers - with one it is working without problems - but with the other API-key it seems that no result is provided - but without any error-message or anything)

This is the output with the API-Key which is NOT working:

  File "D:\DEV\Fiverr2024\TRY\larssegelke\checkHTML2.py", line 62, in <module>
    resultAnswer = results.data[0].content[0].text.value
                   ^^^^^^^^^^^^
  File "D:\DEV\.venv\openaiplus\Lib\site-packages\pydantic\main.py", line 856, in __getattr__
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'Message' object has no attribute 'data'
(openaiplus)

Why ist that not working with any API-Key?
What is the problem with this API-Key?

import os
import sys
from dotenv import load_dotenv
from openai import OpenAI

path = os.path.abspath(os.path.dirname(sys.argv[0])) 
fn = os.path.join(path, ".env")
load_dotenv(fn)
CHATGPT_API_KEY = os.environ.get("CHATGPT_API_KEY")
client = OpenAI(api_key = CHATGPT_API_KEY)

fn = os.path.join(path, "workText.txt")
vector_store = client.beta.vector_stores.create(name="HTML-File")
file_paths = [fn]
file_streams = [open(path, "rb") for path in file_paths]
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
  vector_store_id=vector_store.id, files=file_streams
)
print(file_batch.status)
print(file_batch.file_counts)
# exit()

print(f"Preparing assistant")
assistant = client.beta.assistants.create(
  name="HTML Analyse Assistant",
  instructions="You are a machine learning researcher, answer questions about the provided text-file",
  model="gpt-4o",
  tools=[{"type": "file_search"}],
)
assistant = client.beta.assistants.update(
  assistant_id=assistant.id,
  tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)

question = """
What is the name of the location?
"""

print(f"Preparing thread")
thread = client.beta.threads.create()
print(f"Preparing question")
results = client.beta.threads.messages.create(
  thread_id = thread.id,
  role = "user",
  content = question
)
print(f"Running for answer")
run = client.beta.threads.runs.create (
  thread_id = thread.id,
  assistant_id = assistant.id
)
while run.status not in ["completed", "failed"]:
  run = client.beta.threads.runs.retrieve (
    thread_id = thread.id,
    run_id = run.id
  )
if run.status == "completed":
  results = client.beta.threads.messages.list(
      thread_id=thread.id
  )

resultAnswer = results.data[0].content[0].text.value
fn = os.path.join(path, "result.txt")
# with open(fn, "w", encoding="utf-8", errors="ignore") as f:
#   lines = f.writelines(resultAnswer)
with open(fn, "w", encoding="utf-8", errors="ignore") as f:
  lines = f.writelines(str(results))

There has to be money in the account to use the API. Most likely the problem is no funds have been added.

I have uploaded 10usd and it still not working

This issue can arise when run.status is “failed”.

while run.status not in ["completed", "failed"]:

In this loop, the process continues as long as the status is neither “completed” nor “failed”.

This means the loop will exit when the status is either “completed” or “failed”.
 

However, if run.status is “failed”, the following condition:

if run.status == "completed":

is not met, so the code inside the block is not executed, and yet the following assignment is still attempted:

resultAnswer = results.data[0].content[0].text.value

Because run.status is “failed”, this code:

results = client.beta.threads.messages.list(
    thread_id=thread.id
)

is never executed.
As a result, “results” remains as the message object assigned earlier:

results = client.beta.threads.messages.create(
    thread_id = thread.id,
    role = "user",
    content = question
)

Since the message object does not have a “data” attribute, this leads to the validation error mentioned above.

 

To resolve this, you need to investigate why run.status is “failed”.

Additionally, it’s important to ensure that no further processing occurs in the event of a “failed” status.

I hope this provides some hint toward resolving the issue :slightly_smiling_face:

1 Like

Your are completely right - but how should i investigate why the status fails?

With the one API-Key its working fine with status complete.
And with the other API-Key its not working with status complete.
100% identical code-base

How can i investigate why the API-Key ist not working?
Both accounts are charged up with 10usd.
Is there anything else i have to check in my dashboard with the not working api-key?

It might just be something I’m unaware of, but it seems a bit strange that the behavior of the code differs solely based on the API key.

What you can find out from the dashboard is whether the permission settings for the API keys are the same or not, but I’m not sure if that would directly cause changes in the behavior of the assistant API.

Still, it might be helpful to check if the permissions for both API keys are identical.

On the other hand, when investigating why run.status failed, you can obtain the error code and a human-readable description of the error by adding the following after:

run = client.beta.threads.runs.create (
  thread_id = thread.id,
  assistant_id = assistant.id
)

you can add the following:

print(run.last_error.code)
print(run.last_error.message)

API Document object-last_error

Without a way to reproduce the issue, such as by providing the exact file or method you used, it’s difficult for us to speculate on the cause.

However, by investigating the error using the method above, you may be able to identify the cause from among the many possibilities, whether it’s related to the file upload process, the code interpreter, or something else.

This is far from a complete solution, but I hope it helps in some way :slightly_smiling_face:

Thx - i think that was helpful - when i do the print statements i get this error-info:

rate_limit_exceeded
Request too large for gpt-4o in organization org-uethKLI2dw4Lq0j8f8KFjaBp on tokens per min (TPM): Limit 30000, Requested 30480. The input or output tokens must be reduced in order to run successfully. Visit https://platform.openai.com/account/rate-limits to learn more.

Is there any reason why obviously this rate-limit throws an error with one API-key - but not with another one (with the exact same code and input-files of course)?

Is there any way to see the requested tokens per min as part of the initial code above?

Hello - i think i found a workaround -
According to the rate limits
https://platform.openai.com/settings/organization/limits
i changed the model to “gpt-4o-mini” which has a 200.000 TPM limit (instead of 30.000 TPM as it is in the “gpt-4o” model.

I still don´t undertand why this is in the previous example wit “gpt-4o” working with one API-key and with another one not.
But at least i think i have a workaround.