Assistant instructions are discarded

Hi everyone!

I am having trouble with the assistants. It works fine in playground and gives answers as expected but when I access it through my code it loses it’s “personality”. E.g I made a test assistant that only had the instructions that it’s name is bob. When asked in playground who it is it replies correctly bob but with my code it gives a generic reply like I am an AI assistant here to help … I have tripple checked the id. It is like this with all my assistants.

The code I use is just standard python and looks roughly like this:

client = OpenAI(api_key=key, default_headers={"OpenAI-Beta": "assistants=v2"})
assistant_id = os.environ.get("ASSISTANT_ID")
thread = client.beta.threads.create()
client.beta.threads.messages.create(
		thread_id=thread_id,
		role="user",
		content="Who are you?",
	)
run = client.beta.threads.runs.create_and_poll(
		thread_id=thread_id,
		assistant_id=assistant_id,
		instructions=instructions
	)
all_messages = client.beta.threads.messages.list(
		thread_id=thread_id
	)
	response = all_messages.data[0].content[0].text.value

Thanks for any help <3 It might be something stupid. I am going crazy with this

assistant_id = os.environ.get(“ASSISTANT_ID”)

is the culprit

1 Like

Here’s a step-by-step guide to troubleshoot and correct the issue:

  1. Verify API Headers and Versions:
    Make sure the correct API version header is being used. You’re correctly using assistants=v2.

  2. Check Assistant Creation:
    Ensure that the assistant is created with the correct instructions and those instructions are referenced properly. Here is a sample code snippet for creating an assistant:

    from openai import OpenAI
    import os
    
    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
    
    assistant = client.beta.assistants.create(
        name="Test Assistant",
        instructions="Your name is Bob. You should always introduce yourself as Bob.",
        model="gpt-4-turbo"
    )
    
  3. Correctly Create and Use Threads and Runs:
    Ensure that the thread and run are created correctly and that the assistant’s instructions are applied. Here’s an updated version of your code:

    from openai import OpenAI
    import os
    import time
    
    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"), default_headers={"OpenAI-Beta": "assistants=v2"})
    assistant_id = os.environ.get("ASSISTANT_ID") 
    # assistant_id = "ASSISTANT_ID"   # or like this, maybe you didn't set it well, test both versions
    
    
    # Create a new thread
    thread = client.beta.threads.create()
    
    # Add a user message to the thread
    client.beta.threads.messages.create(
        thread_id=thread.id,
        role="user",
        content="Who are you?"
    )
    
    # Create and poll a run to get the assistant's response
    run = client.beta.threads.runs.create(
        thread_id=thread.id,
        assistant_id=assistant_id
    )
    
    def wait_on_run(run, thread):
        while run.status == "queued" or run.status == "in_progress":
            run = client.beta.threads.runs.retrieve(
                thread_id=thread.id,
                run_id=run.id,
            )
            time.sleep(0.5)
        return run
    
    run = wait_on_run(run, thread)
    
    # Retrieve the messages from the thread
    messages = client.beta.threads.messages.list(thread_id=thread.id)
    response = messages.data[-1].content[0].text.value
    
    print(response)  # Should print: "My name is Bob."
    
  4. Check Assistant ID and Instructions:
    Ensure the assistant ID is correctly set in the environment variable ASSISTANT_ID and that the instructions are being referenced correctly in the API calls.

  5. Debugging Steps:

    • Verify that the assistant is correctly created by checking the assistant’s details.
    • Print the assistant’s ID to ensure it matches the one used in the environment variable.
    • Check the content of the messages retrieved to see if the assistant’s instructions are reflected in the responses.

By following these steps, you should be able to ensure that the assistant retains its personality as defined in the instructions when accessed through your code. If issues persist, further debugging of each step in the process will be necessary to pinpoint where the configuration might be going wrong.

I hope this helps! Let me know if you need any further assistance.


1 Like

Thanks for the detailed answer! I sadly have already tried all these steps.
Here is some more information on what I have tired so far:

  • I know the correct assistant is referenced because function calling works
  • I know the instructions are setup correctly because in playground I get the expected results
  • I have tried created another assistant (both in code and with playground)
  • When asking about a file connected to the assistant I got the following response (shortened): It appears there is a technical issue preventing me from accessing the files to provide information on (topic of file).

I am not sure how you set the assistant id, try to copy paste and put it like this, maybe you didn’t set the assistant well.

assistant_id = "asst_qdEud4WDBfghO08SbpVI2IYI"

Right now without more information or some screenshots to see the errors, I cant help.

The assistant API is in beta, there are no guarantees that it will work how you expect outside of the playground.

  • I think I can help with this. Assistants work great outside (I have conducted many tests).
  • I will make my code open-source next week, and I will create tutorials too.
  • I hope this will make everything simpler for building Assistants with APIs and working with data in the Cloud or on a Personal PC.

Check platform.openai.com. You should be able to see the details of every thread. It will show you what the id of the assistant used and also the instructions etc. Found in the left side menu. You might have to enable the view in settings.