Skip to content

Commit 6591d36

Browse files
committed
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
1 parent 18d7bb3 commit 6591d36

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/Transporters/HttpTransporter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,15 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
172172
}
173173

174174
try {
175-
/** @var (array{error?: string|array{message: string|array<int, string>, type: string, code: string}})|(array{0?: array{error?: string|array{message: string|array<int, string>, code: string}}}) $data */
175+
/** @var (array{error?: string|array{message: string|array<int, string>, type: string, code: string}})|(array{0?: array{error?: string|array{message: string|array<int, string>, code: string, status: string, type?: string}}}) $data */
176176
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
177177

178178
if (isset($data['error'])) {
179179
throw new ErrorException($data['error'], $response);
180180
} elseif (isset($data[0]['error'])) {
181+
if (! isset($data[0]['error']['type']) && isset($data[0]['error']['status'])) {
182+
$data[0]['error']['type'] = $data[0]['error']['status'];
183+
}
181184
throw new ErrorException($data[0]['error'], $response);
182185
}
183186
} catch (JsonException $jsonException) {

tests/Transporters/HttpTransporter.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,53 @@
624624

625625
$this->http->requestObject($payload);
626626
});
627+
628+
test('error for gemini with invalid parameter', function (string $requestMethod) {
629+
$payload = Payload::create('completions', [
630+
'model' => 'gemini-2.5-flash',
631+
'messages' => [
632+
[
633+
'role' => 'system',
634+
'content' => 'You can use tools if needed.',
635+
],
636+
[
637+
'role' => 'user',
638+
'content' => 'Yo',
639+
],
640+
],
641+
'ddd' => 'auto',
642+
]);
643+
644+
$response = new Response(400, ['Content-Type' => 'application/json; charset=utf-8'], json_encode(
645+
[[
646+
'error' => [
647+
'code' => 400,
648+
'message' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
649+
'status' => 'INVALID_ARGUMENT',
650+
'details' => [
651+
[
652+
'@type' => 'type.googleapis.com/google.rpc.BadRequest',
653+
'fieldViolations' => [
654+
[
655+
'description' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
656+
],
657+
],
658+
],
659+
],
660+
],
661+
]]
662+
));
663+
664+
$this->client
665+
->shouldReceive('sendRequest')
666+
->once()
667+
->andReturn($response);
668+
669+
expect(fn () => $this->http->$requestMethod($payload))
670+
->toThrow(function (ErrorException $e) {
671+
expect($e->getMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.')
672+
->and($e->getErrorMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.')
673+
->and($e->getErrorCode())->toBe(400)
674+
->and($e->getErrorType())->toBe('INVALID_ARGUMENT');
675+
});
676+
})->with('request methods');

0 commit comments

Comments
 (0)