openai-php / client

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

[Bug]: Empty body in HttpTransporter::requestObject ($response->getBody()->getContents()) when using Guzzle client with Middleware::log() #432

Open root-phpdev opened 5 months ago

root-phpdev commented 5 months ago

Description

When using the Guzzle client and middleware to log requests/responses in the \Anthropic\Transporters\HttpTransporter::requestObject() method, when calling $response->getBody()->getContents() the result is an empty string. This is a well known old issue https://github.com/guzzle/guzzle/issues/1582

and it produce an error: Syntax error

Perhaps to solve the problem it is worth using cast to string or calling the __toString() method or rewind body

Steps To Reproduce

$stack = HandlerStack::create();

$stack->push(Middleware::log(
    $this->logger,
    new MessageFormatter('{hostname} {req_header_User-Agent} - [{date_common_log}] "{method} {uri} HTTP/{version}" {code} {res_header_Content-Type} {res_header_Content-Length} {res_body}')
));

$httpClient = new \GuzzleHttp\Client([
    'handler' => $stack,
]);

$client = OpenAI::factory()
    ->withApiKey($apiKey)
    ->withHttpClient($httpClient)
    ->make();

OpenAI PHP Client Version

v0.8.5

PHP Version

8.1.20

Notes

No response