NAME

Langertha::Role::Streaming - Role for streaming support

VERSION

version 0.202

SYNOPSIS

# Synchronous streaming via Role::HTTP
my $chunks = $engine->execute_streaming_request($request, sub {
    my ($chunk) = @_;
    print $chunk->content;
});

# Streaming with iterator
my $stream = $engine->simple_chat_stream_iterator('Tell me a story');
while (my $chunk = $stream->next) {
    print $chunk->content;
}

DESCRIPTION

Provides stream parsing for server-sent events (SSE) and newline-delimited JSON (NDJSON) streaming responses from LLM APIs. Engines composing this role must implement parse_stream_chunk and stream_format. Works together with Langertha::Role::HTTP for synchronous streaming and Langertha::Role::Chat for the higher-level streaming API.

parse_sse_line

my $parsed = $engine->parse_sse_line($line);

Parses a single Server-Sent Events line. Returns undef for empty lines and SSE comments. Returns a HashRef with type set to 'done', 'data', or 'event'. Data lines have their JSON decoded under the data key.

parse_ndjson_line

my $parsed = $engine->parse_ndjson_line($line);

Parses a single newline-delimited JSON line. Returns undef for empty lines. Returns a HashRef with type => 'data' and the decoded data.

process_stream_data

my $chunks = $engine->process_stream_data($raw_body, $chunk_callback);
my $chunks = $engine->process_stream_data($raw_body);

Parses a complete streaming response body according to the engine's stream_format ('sse' or 'ndjson'). Calls parse_stream_chunk on each data event and optionally calls $chunk_callback with each resulting Langertha::Stream::Chunk. Returns an ArrayRef of all chunks.

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.