I’m getting an error when using the OpenAI PHP client with Laravel. The problem happens when I make a request to the GPT-3.5-turbo model and expect a list response back.
Here’s the error message I’m seeing:
WARNING: Undefined array key "choices" in
vendor/openaiphp/client/src/Responses/Completions/CreateResponse.php on line 45
TypeError: array_map(): Argument #2 ($array) must be of type array, null given
This is the test code I’m running:
use OpenAI\Laravel\Facades\OpenAI;
$response = OpenAI::completions()->create([
'model' => 'gpt-3.5-turbo',
'prompt' => 'Name 5 biggest companies worldwide',
]);
The error seems to indicate that the response structure is missing the expected “choices” key. Has anyone encountered this issue before? Any ideas on how to resolve this?
You’re hitting a common issue with the OpenAI PHP client. GPT-3.5-turbo is built for chat interactions, not the old completions format. Use the chat() method instead of completions(). Structure your request with a messages array and put your prompt as a user message. I ran into this same problem recently - switching methods fixed the weird response structure issues.
This happens because the OpenAI PHP package expects different response formats for each endpoint. When you call completions() with GPT-3.5-turbo, you get a chat-formatted response that breaks the completions parser. The completions endpoint was built for older models like text-davinci-003. I hit this exact TypeError last month during a migration. Fix is simple - swap completions() for chat() and structure your prompt as a message array with role and content keys. That way the response matches what the PHP client expects.
totally agree, using the chat method is the way to go! that completions thing is really outdated for gpt-3.5-turbo. switching to OpenAI::chat()->create() with the right message format helped me out too when i faced that issue.