Skip to content

Commit 54d60ea

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 54d60ea

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,52 @@
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+
'role' => 'system',
633+
'content' => 'You can use tools if needed.',
634+
],
635+
[
636+
'role' => 'user',
637+
'content' => 'Yo',
638+
],
639+
],
640+
'ddd' => 'auto',
641+
]);
642+
643+
$response = new Response(400, ['Content-Type' => 'application/json; charset=utf-8'], json_encode(
644+
[[
645+
'error' => [
646+
'code' => 400,
647+
'message' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
648+
'status' => 'INVALID_ARGUMENT',
649+
'details' => [
650+
[
651+
'@type' => 'type.googleapis.com/google.rpc.BadRequest',
652+
'fieldViolations' => [
653+
[
654+
'description' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
655+
],
656+
],
657+
],
658+
],
659+
],
660+
]]
661+
));
662+
663+
$this->client
664+
->shouldReceive('sendRequest')
665+
->once()
666+
->andReturn($response);
667+
668+
expect(fn () => $this->http->$requestMethod($payload))
669+
->toThrow(function (ErrorException $e) {
670+
expect($e->getMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.')
671+
->and($e->getErrorMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.')
672+
->and($e->getErrorCode())->toBe(400)
673+
->and($e->getErrorType())->toBe('INVALID_ARGUMENT');
674+
});
675+
})->with('request methods');

0 commit comments

Comments
 (0)