Skip to main content
When the LLM response contains a list of items rather than a single object, use the Sequence wrapper. It saves you from creating a dedicated class with a single array property just to hold a list.

Basic Usage

Pass Sequence::of(ClassName::class) as the response model to extract a typed collection.
The returned $list is a Sequence instance containing fully validated Person objects.

Working with Sequences

Sequence implements ArrayAccess and IteratorAggregate, so you can use it like an array. It also provides convenience methods for common operations.

Streaming Sequences

One of the most powerful features of sequences is streaming completed items. While partials() yields the entire object on every property change, sequence() yields only when a new item in the list is fully populated.
This is ideal for progressive UI updates — you can render each person in a list as soon as the LLM finishes generating their data, without waiting for the entire response. Keep in mind that items yielded during streaming are deserialized but not yet validated. Only the final sequence returned by finalValue() is fully validated.

Named Sequences

You can provide a name and description to give the LLM more context about what the collection represents.

Combining with Output Formats

You can combine sequences with output formats. For example, to get the sequence data as a plain array of arrays: