Hi, I have a problem to make streaming work properly. It works fine except all streamed events are returned all at once at the end and they're not streamed continuously throughout the request. As I was debugging it with Claude it came up with following response
The SDK uses Guzzle's PSR-18 sendRequest() which calls curl_exec() synchronously and buffers the entire body
before returning — stream => true only controls where Guzzle writes the buffer (temp file vs memory), not
whether it waits. There's no way to get true chunked reading through PSR-18's interface.
The project already has symfony/http-client installed, which has proper streaming support. Let me replace the SDK's HTTP layer with it for this use case.
Once Claude replaced it with following code, it started to work as expected.
use Symfony\Component\HttpClient\HttpClient;
$httpClient = HttpClient::create(['timeout' => 120]);
$response = $httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
'headers' => [
'x-api-key' => 'claudeapikey',
'anthropic-version' => '2023-06-01',
'content-type' => 'application/json',
],
'json' => [
'model' => $model,
'max_tokens' => $maxTokens,
'stream' => true,
'messages' => [
['role' => 'user', 'content' => $prompt],
],
],
]);
$buffer = '';
foreach ($httpClient->stream($response) as $chunk)
{
$buffer .= $chunk->getContent();
while (($pos = strpos($buffer, "\n\n")) !== false)
{
$rawEvent = substr($buffer, 0, $pos);
$buffer = substr($buffer, $pos + 2);
foreach (explode("\n", $rawEvent) as $line)
{
if (!str_starts_with($line, 'data: '))
continue;
$data = substr($line, 6);
if ($data === '[DONE]')
continue;
$parsed = json_decode($data, true);
if ($parsed !== null)
yield $parsed;
}
}
}
Hi, I have a problem to make streaming work properly. It works fine except all streamed events are returned all at once at the end and they're not streamed continuously throughout the request. As I was debugging it with Claude it came up with following response
Once Claude replaced it with following code, it started to work as expected.