NAME

Langertha::Stream - Iterator for streaming responses

VERSION

version 0.202

SYNOPSIS

my $stream = $engine->simple_chat_stream_iterator('Tell me a story');

# Iterate chunk by chunk
while (my $chunk = $stream->next) {
    print $chunk->content;
}

# Or use the callback form
$stream->reset;
$stream->each(sub {
    my ($chunk) = @_;
    print $chunk->content;
});

# Collect all remaining chunks
my @chunks = $stream->collect;

# Get complete content as a string
my $full_text = $stream->content;

DESCRIPTION

An iterator object wrapping an array of Langertha::Stream::Chunk objects returned from a streaming LLM response. Created by "simple_chat_stream_iterator" in Langertha::Role::Chat.

The iterator maintains a position cursor so you can step through chunks one at a time with next, consume them all with collect or each, and start over with reset.

chunks

ArrayRef of Langertha::Stream::Chunk objects comprising the full streaming response. Required.

next

while (my $chunk = $stream->next) {
    print $chunk->content;
}

Returns the next Langertha::Stream::Chunk and advances the cursor, or undef when all chunks have been consumed.

has_next

if ($stream->has_next) { ... }

Returns true if there are more chunks to iterate over.

collect

my @chunks = $stream->collect;

Returns all remaining chunks as a list and advances the cursor to the end.

content

my $text = $stream->content;

Returns the concatenated content of all chunks in the stream as a single string, regardless of the current cursor position.

each

$stream->each(sub {
    my ($chunk) = @_;
    print $chunk->content;
});

Iterates over all remaining chunks, calling $callback with each Langertha::Stream::Chunk. Dies if no callback is provided.

reset

$stream->reset;

Resets the cursor to the beginning so the stream can be iterated again.

SEE ALSO

SUPPORT

Issues

Please report bugs and feature requests on GitHub at https://github.com/Getty/langertha/issues.

CONTRIBUTING

Contributions are welcome! Please fork the repository and submit a pull request.

AUTHOR

Torsten Raudssus <torsten@raudssus.de> https://raudss.us/

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Torsten Raudssus.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.