blib/lib/Langertha/Stream/Chunk.pm

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 22 / 22 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Langertha/Role/Temperature.pm ↑ Summary blib/lib/LangerthaX.pm »
1 package Langertha::Stream::Chunk;
2 # ABSTRACT: Represents a single chunk from a streaming response
3 181 our $VERSION = '0.503';
4 181 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 181 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 181 has content => (
9 is => 'ro',
10 isa => 'Str',
11 required => 1,
12 );
13  
14  
15 181 has raw => (
16 is => 'ro',
17 isa => 'HashRef',
18 predicate => 'has_raw',
19 );
20  
21  
22 181 has is_final => (
23 is => 'ro',
24 isa => 'Bool',
25 default => 0,
26 );
27  
28  
29 181 has model => (
30 is => 'ro',
31 isa => 'Str',
32 predicate => 'has_model',
33 );
34  
35  
36 181 has finish_reason => (
37 is => 'ro',
38 isa => 'Maybe[Str]',
39 predicate => 'has_finish_reason',
40 );
41  
42  
43 181 has usage => (
44 is => 'ro',
45 isa => 'Maybe[HashRef]',
46 predicate => 'has_usage',
47 );
48  
49 181 has cached_tokens => (
50 is => 'ro',
51 isa => 'Maybe[Int]',
52 predicate => 'has_cached_tokens',
53 );
54  
55 181 has tool_calls => (
56 is => 'ro',
57 isa => 'Maybe[ArrayRef[Langertha::ToolCall]]',
58 predicate => 'has_tool_calls',
59 );
60  
61  
62 181 has citations => (
63 is => 'ro',
64 isa => 'Maybe[ArrayRef]',
65 predicate => 'has_citations',
66 );
67  
68  
69 181 has thinking => (
70 is => 'ro',
71 isa => 'Maybe[Str]',
72 predicate => 'has_thinking',
73 );
74  
75  
76 181 has refusal => (
77 is => 'ro',
78 isa => 'Maybe[Str]',
79 predicate => 'has_refusal',
80 );
81  
82  
83  
84  
85  
86 181 __PACKAGE__->meta->make_immutable;
87  
88 181 1;
89  
90 __END__
91  
92 =pod
93  
94 =encoding UTF-8
95  
96 =head1 NAME
97  
98 Langertha::Stream::Chunk - Represents a single chunk from a streaming response
99  
100 =head1 VERSION
101  
102 version 0.503
103  
104 =head1 SYNOPSIS
105  
106 my $stream = $engine->simple_chat_stream_iterator('Tell me a story');
107  
108 while (my $chunk = $stream->next) {
109 print $chunk->content;
110  
111 if ($chunk->is_final) {
112 say "\nModel: ", $chunk->model if $chunk->has_model;
113 say "Finish: ", $chunk->finish_reason if $chunk->has_finish_reason;
114 }
115 }
116  
117 =head1 DESCRIPTION
118  
119 A single text chunk delivered during a streaming LLM response. Each chunk
120 carries incremental content text and optional metadata. Chunks are collected
121 into a L<Langertha::Stream> iterator by
122 L<Langertha::Role::Chat/simple_chat_stream_iterator>.
123  
124 =head2 content
125  
126 The incremental text content delivered in this chunk. Required. For most
127 chunks this is a word or partial word; the final chunk may be an empty
128 string.
129  
130 =head2 raw
131  
132 The raw parsed API response data for this chunk as a HashRef. Use
133 C<has_raw> to check whether it was provided.
134  
135 =head2 is_final
136  
137 Boolean flag set to C<1> on the last chunk of a stream. Defaults to C<0>.
138  
139 =head2 model
140  
141 The model identifier returned by the provider, if present. Use C<has_model>
142 to check availability.
143  
144 =head2 finish_reason
145  
146 The reason the stream ended: C<stop>, C<length>, C<tool_calls>, etc.
147 Provider-specific values are preserved as-is. C<undef> on non-final chunks.
148 Use C<has_finish_reason> to check availability.
149  
150 =head2 tool_calls
151  
152 Optional ArrayRef of finished L<Langertha::ToolCall> objects that complete
153 on this chunk. Every call the model streams lands on exactly one chunk, as the
154 same object the non-streaming reply of that response carries on
155 L<Langertha::Response/tool_calls>; a chunk never holds a fragment. Where it
156 lands depends on the dialect:
157  
158 =over
159  
160 =item * Chat-Completions (L<Langertha::Role::OpenAICompatible>): the
161 C<delta.tool_calls> fragments are assembled per C<index>, and all calls land on
162 the chunk that carries C<finish_reason>.
163  
164 =item * Anthropic Messages (L<Langertha::Role::AnthropicCompatible>): each
165 C<tool_use> block is assembled from its C<input_json_delta> fragments and lands
166 on the chunk for its C<content_block_stop>, before the final chunk.
167  
168 =item * Gemini and Ollama native: calls arrive whole and land on the chunk that
169 carries them.
170  
171 =item * Open-Responses (L<Langertha::Role::ResponsesCompatible>): the calls land
172 on the final chunk, read from the terminal C<response.completed> event.
173  
174 =item * Hermes (L<Langertha::Role::HermesTools>, tools in the prompt): the
175 C<E<lt>tool_callE<gt>> blocks are withheld from C<content> and their calls land
176 on the final chunk (see L<Langertha::Role::Chat/chat_stream_realtime_f>).
177  
178 =back
179  
180 Most chunks have no tool calls — use C<has_tool_calls> to check, or
181 L<Langertha::Role::Chat/aggregate_tool_calls> to collect them all in stream
182 order.
183  
184 =head2 citations
185  
186 Optional ArrayRef of search-augmented source citations, populated on the final
187 chunk when a search-augmented engine emits them mid-stream. The Open-Responses
188 envelope (L<Langertha::Engine::Perplexity>) lifts the C<search_results> block
189 out of the terminal C<response.completed> C<output[]> here, so a streamed reply
190 surfaces the same sources the non-streaming path exposes as
191 L<Langertha::Response/citations>. Most chunks carry none — use C<has_citations>
192 to check. L<Langertha::Stream/citations> reassembles them off the stream.
193  
194 =head2 thinking
195  
196 Optional incremental chain-of-thought / reasoning text delivered in this
197 chunk, parallel to L</content> and L</tool_calls>. Populated by the dialect
198 stream parsers from their verified per-provider delta spelling — the
199 OpenAI-compatible C<delta.reasoning_content> / bare C<delta.reasoning>,
200 Anthropic's C<thinking_delta>, Gemini's C<thought> parts, and Ollama native
201 C<message.thinking>. Most chunks carry no thinking — use C<has_thinking> to
202 check. The full streamed thinking is reassembled by
203 L<Langertha::Role::Chat/aggregate_thinking>, the streaming counterpart of
204 L<Langertha::Response/thinking> on the non-streaming path.
205  
206 =head2 refusal
207  
208 Optional fragment of a refusal delivered in this chunk: the OpenAI-compatible
209 C<delta.refusal>, and the whole refusal of a Responses API stream on its final
210 chunk. Concatenated in order, the fragments are the text
211 L<Langertha::Response/refusal> carries on the non-streaming path. Use
212 C<has_refusal> to check.
213  
214 =head2 usage
215  
216 Token usage counts as a HashRef, if provided by the engine on the final
217 chunk. Keys vary by provider. Use C<has_usage> to check availability. An
218 OpenAI-compatible stream requested with C<include_usage> reports it on a
219 content-less chunk after the final one;
220 L<Langertha::Role::Chat/aggregate_usage> finds it either way.
221  
222 =head2 cached_tokens
223  
224 Number of prompt tokens served from the prefix cache, if reported by the
225 provider on the final chunk. Populated from
226 C<usage.prompt_tokens_details.cached_tokens> on the OpenAI-compatible wire
227 (SGLang with C<return_cached_tokens_details> enabled, and other servers
228 that emit the detail block) and from C<usage.input_tokens_details.cached_tokens>
229 on the Open-Responses wire (OpenAI Responses / Perplexity Agent). C<undef>
230 when the provider does not report it. Use C<has_cached_tokens> to check
231 availability.
232  
233 =head1 SEE ALSO
234  
235 =over
236  
237 =item * L<Langertha::Stream> - Iterator that holds chunks
238  
239 =item * L<Langertha::Response> - Non-streaming response object
240  
241 =item * L<Langertha::Role::Chat> - Chat role that produces streams
242  
243 =back
244  
245 =head1 SUPPORT
246  
247 =head2 Issues
248  
249 Please report bugs and feature requests on GitHub at
250 L<https://github.com/Getty/langertha/issues>.
251  
252 =head2 IRC
253  
254 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
255  
256 =head1 CONTRIBUTING
257  
258 Contributions are welcome! Please fork the repository and submit a pull request.
259  
260 =head1 AUTHOR
261  
262 Torsten Raudssus <getty@cpan.org>
263  
264 =head1 COPYRIGHT AND LICENSE
265  
266 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
267  
268 This is free software; you can redistribute it and/or modify it under
269 the same terms as the Perl 5 programming language system itself.
270  
271 =cut
« blib/lib/Langertha/Role/Temperature.pm ↑ Summary blib/lib/LangerthaX.pm »