From 273cc375264019f9fa3a86b0034a5a131f2c57a5 Mon Sep 17 00:00:00 2001 From: Alexandre Marcoux Date: Wed, 1 Oct 2025 19:55:27 -0400 Subject: [PATCH 1/2] Fix #692 [Bug]: Passing invalid parameters to gemini throws Undefined array key "choices" /www/htdocs/vendor/openai-php/client/src/Responses/Chat/CreateResponse.php 54 Change-Id: Ibbf64cc554ef68844fd8338583f3fc8ec5fde413 --- src/Transporters/HttpTransporter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index 0b1d8af6..adbd7be7 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -172,11 +172,13 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn } try { - /** @var array{error?: string|array{message: string|array, type: string, code: string}} $data */ + /** @var (array{error?: string|array{message: string|array, type: string, code: string}})|(array{0?: array{error?: string|array{message: string|array, code: string}}}) $data */ $data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR); if (isset($data['error'])) { throw new ErrorException($data['error'], $response); + } elseif (isset($data[0]['error'])) { + throw new ErrorException($data[0]['error'], $response); } } catch (JsonException $jsonException) { throw new UnserializableResponse($jsonException, $response); From 6591d360671904b202fd6083beaa981c17c610f5 Mon Sep 17 00:00:00 2001 From: Alexandre Marcoux Date: Fri, 3 Oct 2025 18:13:31 -0400 Subject: [PATCH 2/2] Fix #692 [Bug]: Passing invalid parameters to gemini throws Undefined array key "choices" /www/htdocs/vendor/openai-php/client/src/Responses/Chat/CreateResponse.php 54 Add tests for gemini with invalid parameter. Set ErrorException type to return error status for invalid gemini parameter. Change-Id: I076016d0470eb3060d3f972702e4e3701b52207d --- src/Transporters/HttpTransporter.php | 5 ++- tests/Transporters/HttpTransporter.php | 50 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index adbd7be7..880a85df 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -172,12 +172,15 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn } try { - /** @var (array{error?: string|array{message: string|array, type: string, code: string}})|(array{0?: array{error?: string|array{message: string|array, code: string}}}) $data */ + /** @var (array{error?: string|array{message: string|array, type: string, code: string}})|(array{0?: array{error?: string|array{message: string|array, code: string, status: string, type?: string}}}) $data */ $data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR); if (isset($data['error'])) { throw new ErrorException($data['error'], $response); } elseif (isset($data[0]['error'])) { + if (! isset($data[0]['error']['type']) && isset($data[0]['error']['status'])) { + $data[0]['error']['type'] = $data[0]['error']['status']; + } throw new ErrorException($data[0]['error'], $response); } } catch (JsonException $jsonException) { diff --git a/tests/Transporters/HttpTransporter.php b/tests/Transporters/HttpTransporter.php index 813b9e0e..eed44907 100644 --- a/tests/Transporters/HttpTransporter.php +++ b/tests/Transporters/HttpTransporter.php @@ -624,3 +624,53 @@ $this->http->requestObject($payload); }); + +test('error for gemini with invalid parameter', function (string $requestMethod) { + $payload = Payload::create('completions', [ + 'model' => 'gemini-2.5-flash', + 'messages' => [ + [ + 'role' => 'system', + 'content' => 'You can use tools if needed.', + ], + [ + 'role' => 'user', + 'content' => 'Yo', + ], + ], + 'ddd' => 'auto', + ]); + + $response = new Response(400, ['Content-Type' => 'application/json; charset=utf-8'], json_encode( + [[ + 'error' => [ + 'code' => 400, + 'message' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.', + 'status' => 'INVALID_ARGUMENT', + 'details' => [ + [ + '@type' => 'type.googleapis.com/google.rpc.BadRequest', + 'fieldViolations' => [ + [ + 'description' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.', + ], + ], + ], + ], + ], + ]] + )); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->andReturn($response); + + expect(fn () => $this->http->$requestMethod($payload)) + ->toThrow(function (ErrorException $e) { + expect($e->getMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.') + ->and($e->getErrorMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.') + ->and($e->getErrorCode())->toBe(400) + ->and($e->getErrorType())->toBe('INVALID_ARGUMENT'); + }); +})->with('request methods');