After adding metadata to a thread returns empty metadata

with a function to create a thread and run that looks like this

def create_thread_and_run(assistant_id, message_content, user_id):
    # Retrieve assistant
    assistant = client.beta.assistants.retrieve(assistant_id)

    metadata = {
        "user_id": str(user_id),
        "assistant_id": str(assistant_id),
        "assistant_name": str(assistant.name),
    }
    # Create thread and run the assistant
    run = client.beta.threads.create_and_run(
        assistant_id=assistant_id,
        thread={
            "messages": [{"role": "user", "content": message_content}],
            "metadata": metadata,
        },
    )


    return run


def retrieve_thread(thread_id):
    thread = client.beta.threads.retrieve(thread_id)
    return thread

the expected output is as follows
if I call to the retrieve_thread function it should return the thread with metadata, but it returns a thread with empty metadata.

what am I doing wrong?

2 Likes

we faced same issue
according to https://platform.openai.com/docs/api-reference/runs/createThreadAndRun
there are separate metadata for Thread and Run
but after execution of request

{
    "assistant_id": "asst_i1dgizQUkSmUuzhaNtJKecHj",
    "thread": {
        "messages": [
            {
                "role": "user",
                "content": "Explain deep learning to a 5 year old."
            }
        ],
        "metadata": {
            "test-thread": "test-value"
        }
    },
    "metadata": {
        "test-run": "test-value"
    }
}

both Thread and Rub objects will contain same metadata that supposed to be in run:

{
    "id": "run_IWKYi3ht3t2CFRT3dZYJyJpI",
    "object": "thread.run",
    "created_at": 1712155259,
    "assistant_id": "asst_i1dgizQUkSmUuzhaNtJKecHj",
    "thread_id": "thread_E4ptppy2ZlRnyzdcBscaZIGS",
    "status": "completed",
 ....
    "metadata": {
        "test-run": "test-value"
    },
    "temperature": 1.0,
    "usage": {
        "prompt_tokens": 171,
        "completion_tokens": 38,
        "total_tokens": 209
    }
}
{
    "id": "thread_E4ptppy2ZlRnyzdcBscaZIGS",
    "object": "thread",
    "created_at": 1712155258,
    "metadata": {
        "test-run": "test-value"
    }
}