openai-php / openai-php/client

[Bug]: Passing invalid parameters to gemini throws Undefined array key "choices" /www/htdocs/vendor/openai-php/client/src/Responses/Chat/CreateResponse.php 54

Open
#692 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
5.8k
Forks
679
Avg merge
19m
Merged PRs (30d)
2

Description

Description

While testing gemini I noticed that passing invalid chat parameters doesn't throw the appropriate exception. It throws:

Undefined array key "choices" /www/htdocs/vendor/openai-php/client/src/Responses/Chat/CreateResponse.php 54

The reason the error isn't caught is because the error is returned as:

 array (
  0 =>
  array (
    'error' =>
    array (
      'code' => 400,
      'message' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
      'status' => 'INVALID_ARGUMENT',
      'details' =>
      array (
        0 =>
        array (
          '@type' => 'type.googleapis.com/google.rpc.BadRequest',
          'fieldViolations' =>
          array (
            0 =>
            array (
              'description' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
            ),
          ),
        ),
      ),
    ),
  ),
)

The HttpTransporter::throwIfJsonError() only checks for $response['error'], but in this case it's $response[0]['error'].

Steps To Reproduce
 $client  = \OpenAI::factory()
    ->withApiKey('YOUR-KEY')
    ->withBaseUri('https://generativelanguage.googleapis.com/v1beta/openai')
    ->make();
$result = $client->chat()->create($params = [
    'model' => 'gemini-2.5-flash',
    'messages' => [
        ['role' => 'system', 'content' => 'You can use tools if needed.'],
        ['role' => 'user', 'content' => 'Hello you.'],
    ],
    'ddd' => 'auto',
  ]);
OpenAI PHP Client Version

v0.16.1

PHP Version

8.2.5

Notes

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in HttpTransporter::throwIfJsonError() and inspect how the response is handled before CreateResponse.php accesses the choices key. Reproduce the invalid-parameter request against the Gemini base URI, then verify that the nested error response raises the appropriate exception instead of an undefined array-key error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.