Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Undefined array key "index" #526

Open
j-schumann opened this issue Feb 20, 2025 · 2 comments
Open

[Bug]: Undefined array key "index" #526

j-schumann opened this issue Feb 20, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@j-schumann
Copy link

Description

Warning: Undefined array key "index" caused in Responses/Completions/CreateResponseChoice.php#L23

Steps To Reproduce

By receiving the following attributes:
Image

from Openrouter.ai with different models, using

        $client = \OpenAI::factory()
            ->withApiKey($yourApiKey)
            ->withBaseUri('https://openrouter.ai/api/v1')
            ->make();

        $result = $client->completions()->create([
            //'model' => 'gpt-3.5-turbo-instruct',
            "model" => "google/gemini-2.0-pro-exp-02-05:free",
            'prompt' => 'Say this is a test',
            'max_tokens' => 6,
            'temperature' => 0
        ]);

OpenAI PHP Client Version

v0.10.3

PHP Version

8.4.3

Notes

No response

@j-schumann j-schumann added the bug Something isn't working label Feb 20, 2025
@legsak1mbo
Copy link

I found the same when using Gemini. The issue appears to be that Google do not return an id attribute with their responses. A quick fix was to null coalesce $attributes['id'] in the CreateResponse class for both Chats and Completions (lines 54 and 53 respectively):-

$attributes['id'] ?? bin2hex(random_bytes(6)),

NB I gave them a random value rather than empty or null just in case.

@ghostza1209
Copy link

I've fixed it by using my custom HTTP client

    $yourApiKey = config('app.gemini_api_key');
        $client = OpenAI::factory()
            ->withApiKey($yourApiKey)
            ->withHttpClient(new HttpClient())
            ->withBaseUri('generativelanguage.googleapis.com/v1beta/openai')
            ->make();

HttpClient.php

<?php

namespace Modules\RealtyVision\Client;

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Utils;
use Illuminate\Support\Arr;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class HttpClient implements ClientInterface
{
    /**
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ClientExceptionInterface
     */
    public function sendRequest(RequestInterface $request): ResponseInterface
    {
        $stack = HandlerStack::create();
        $stack->push(Middleware::mapResponse(function ($response) {
            $body = $this->ensureIdExisted($response->getBody()->getContents());
            $newBody = Utils::streamFor($body);
            return $response->withBody($newBody);
        }));

        $client = new Client(['handler' => $stack]);
        return $client->sendRequest($request);
    }

    /**
     * @param string $body
     * @return string
     */
    private function ensureIdExisted(string $body): string
    {
        $array = json_decode($body, true);
        $array['id'] = Arr::get($array, 'id') ?? uniqid();

        return json_encode($array);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants