Description
###
Description
When calling OpenAI::models()->list()
, an uncaught TypeError
is thrown due to ErrorException::__construct()
receiving a string
instead of the expected array
.
Stack trace:
#0 vendor/openai-php/client/src/Transporters/HttpTransporter.php(133): OpenAI\Exceptions\ErrorException->__construct('PostgresConnect...', 503)
#1 vendor/openai-php/client/src/Transporters/HttpTransporter.php(57): OpenAI\Transporters\HttpTransporter->throwIfJsonError(Array, '{"error":"Postg...')
#2 vendor/openai-php/client/src/Resources/Models.php(28): OpenAI\Transporters\HttpTransporter->requestObject(Object(OpenAI\ValueObjects\Transporter\Payload))
#3 backend/modules/aiAssistant/services/AiService.php(190): OpenAI\Resources\Models->list()
Cause
The throwIfJsonError()
method passes the raw response body (a string) into ErrorException::__construct()
expecting it to be an array. However, when the response is invalid JSON or a plain error string (e.g., "PostgresConnectionError"), this causes a fatal TypeError
.
Expected behavior
The SDK should gracefully handle string error responses and normalize them into an appropriate format before passing to ErrorException
.
Suggested fix
Add a type check or JSON decode fallback in HttpTransporter::throwIfJsonError()
before instantiating ErrorException
.
if (is_string($body)) {
$decoded = json_decode($body, true);
if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
$body = $decoded;
} else {
$body = ['error' => $body];
}
}
SDK Version
openai-php/client version: v^0.10.3
PHP version: 8.3
Framework: Yii2
Additional context
Triggered during a call to OpenAI::models()->list() — most likely while OpenAI returned a non-JSON 503 error response (PostgresConnectionError).
Steps To Reproduce
Install the openai-php/client package (I was using version x.x.x, please adjust to your actual version).
Call the method OpenAI::models()->list() (or any other method that triggers an API call).
Simulate an API error response by:
Using an invalid API key,
Disabling the internet connection,
Or waiting for a temporary OpenAI outage (e.g., 503 PostgresConnectionError).
Observe that a raw TypeError is thrown instead of a handled exception.
OpenAI PHP Client Version
v0.10.3
PHP Version
8.3
Notes
No response