openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.7k stars 483 forks source link

fix: Use method that seeks stream #204

Closed AlfredSorrow closed 9 months ago

AlfredSorrow commented 11 months ago

Right now if you are using a guzzle middleware that reads content (or something else that also reads content), you will get empty string when you call getBody()->getContents() and as a consequence a json decode error, because the stream has already been read

Sample code that reads the body before returning the response:

$stack = HandlerStack::create();

$stack->push(
    Middleware::log(
        $this->logger,
        new MessageFormatter(MessageFormatter::DEBUG)
    )
);

$this->client = (new Factory())->withApiKey($this->apiKey)
    ->withOrganization($this->organization)
    ->withHttpClient(new Client(['handler' => $stack]))
    ->make();

Solution:
Use StreamInterface::_toString(), because it must attempt to seek to the beginning of the stream

gehrisandro commented 11 months ago

Hi @AlfredSorrow

Could you please add a test?

gehrisandro commented 9 months ago

Going to close this for now. Feel free to reopen and provide the additional information required.