blib/lib/Langertha/ToolChoice.pm

filter: Statementstmt 91.3% 84 / 92 Branchbran 82.5% 66 / 80 Conditioncond 57.5% 19 / 33 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 90.0% 18 / 20 Podpod 100% 15 / 15 totaltotal 80.3% 209 / 260 CC 54 SCAR 25.5
CC
54
Cov
82%
from_hash
28.2
to_openai
7.8
to_anthropic
7.8
CRAP 12.8
« blib/lib/Langertha/Role/AsyncHTTP.pm ↑ Summary blib/lib/Langertha/Role/ResponseFormat.pm »
1 package Langertha::ToolChoice;
2 # ABSTRACT: Immutable canonical tool-selection policy with cross-provider conversion
3 183 our $VERSION = '0.503';
4 183 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 183 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 183 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 183 ▶use Moose::Util::TypeConstraints qw( enum );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # Canonical types: 'auto' (let model decide), 'any' (must call any tool),
11 # 'none' (no tool calling), 'tool' (must call this specific tool).
12 183 enum 'Langertha::ToolChoice::Type' => [qw( auto any none tool )];
13  
14 183 has type => (
15 is => 'ro',
16 isa => 'Langertha::ToolChoice::Type',
17 required => 1,
18 );
19  
20  
21 has name => (
22 is => 'ro',
23 isa => 'Maybe[Str]',
24 155 ▶ default => sub { undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 183 );
26  
27  
28 # --- Convenience constructors ---
29  
30 45 ▶sub auto { my $class = shift; $class->new( type => 'auto' ) }
Subroutine auto: called CC=1 SCAR=0.0
Pod documented
31 58 ▶sub any { my $class = shift; $class->new( type => 'any' ) }
Subroutine any: called CC=1 SCAR=0.0
Pod documented
32 51 ▶sub none { my $class = shift; $class->new( type => 'none' ) }
Subroutine none: called CC=1 SCAR=0.0
Pod documented
33 99 ▶sub specific {
Subroutine specific: called CC=1 SCAR=0.0
Pod documented
34 99 my ( $class, $name ) = @_;
35 99 return $class->new( type => 'tool', name => $name );
36 }
37  
38  
39 # --- Constructors from wire-format hashes/strings ---
40  
41 241 ▶sub from_hash {
Subroutine from_hash: called CC=24 SCAR=33.4
Pod documented
42 241 my ($class, $val) = @_;
43 241 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 2 239 241
44 # A ToolChoice is already canonical input (karr k235): hand it back, so every
45 # request builder serializes it with ->to($fmt) instead of leaking TO_JSON.
46 239 ▶ return $val if blessed($val) && $val->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and $val->isa(__PACKAGE__) 54 185 239
Condition100%3/3
builtin::blessed($val) and $val->isa(__PACKAGE__)
!ll&&!rl&&r
184154
MC/DC100%2/2
builtin::blessed($val) and $val->isa(__PACKAGE__)
builtin::blessed($val) $val->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($val)B= $val->isa(__PACKAGE__)
ABresult
0X0
100
111
47  
48 185 ▶ if ( !ref($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $val) 100 85 185
49 100 ▶ return $class->any if $val eq 'required' || $val eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "required" or $val eq "any" 41 59 100
Condition100%3/3
$val eq "required" or $val eq "any"
l!l&&r!l&&!r
32959
MC/DC100%2/2
$val eq "required" or $val eq "any"
$val eq "required" $val eq "any"
Truth table100%3/3
A or B
A= $val eq "required"B= $val eq "any"
ABresult
1X1
011
000
50 59 ▶ return $class->auto if $val eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "auto" 25 34 59
51 34 ▶ return $class->none if $val eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "none" 34 0 34
52 0 return undef;
53 }
54  
55 85 ▶ return undef unless ref($val) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "HASH" 1 84 85
56 84 ▶ my $type = $val->{type} // '';
Condition50%1/2
$val->{"type"} // ""
l!l
840
MC/DC0%0/1
$val->{"type"} // ""
$val->{"type"}
Truth table50%1/2
A
A= $val->{"type"}
Aresult
11
00
57  
58 84 ▶ if ( $type eq 'function' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "function") 7 77 84
59 7 my $name = '';
60 7 ▶ if ( ref( $val->{function} ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val->{'function'} eq 'HASH') { } 7 0 7
elsif (defined $val->{'name'}) { } 0 0 0
61 7 ▶ $name = $val->{function}{name} // '';
Condition50%1/2
$val->{"function"}{"name"} // ""
l!l
70
MC/DC0%0/1
$val->{"function"}{"name"} // ""
$val->{"function"}{"name"}
Truth table50%1/2
A
A= $val->{"function"}{"name"}
Aresult
11
00
62 } elsif ( defined $val->{name} ) {
63 0 ▶ $name = $val->{name} // '';
Condition0%0/2
$val->{"name"} // ""
l!l
00
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table0%0/2
A
A= $val->{"name"}
Aresult
11
00
64 }
65 7 ▶ return length($name) ? $class->specific($name) : $class->auto;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 7 0 7
66 }
67  
68 77 ▶ if ( $type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tool") 67 10 77
69 67 ▶ my $name = $val->{name} // '';
Condition50%1/2
$val->{"name"} // ""
l!l
670
MC/DC0%0/1
$val->{"name"} // ""
$val->{"name"}
Truth table50%1/2
A
A= $val->{"name"}
Aresult
11
00
70 67 ▶ return length($name) ? $class->specific($name) : $class->auto;
Branch: 1 branch
BranchTrueFalseTotal
length $name ? : 67 0 67
71 }
72  
73 10 ▶ return $class->any if $type eq 'any' || $type eq 'required';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "any" or $type eq "required" 3 7 10
Condition100%3/3
$type eq "any" or $type eq "required"
l!l&&r!l&&!r
217
MC/DC100%2/2
$type eq "any" or $type eq "required"
$type eq "any" $type eq "required"
Truth table100%3/3
A or B
A= $type eq "any"B= $type eq "required"
ABresult
1X1
011
000
74 7 ▶ return $class->auto if $type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "auto" 2 5 7
75 5 ▶ return $class->none if $type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "none" 0 5 5
76 5 return undef;
77 }
78  
79  
80 0 ▶sub from_openai { shift->from_hash(@_) }
Subroutine from_openai: not called CC=1 SCAR=6.9
Pod documented
81 0 ▶sub from_anthropic { shift->from_hash(@_) }
Subroutine from_anthropic: not called CC=1 SCAR=6.9
Pod documented
82  
83 # --- Serializers ---
84  
85 39 ▶sub to_openai {
Subroutine to_openai: called CC=7 SCAR=20.5
Pod documented
86 39 my ($self) = @_;
87 39 ▶ return 'required' if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 9 30 39
88 30 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 6 24 30
89 24 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 7 17 24
90 17 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 17 0 17
91 17 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 17 0 17
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table33%1/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
92 ? { type => 'function', function => { name => $self->name } }
93 : 'auto';
94 }
95 0 return undef;
96 }
97  
98 28 ▶sub to_anthropic {
Subroutine to_anthropic: called CC=7 SCAR=20.5
Pod documented
99 28 my ($self) = @_;
100 28 ▶ return { type => 'any' } if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 4 24 28
101 24 ▶ return { type => 'auto' } if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 5 19 24
102 19 ▶ return { type => 'none' } if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 4 15 19
103 15 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 15 0 15
104 15 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 15 0 15
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table33%1/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
105 ? { type => 'tool', name => $self->name }
106 : { type => 'auto' };
107 }
108 0 return undef;
109 }
110  
111  
112 4 ▶sub to_perplexity {
Subroutine to_perplexity: called CC=3 SCAR=11.0
Pod documented
113 4 my ($self) = @_;
114 # Perplexity only accepts string forms: none / auto / required.
115 # Named-tool forcing is not supported on the wire — engine layer
116 # is expected to switch to a response_format-based path when a
117 # named tool is requested. We coerce here so callers that ignore
118 # capabilities still get *something* sensible.
119 4 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 1 3 4
120 3 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 1 2 3
121 2 return 'required';
122 }
123  
124  
125 18 ▶sub to_gemini {
Subroutine to_gemini: called CC=7 SCAR=19.7
Pod documented
126 18 my ($self) = @_;
127 # Gemini uses toolConfig.functionCallingConfig:
128 # { mode => AUTO|ANY|NONE, allowed_function_names => [...] }
129 # Gemini also offers a fifth mode, VALIDATED ("model decides, but validates a
130 # function call with constrained decoding") — deliberately NOT modeled here:
131 # the canonical vocabulary is none|auto|required|named, and VALIDATED ("auto,
132 # but a call it makes is schema-valid") has no canonical equivalent. If the
133 # canonical set ever grows a fifth policy, that is where it maps (karr k140).
134 18 ▶ return { functionCallingConfig => { mode => 'NONE' } } if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 3 15 18
135 15 ▶ return { functionCallingConfig => { mode => 'AUTO' } } if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 6 9 15
136 9 ▶ return { functionCallingConfig => { mode => 'ANY' } } if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 3 6 9
137 6 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 6 0 6
138 6 ▶ return { functionCallingConfig => { mode => 'AUTO' } }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->name and length $self->name 1 5 6
Condition66%2/3
defined $self->name and length $self->name
!ll&&!rl&&r
105
MC/DC50%1/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table66%2/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
139 unless defined $self->name && length $self->name;
140 return {
141 5 functionCallingConfig => {
142 mode => 'ANY',
143 allowed_function_names => [ $self->name ],
144 },
145 };
146 }
147 0 return undef;
148 }
149  
150  
151 13 ▶sub to_responses {
Subroutine to_responses: called CC=7 SCAR=20.5
Pod documented
152 13 my ($self) = @_;
153 # Responses API uses flat {type => 'function', name => 'foo'} — no nested function wrapper
154 13 ▶ return 'auto' if $self->type eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "auto" 3 10 13
155 10 ▶ return 'none' if $self->type eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "none" 3 7 10
156 7 ▶ return 'required' if $self->type eq 'any';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq "any" 2 5 7
157 5 ▶ if ( $self->type eq 'tool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type eq "tool") 5 0 5
158 5 ▶ return defined $self->name && length $self->name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name && length $self->name ? : 5 0 5
Condition33%1/3
defined $self->name and length $self->name
!ll&&!rl&&r
005
MC/DC0%0/2
defined $self->name and length $self->name
defined $self->name length $self->name
Truth table33%1/3
A and B
A= defined $self->nameB= length $self->name
ABresult
0X0
100
111
159 ? { type => 'function', name => $self->name }
160 : 'auto';
161 }
162 0 return undef;
163 }
164  
165  
166 5 ▶sub to_hash {
Subroutine to_hash: called CC=2 SCAR=6.9
Pod documented
167 5 my ($self) = @_;
168 5 ▶ return { type => $self->type, ( defined $self->name ? ( name => $self->name ) : () ) };
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 3 2 5
169 }
170  
171  
172 # Make the object transparent to any JSON encoder configured with
173 # convert_blessed => 1 (the house default, see Langertha::Plugin::Langfuse).
174 # to_hash is the complete canonical representation, so this is a plain
175 # delegator — nothing is dropped.
176 2 ▶sub TO_JSON { shift->to_hash }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
177  
178 # --- Tag-driven dispatch ---
179  
180 # Maps a tool_wire_format tag to the per-format serializer. Only the wires that
181 # actually carry a tool_choice request parameter are listed: Ollama and Hermes
182 # have no wire-level tool_choice (Hermes forces via prompt injection), so to()
183 # croaks for them. Perplexity is not a tool_wire_format value — its named-tool
184 # request is rewritten to response_format by chat_f — so to_perplexity stays a
185 # standalone helper, off the tag dispatch; it is legacy (Sonar /chat/completions
186 # forms), the Agent API has no tool_choice (karr k233).
187 183 my %TO_METHOD = (
188 openai => 'to_openai',
189 anthropic => 'to_anthropic',
190 gemini => 'to_gemini',
191 responses => 'to_responses',
192 );
193  
194 71 ▶sub to {
Subroutine to: called CC=4 SCAR=14.3
Pod documented
195 71 my ( $self, $fmt ) = @_;
196 71 ▶ my $method = $TO_METHOD{ $fmt // '' }
Branch: 1 branch
BranchTrueFalseTotal
my $method = $TO_METHOD{$fmt // ""} or croak(q[Langertha::ToolChoice: unknown wire format '] . ($fmt // "") . q[']) 68 3 71
Condition50%2/4
$fmt // ""
l!l
710
$fmt // ""
l!l
30
MC/DC0%0/1
$fmt // ""
$fmt
MC/DC0%0/1
$fmt // ""
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
Truth table50%1/2
A
A= $fmt
Aresult
11
00
197 or croak "Langertha::ToolChoice: unknown wire format '" . ( $fmt // '' ) . "'";
198 68 return $self->$method;
199 }
200  
201  
202 183 __PACKAGE__->meta->make_immutable;
203  
204  
205 183 1;
206  
207 __END__
208  
209 =pod
210  
211 =encoding UTF-8
212  
213 =head1 NAME
214  
215 Langertha::ToolChoice - Immutable canonical tool-selection policy with cross-provider conversion
216  
217 =head1 VERSION
218  
219 version 0.503
220  
221 =head1 SYNOPSIS
222  
223 use Langertha::ToolChoice;
224  
225 my $choice = Langertha::ToolChoice->specific('get_weather');
226  
227 $choice->to('openai'); # { type => 'function', function => { name => 'get_weather' } }
228 $choice->to('anthropic'); # { type => 'tool', name => 'get_weather' }
229 $choice->to('responses'); # { type => 'function', name => 'get_weather' }
230 $choice->to('gemini');
231 # { functionCallingConfig => { mode => 'ANY',
232 # allowed_function_names => ['get_weather'] } }
233  
234 # Normalize whatever the caller passed (string, any provider's hash,
235 # or a ToolChoice object) into the canonical form
236 my $tc = Langertha::ToolChoice->from_hash('required'); # type 'any'
237 $tc = Langertha::ToolChoice->from_hash(
238 { type => 'function', function => { name => 'extract' } } );
239 say $tc->type, ' ', $tc->name; # tool extract
240  
241 # A ToolChoice object is valid tool_choice input on every engine
242 my $response = await $engine->chat_f(
243 messages => [ { role => 'user', content => $prompt } ],
244 tools => [ $tool ],
245 tool_choice => Langertha::ToolChoice->specific('extract'),
246 );
247  
248 =head1 DESCRIPTION
249  
250 Canonical value object for the tool-selection policy of a request: may the
251 model call a tool, must it, and must it call one particular tool. It sits
252 beside L<Langertha::Tool>, L<Langertha::ToolCall> and L<Langertha::ToolResult>
253 in the tool wire-translation seam: request builders normalize the caller's
254 C<tool_choice> with L</from_hash> and serialize it with L</to>, dispatched by
255 the engine's C<tool_wire_format>, so the per-provider spelling lives in this
256 one place (ADR 0001, ADR 0010).
257  
258 The canonical vocabulary has four policies, held in L</type>:
259  
260 =over 4
261  
262 =item * C<auto> - the model decides whether to call a tool
263  
264 =item * C<any> - the model must call some tool (OpenAI's C<required>)
265  
266 =item * C<none> - the model must not call a tool
267  
268 =item * C<tool> - the model must call the tool named in L</name>
269  
270 =back
271  
272 A ToolChoice object is valid C<tool_choice> input on every engine and tool
273 wire: L</from_hash> hands an object back unchanged, and every request builder
274 serializes it through L</to> rather than passing the object through. What
275 happens beyond the serialized value (for example L<Langertha::Role::Chat/chat_f>
276 rewriting a named choice into a C<response_format> on engines without the
277 C<tool_choice_named> capability) is the engine's business, not this class's.
278  
279 Instances are immutable.
280  
281 =head2 type
282  
283 Required. The canonical policy: one of C<auto>, C<any>, C<none> or C<tool>
284 (the C<Langertha::ToolChoice::Type> enum). Anything else fails the type
285 constraint at construction.
286  
287 =head2 name
288  
289 The tool to force when L</type> is C<tool>; C<undef> by default. A C<tool>
290 choice without a non-empty name serializes as C<auto> on every wire. The name
291 is not checked against the request's tool list.
292  
293 =head2 auto
294  
295 my $auto = Langertha::ToolChoice->auto;
296  
297 Class method that builds an C<auto> choice.
298  
299 =head2 any
300  
301 my $any = Langertha::ToolChoice->any;
302  
303 Class method that builds an C<any> choice.
304  
305 =head2 none
306  
307 my $none = Langertha::ToolChoice->none;
308  
309 Class method that builds a C<none> choice.
310  
311 =head2 specific
312  
313 my $choice = Langertha::ToolChoice->specific('get_weather');
314  
315 Class method that builds a C<tool> choice forcing the named tool.
316  
317 =head2 from_hash
318  
319 my $choice = Langertha::ToolChoice->from_hash($tool_choice);
320  
321 Class method that normalizes a caller-supplied C<tool_choice> into a
322 ToolChoice, whichever provider's spelling it uses. Accepts:
323  
324 =over 4
325  
326 =item * a ToolChoice object, returned unchanged
327  
328 =item * the strings C<auto>, C<none>, and C<required> or C<any> (both give
329 C<any>)
330  
331 =item * a hash with C<type> C<auto>, C<none>, C<any> or C<required>
332  
333 =item * a named-tool hash in the OpenAI Chat Completions shape
334 C<< { type => 'function', function => { name => ... } } >>, the flat
335 Responses shape C<< { type => 'function', name => ... } >>, or the Anthropic
336 shape C<< { type => 'tool', name => ... } >>; a missing or empty name gives
337 C<auto>
338  
339 =back
340  
341 Returns C<undef> for C<undef> and for anything it does not recognize (an
342 unknown string or C<type>, a non-hash reference). It never dies, so callers
343 test the result.
344  
345 =head2 from_openai
346  
347 Alias for L</from_hash>, which reads every supported shape regardless of the
348 provider it came from.
349  
350 =head2 from_anthropic
351  
352 Alias for L</from_hash>, like L</from_openai>.
353  
354 =head2 to_openai
355  
356 my $wire = $choice->to_openai;
357  
358 Serializes for the OpenAI Chat Completions C<tool_choice> field: the strings
359 C<auto>, C<none> and C<required> (for C<any>), or
360 C<< { type => 'function', function => { name => $name } } >> for a named
361 tool. The C<openai> entry of L</to>.
362  
363 =head2 to_anthropic
364  
365 my $wire = $choice->to_anthropic;
366  
367 Serializes for the Anthropic Messages C<tool_choice> field: always a hash,
368 C<< { type => 'auto' } >>, C<< { type => 'any' } >>, C<< { type => 'none' } >>
369 or C<< { type => 'tool', name => $name } >>. The C<anthropic> entry of
370 L</to>. C<disable_parallel_tool_use> is not part of this value; the request
371 builder in L<Langertha::Role::AnthropicCompatible> folds the engine's
372 C<parallel_tool_use> (L<Langertha::Role::ParallelToolUse>) into the block.
373  
374 =head2 to_perplexity
375  
376 my $wire = $choice->to_perplexity; # 'none' | 'auto' | 'required'
377  
378 B<Legacy.> Serializes to the string forms of Perplexity's old Sonar
379 C</chat/completions> endpoint: C<none>, C<auto>, and C<required> for both
380 C<any> and a named tool (that wire could not force a named tool). Kept for
381 callers of that endpoint; L<Langertha::Engine::Perplexity> no longer uses it.
382 The Agent API (C</v1/agent>) that engine speaks has no C<tool_choice> field at
383 all, so the engine never sends one (see L<Langertha::Role::ResponsesCompatible>).
384 Not on the C<to($fmt)> dispatch.
385  
386 =head2 to_gemini
387  
388 my $wire = $choice->to_gemini;
389 # { functionCallingConfig => { mode => 'ANY', allowed_function_names => ['x'] } }
390  
391 Serializes for Gemini's C<toolConfig>: C<< { functionCallingConfig => { mode
392 => 'AUTO' | 'ANY' | 'NONE' } } >>, and for a named tool mode C<ANY> with
393 C<allowed_function_names> holding that one name. Gemini's C<VALIDATED> mode
394 has no canonical equivalent and is never produced. The C<gemini> entry of
395 L</to>.
396  
397 =head2 to_responses
398  
399 my $wire = $choice->to_responses;
400  
401 Serializes for the Open-Responses C<tool_choice> field
402 (L<Langertha::Engine::OpenAIResponses>): the strings C<auto>, C<none> and
403 C<required> (for C<any>), or the flat C<< { type => 'function', name =>
404 $name } >> for a named tool, without the nested C<function> wrapper of Chat
405 Completions. The C<responses> entry of L</to>.
406  
407 =head2 to_hash
408  
409 my $hash = $choice->to_hash; # { type => 'tool', name => 'extract' }
410  
411 The canonical, provider-neutral form: C<type>, plus C<name> when it is
412 defined. L</from_hash> reads it back.
413  
414 =head2 TO_JSON
415  
416 Returns L</to_hash>, so a JSON encoder with C<convert_blessed> enabled
417 serializes the object in its canonical form. That is for logging and
418 tracing (for example L<Langertha::Plugin::Langfuse>); a request body gets the
419 wire form from L</to>.
420  
421 =head2 to
422  
423 my $wire = $choice->to( $engine->tool_wire_format );
424  
425 Serializes for a C<tool_wire_format> by dispatching to L</to_openai>,
426 L</to_anthropic>, L</to_gemini> or L</to_responses>. Only those four wires
427 carry a C<tool_choice> request field. C<ollama> and C<hermes> have none, so
428 C<to> croaks for them, as it does for any unknown or undefined format. (On
429 the hermes wire L<Langertha::Role::Chat> handles the choice itself: C<none>
430 withholds the tools from the prompt, anything else but C<auto> is ignored
431 with a warning.) L</to_perplexity> is not on this dispatch.
432  
433 =head1 SEE ALSO
434  
435 =over
436  
437 =item * L<Langertha::Tool> - Sibling value object for tool definitions
438  
439 =item * L<Langertha::ToolCall> - Sibling value object for the calls a model emits
440  
441 =item * L<Langertha::ToolResult> - Sibling value object for tool result blocks
442  
443 =item * L<Langertha::Role::Chat/chat_f> - Takes C<tool_choice> and rewrites a
444 named choice where the wire cannot force a tool
445  
446 =item * L<Langertha::Role::Capabilities> - The C<tool_choice_auto>,
447 C<tool_choice_any>, C<tool_choice_none> and C<tool_choice_named> flags
448  
449 =item * ADR 0001 and ADR 0010 in F<docs/adr/> - Why tool wire-translation
450 routes through these value objects
451  
452 =back
453  
454 =head1 SUPPORT
455  
456 =head2 Issues
457  
458 Please report bugs and feature requests on GitHub at
459 L<https://github.com/Getty/langertha/issues>.
460  
461 =head2 IRC
462  
463 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
464  
465 =head1 CONTRIBUTING
466  
467 Contributions are welcome! Please fork the repository and submit a pull request.
468  
469 =head1 AUTHOR
470  
471 Torsten Raudssus <getty@cpan.org>
472  
473 =head1 COPYRIGHT AND LICENSE
474  
475 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
476  
477 This is free software; you can redistribute it and/or modify it under
478 the same terms as the Perl 5 programming language system itself.
479  
480 =cut
« blib/lib/Langertha/Role/AsyncHTTP.pm ↑ Summary blib/lib/Langertha/Role/ResponseFormat.pm »