使用openai-php库调用OpenAI Assistant Run持续失败求助
使用openai-php库创建Run时100%失败排查求助
使用openai-php库创建Run时100%失败,本人PHP经验不足,现将测试脚本及运行结果提供如下:
测试脚本
<?php require '../../../dev/vendor/autoload.php'; $apiKey = getenv('ASSISTANT_API_KEY'); @ini_set('zlib.output_compression',0); @ini_set('implicit_flush',1); @ob_end_clean(); set_time_limit(0); ob_implicit_flush(1); $client = OpenAI::client($apiKey); echo "Creating thread...<br/>"; $response = $client->threads()->create([]); $array = $response->toArray(); $threadId = $response->id; echo "Thread created, id:" . $response->id . "<br/>"; echo "Adding message Hello World...<br/>"; $response = $client->threads()->messages()->create($threadId, [ 'role' => 'user', 'content' => 'Hello World!', ] ); echo "Message created, id:" . $response->id . "<br/>"; echo "Creating a run...<br/>"; $response = $client->threads()->runs()->create( threadId: $threadId, parameters: [ 'assistant_id' => 'asst_wYzcGyBRwvpcKY3G4O0fEM5A', ], ); echo "Run created, id:" . $response->id . "<br/>"; $runId = $response->id; echo "Waiting for run result...<br/>"; for ($i=0; $i <= 20; $i++) { $response = $client->threads()->runs()->retrieve( threadId: $threadId, runId: $runId, ); $runStatus = $response->status; if(strcmp($runStatus, "in_progress")==0){ echo "In progres... <br/>"; } if(strcmp($runStatus, "failed")==0){ echo "Failed... <br/>"; echo json_encode($response->toArray(), JSON_PRETTY_PRINT); echo "<br/>"; break; } if(strcmp($runStatus, "completed") ==0){ echo "In completed... <br/>"; break; } sleep(1); } echo "Run complete<br/>"; ?>
运行结果
Creating thread... Thread created, id:thread_ZvHJYWZjoYXf04CR6BJbw67C Adding message Hello World... Message created, id:msg_Z1Y6Ei4j9b5EKI48BtSzHvot Creating a run... Run created, id:run_jR2oc8y3wLqWS8jwfcizK70I Waiting for run result... In progres... Failed... { "id": "run_jR2oc8y3wLqWS8jwfcizK70I", "object": "thread.run", "created_at": 1710458772, "assistant_id": "asst_wYzcGyBRwvpcKY3G4O0fEM5A", "thread_id": "thread_ZvHJYWZjoYXf04CR6BJbw67C", "status": "failed", "started_at": 1710458772, "expires_at": null, "cancelled_at": null, "failed_at": 1710458773, "completed_at": null, "last_error": { "code": "server_error", "message": "Sorry, something went wrong." }, "model": "gpt-4-1106-preview", "instructions": "You're helping debugging why the api calls to you are not working. Please share everything , all technical details you know about the interaction and the communication aspects. ", "tools": [], "file_ids": [], "metadata": [], "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0 } } Run complete
通过API创建的thread和message,在platform.openai.com网站手动执行Run可正常完成,但脚本中的Run始终失败,错误码为server_error,提示“Sorry, something went wrong.”,恳请帮助排查问题。
排查建议
- 确认openai-php库版本:升级到最新稳定版,旧版本可能存在Assistant API的兼容性问题
- 检查Run创建参数:核对
threads()->runs()->create的参数结构是否符合库的要求,排查是否存在参数名拼写错误或隐性必填项遗漏 - 开启调试日志:初始化OpenAI客户端时开启调试模式,打印实际发送的HTTP请求头和内容,对比平台手动调用的请求差异
- 网络环境检查:确认服务器能正常访问OpenAI API域名,排除防火墙、代理导致的请求中断,可尝试用curl直接调用API测试连通性
- 账号状态验证:确认API_KEY对应的账号有使用Assistant API的权限,且账号额度充足,未触发限制策略
内容的提问来源于stack exchange,提问作者Harmen
相关产品推荐
相关产品推荐

