Streaming lets your application display tokens as the model produces them rather than waiting for the entire response to be assembled. This makes long-form responses feel substantially faster to end users because the first words appear almost immediately after the request is sent.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/groq/llms.txt
Use this file to discover all available pages before exploring further.
How Streaming Works
When you call->stream() instead of ->run(), the SDK opens a server-sent events connection to Groq’s API. The model writes tokens into that stream as it generates them. The SDK surfaces those tokens to your code through a chunks() generator so you can echo, buffer, or process each piece independently.
After the stream is exhausted you call ->run() on the stream object to get the same TextModelResponse you would receive from a non-streaming call — with text, usage, and providerMetadata all populated.
Basic Streaming Example
Replace->run() with ->stream(), then iterate the chunks. Call ->run() on the stream object when you need token counts or provider metadata.
stream-basic.php
->run() must be called after the foreach loop finishes. Calling it before the stream is exhausted will not yield correct usage counts.Streaming in a CLI Application
The following is a complete command-line script that streams a response and prints a usage summary once the stream finishes.cli-stream.php
Terminal
Streaming in a Web Context
Browsers buffer output by default. You need to flush PHP’s output buffers on every chunk to ensure tokens reach the client as they are produced.Disable output buffering and set the content type
Send the correct headers before any output so the browser treats the response as a stream.
web-stream.php
Checking Streaming Support
All Groq models registered with the package declareStreaming as a Native capability. You can verify this at runtime with ->supports().
check-streaming.php
check-custom-model.php
| Model ID | Streaming |
|---|---|
llama-3.1-8b-instant | ✅ Native |
llama-3.3-70b-versatile | ✅ Native |
meta-llama/llama-4-scout* | ✅ Native |
meta-llama/llama-4-maverick* | ✅ Native |
moonshotai/kimi* | ✅ Native |
openai/gpt-oss-20b | ✅ Native |
openai/gpt-oss-120b | ✅ Native |
Full API Reference for Streaming
| Method | Returns | Description |
|---|---|---|
->stream() | Stream object | Opens the SSE connection and returns a lazy stream |
$stream->chunks() | Generator<string> | Yields each text chunk as a string as it arrives |
$stream->run() | TextModelResponse | Blocks until the stream is exhausted; returns the full response |
TextModelResponse returned by $stream->run() is identical to the one returned by ->run() in non-streaming mode and carries text, usage->inputTokens, usage->outputTokens, and providerMetadata['groq'].