You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用OpenAI Assistants API连接线程报错:'Thread'对象不可下标访问

解决OpenAI Thread对象不可下标访问的错误

错误出在thread["assistant"]这行代码——调用client.beta.threads.retrieve()返回的是Thread实例对象,不是字典,不能用下标[]访问属性,必须用点.语法访问对象属性。另外,Thread对象中存储助理ID的属性是assistant_id,不是assistant。

修改后的代码如下:

def connect_helmsman_gpt():
    config = load_config()
    api_key = config.get("api_key")
    thread_id = config.get("thread_id")
    if thread_id is None:
        print("No thread ID found in config")
        return None, None, None
    
    try:
        client = OpenAI(api_key=api_key)
        thread = client.beta.threads.retrieve(thread_id)
        # 修正属性访问方式和属性名
        assistant = client.beta.assistants.retrieve(thread.assistant_id)
        return assistant, client, thread
    except Exception as e:
        print(f"Failed to connect to the GPT thread: {e}")
        return None, None, None

内容的提问来源于stack exchange,提问作者Jack D'Angelo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:23:27