Documentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/core/llms.txt
Use this file to discover all available pages before exploring further.
Stream wraps the provider’s raw streaming generator and gives you three ways to consume a streaming text generation: iterate text chunks directly, inspect every typed stream part via parts(), or register lifecycle hooks that fire automatically as the stream is drained. All three approaches accumulate state internally, so calling result() or run() after any of them returns a fully-populated TextResult.
Methods
Yields each text delta string as it arrives from the model. Only
TextDeltaPart values are surfaced; all other part types (tool calls, finish signals, metadata) are silently consumed and accumulated in the internal StreamState. Use this when you only need the raw text and do not care about other event types.Yields every
StreamPart emitted by the provider, including text deltas, tool call events, reasoning deltas, finish signals, and provider metadata. This is the most detailed consumption path. If an ErrorPart arrives and no onError hook is registered, the wrapped Throwable is re-thrown from inside the generator.Implements
IteratorAggregate, delegating to parts(). Allows the Stream object to be used directly in a foreach loop without calling parts() explicitly.Drains the entire stream — consuming all parts, firing all registered hooks — and returns the accumulated
TextResult. Use this when you want the complete result without manually iterating.Returns the
TextResult assembled from the accumulated StreamState. Call this after you have already iterated the stream (via chunks(), parts(), or foreach). Calling result() before the stream is fully consumed will return a partial result.Registers a callback that is invoked for each text delta as it arrives. The callback receives the delta string:
fn(string $text): void. Returns $this for fluent chaining.A callable with the signature
fn(string $text): void. Receives the incremental text string from each TextDeltaPart.Registers a callback that is invoked once after the stream fully completes and all parts have been consumed. The callback receives the final
TextResult: fn(TextResult $result): void. Returns $this for fluent chaining.A callable with the signature
fn(TextResult $result): void.Registers a callback that is invoked when an
ErrorPart arrives in the stream. The callback receives the wrapped Throwable: fn(Throwable $e): void. When this hook is registered, the error is consumed and the stream continues; without it, the exception is re-thrown. Returns $this for fluent chaining.A callable with the signature
fn(Throwable $e): void.Registers a callback that is invoked once after the stream finishes for each fully assembled
ToolCall. Tool call argument fragments are accumulated across ToolCallStartPart and ToolCallDeltaPart events and decoded into complete ToolCall objects before the callback fires. Returns $this for fluent chaining.A callable with the signature
fn(ToolCall $call): void.Stream part types
Every value yielded byparts() is an instance of StreamPart. Use instanceof checks to branch on the specific type.
Carries an incremental fragment of the model’s text output.
Carries an incremental fragment of the model’s internal reasoning or extended thinking text. Only emitted by reasoning-capable models when reasoning is enabled.
Emitted once when a streamed tool call begins. Carries the slot index and the tool’s identity. Argument JSON arrives in subsequent
ToolCallDeltaPart events tied to the same index.A fragment of a streamed tool call’s argument JSON. Fragments are accumulated by
StreamState per slot index and decoded once the stream finishes, ensuring valid JSON even when the provider splits argument data across many events.Emitted at the end of the stream to signal that generation is complete. Carries the final finish reason and token usage for the turn.
Wraps a
Throwable that occurred during streaming. If an onError hook is registered, the hook is called and the stream continues; otherwise the exception is re-thrown from the generator.Carries provider-specific metadata emitted during the stream. Multiple parts may arrive for the same provider;
StreamState merges them by provider key using array_merge.