Langertha/Engine/OpenAIResponses.pm

filter: Statementstmt 98.1% 104 / 106 Branchbran 70.2% 52 / 74 Conditioncond 51.0% 47 / 92 MC/DCmcdc 10.4% 5 / 48 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 8 totaltotal 64.9% 224 / 345 CC 84 SCAR 32.7
CC
84
Cov
75%
chat_response
42.6
chat_request
41.9
response_text_content
13.4
CRAP 26.3
« Langertha/Raid.pm ↑ Summary Langertha/Engine/Ollama.pm »
1 package Langertha::Engine::OpenAIResponses;
2 # ABSTRACT: OpenAI Responses API (reasoning models like gpt-5.5-pro)
3 1 our $VERSION = '0.502';
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Langertha::Tool;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 extends 'Langertha::Engine::OpenAI';
11  
12  
13 override 'chat_operation_id' => sub {
14 8 ▶ return 'createResponse';
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 };
16  
17 sub chat_request {
18 6 ▶ my ( $self, $messages, %extra ) = @_;
Subroutine chat_request: called CC=24 SCAR=37.3
Pod undocumented
19  
20 # Normalize tool_choice to Responses format
21 6 ▶ if ( exists $extra{tool_choice} && defined $extra{tool_choice} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra{"tool_choice"} and defined $extra{"tool_choice"}) 1 5 6
Condition66%2/3
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
!ll&&!rl&&r
501
MC/DC50%1/2
exists $extra{"tool_choice"} and defined $extra{"tool_choice"}
exists $extra{"tool_choice"} defined $extra{"tool_choice"}
Truth table66%2/3
A and B
A= exists $extra{"tool_choice"}B= defined $extra{"tool_choice"}
ABresult
0X0
100
111
22 1 ▶ if ( my $tc = Langertha::ToolChoice->from_hash( $extra{tool_choice} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tc = "Langertha::ToolChoice"->from_hash($extra{"tool_choice"})) 1 0 1
23 1 $extra{tool_choice} = $tc->to_responses;
24 }
25 }
26  
27 # If tools passed in MCP format (inputSchema camelCase), format them
28 # to Responses flat format. If already formatted (has 'type' key), pass through.
29 6 ▶ if ( exists $extra{tools} && ref $extra{tools} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra{"tools"} and ref $extra{"tools"} eq "ARRAY") 2 4 6
Condition66%2/3
exists $extra{"tools"} and ref $extra{"tools"} eq "ARRAY"
!ll&&!rl&&r
402
MC/DC50%1/2
exists $extra{"tools"} and ref $extra{"tools"} eq "ARRAY"
exists $extra{"tools"} ref $extra{"tools"} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $extra{"tools"}B= ref $extra{"tools"} eq "ARRAY"
ABresult
0X0
100
111
30 2 my @tools = @{$extra{tools}};
31 2 ▶ if ( @tools && ref $tools[0] eq 'HASH' && !exists $tools[0]{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tools and ref $tools[0] eq "HASH" and not exists $tools[0]{"type"}) 2 0 2
Condition33%2/6
@tools and ref $tools[0] eq "HASH" and not exists $tools[0]{"type"}
!ll&&!rl&&r
002
@tools and ref $tools[0] eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/3
@tools and ref $tools[0] eq "HASH" and not exists $tools[0]{"type"}
@tools ref $tools[0] eq "HASH" not exists $tools[0]{"type"}
Truth table25%1/4
A and B and not(C)
A= @toolsB= ref $tools[0] eq "HASH"C= not exists $tools[0]{"type"}
ABCresult
0XX0
10X0
1100
1111
32 2 $extra{tools} = $self->format_tools(\@tools);
33 }
34 }
35  
36 # parallel_tool_use -> parallel_tool_calls (only when tools present)
37 6 ▶ if ( !exists $extra{parallel_tool_calls}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $extra{"parallel_tool_calls"} and exists $extra{"tools"} and $self->can("has_parallel_tool_use") and $self->has_parallel_tool_use) 0 6 6
Condition66%6/9
not exists $extra{"parallel_tool_calls"} and exists $extra{"tools"} and $self->can("has_parallel_tool_use") and $self->has_parallel_tool_use
!ll&&!rl&&r
420
not exists $extra{"parallel_tool_calls"} and exists $extra{"tools"} and $self->can("has_parallel_tool_use")
!ll&&!rl&&r
402
not exists $extra{"parallel_tool_calls"} and exists $extra{"tools"}
!ll&&!rl&&r
042
MC/DC0%0/4
not exists $extra{"parallel_tool_calls"} and exists $extra{"tools"} and $self->can("has_parallel_tool_use") and $self->has_parallel_tool_use
not exists $extra{"parallel_tool_calls"} exists $extra{"tools"} $self->can("has_parallel_tool_use") $self->has_parallel_tool_use
Truth table40%2/5
not(A) and B and C and D
A= not exists $extra{"parallel_tool_calls"}B= exists $extra{"tools"}C= $self->can("has_parallel_tool_use")D= $self->has_parallel_tool_use
ABCDresult
0XXX0
10XX0
110X0
11100
11111
38 && exists $extra{tools}
39 && $self->can('has_parallel_tool_use') && $self->has_parallel_tool_use ) {
40 0 ▶ $extra{parallel_tool_calls} = $self->parallel_tool_use ? JSON->true : JSON->false;
Branch: 1 branch
BranchTrueFalseTotal
$self->parallel_tool_use ? : 0 0 0
41 }
42  
43 # Build input array: strip system messages (go to instructions instead)
44 6 my @input;
45 6 for my $msg (@$messages) {
46 7 ▶ next if ( $msg->{role} // '' ) eq 'system';
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->{"role"} // "") eq "system" 1 6 7
Condition50%1/2
$msg->{"role"} // ""
l!l
70
MC/DC0%0/1
$msg->{"role"} // ""
$msg->{"role"}
Truth table50%1/2
A
A= $msg->{"role"}
Aresult
11
00
47 6 push @input, $self->_normalize_input_item($msg);
48 }
49  
50 6 ▶ my @request_args = (
Branch: 6 branches
BranchTrueFalseTotal
defined $self->chat_model ? : 6 0 6
$self->has_system_prompt ? : 1 5 6
scalar @input ? : 6 0 6
$self->get_response_size ? : 1 5 6
$self->can('has_response_format') && $self->has_response_format ? : 0 6 6
$self->has_temperature ? : 1 5 6
Condition33%1/3
$self->can("has_response_format") and $self->has_response_format
!ll&&!rl&&r
060
MC/DC0%0/2
$self->can("has_response_format") and $self->has_response_format
$self->can("has_response_format") $self->has_response_format
Truth table33%1/3
A and B
A= $self->can("has_response_format")B= $self->has_response_format
ABresult
0X0
100
111
51 defined $self->chat_model ? ( model => $self->chat_model ) : (),
52 $self->has_system_prompt ? ( instructions => $self->system_prompt ) : (),
53 scalar(@input) ? ( input => \@input ) : (),
54 $self->get_response_size ? ( max_tokens => $self->get_response_size ) : (),
55 ( $self->can('has_response_format') && $self->has_response_format )
56 ? ( response_format => $self->response_format )
57 : (),
58 $self->has_temperature ? ( temperature => $self->temperature ) : (),
59 stream => JSON->false,
60 %extra,
61 );
62  
63 return $self->generate_request(
64 $self->chat_operation_id,
65 0 ▶ sub { $self->chat_response(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 6 @request_args,
67 );
68 }
69  
70 sub _normalize_input_item {
71 6 ▶ my ( $self, $msg ) = @_;
Subroutine _normalize_input_item: called CC=1 SCAR=0.0
72 # Pass through for now — expand if Responses gains multimodal content blocks
73 6 return $msg;
74 }
75  
76 sub chat_response {
77 4 ▶ my ( $self, $response ) = @_;
Subroutine chat_response: called CC=29 SCAR=37.5
Pod undocumented
78 4 my $data = $self->parse_response($response);
79  
80 4 my ( $text, @tc_data, $finish_reason, $thinking );
81  
82 4 ▶ for my $item ( @{ $data->{output} // [] } ) {
Condition50%1/2
$data->{"output"} // []
l!l
40
MC/DC0%0/1
$data->{"output"} // []
$data->{"output"}
Truth table50%1/2
A
A= $data->{"output"}
Aresult
11
00
83 7 ▶ next unless ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 7 7
84 7 ▶ my $type = $item->{type} // '';
Condition50%1/2
$item->{"type"} // ""
l!l
70
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
85  
86 7 ▶ if ( $type eq 'reasoning' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'reasoning') { } 3 4 7
elsif ($type eq 'message') { } 3 1 4
elsif ($type eq 'function_call') { } 1 0 1
87 # Collect reasoning summary if present
88 3 ▶ my $summary = $item->{summary}[0]{text} // '';
Condition100%2/2
$item->{"summary"}[0]{"text"} // ""
l!l
21
MC/DC100%1/1
$item->{"summary"}[0]{"text"} // ""
$item->{"summary"}[0]{"text"}
Truth table100%2/2
A
A= $item->{"summary"}[0]{"text"}
Aresult
11
00
89 3 ▶ $thinking //= $summary if length $summary;
Branch: 1 branch
BranchTrueFalseTotal
if length $summary 2 1 3
Condition33%1/3
$thinking //= $summary
l!l&&r!l&&!r
020
MC/DC0%0/2
$thinking //= $summary
$thinking $summary
Truth table33%1/3
A //= B
A= $thinkingB= $summary
ABresult
1X1
011
000
90 }
91 elsif ( $type eq 'message' ) {
92 3 ▶ $finish_reason = ( $item->{status} // '' ) eq 'completed' ? 'stop' : ( $item->{status} // '' );
Branch: 1 branch
BranchTrueFalseTotal
($item->{'status'} // '') eq 'completed' ? : 3 0 3
Condition25%1/4
$item->{"status"} // ""
l!l
30
$item->{"status"} // ""
l!l
00
MC/DC0%0/1
$item->{"status"} // ""
$item->{"status"}
MC/DC0%0/1
$item->{"status"} // ""
$item->{"status"}
Truth table50%1/2
A
A= $item->{"status"}
Aresult
11
00
Truth table0%0/2
A
A= $item->{"status"}
Aresult
11
00
93  
94 3 ▶ for my $block ( @{ $item->{content} // [] } ) {
Condition50%1/2
$item->{"content"} // []
l!l
30
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
Truth table50%1/2
A
A= $item->{"content"}
Aresult
11
00
95 3 ▶ my $block_type = $block->{type} // '';
Condition50%1/2
$block->{"type"} // ""
l!l
30
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
96 3 ▶ if ( $block_type eq 'output_text' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block_type eq 'output_text') { } 2 1 3
elsif ($block_type eq 'function_call') { } 1 0 1
97 2 ▶ $text .= ( $block->{text} // '' );
Condition50%1/2
$block->{"text"} // ""
l!l
20
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"text"}
Aresult
11
00
98 }
99 elsif ( $block_type eq 'function_call' ) {
100 1 push @tc_data, $block;
101 }
102 }
103 }
104 elsif ( $type eq 'function_call' ) {
105 # Real Responses API emits function_call as a top-level output[]
106 # item carrying name/arguments/call_id directly on the item.
107 1 push @tc_data, $item;
108 1 ▶ $finish_reason //= 'tool_calls';
Condition50%1/2
$finish_reason //= "tool_calls"
l!l
01
MC/DC0%0/1
$finish_reason //= "tool_calls"
$finish_reason
Truth table50%1/2
A
A= $finish_reason
Aresult
11
00
109 }
110 }
111  
112 # Normalize usage to chat-style (Goldmine expects prompt_tokens/completion_tokens)
113 4 ▶ my $usage = $data->{usage} // {};
Condition50%1/2
$data->{"usage"} // {}
l!l
40
MC/DC0%0/1
$data->{"usage"} // {}
$data->{"usage"}
Truth table50%1/2
A
A= $data->{"usage"}
Aresult
11
00
114 my $normalized_usage = {
115 prompt_tokens => $usage->{input_tokens},
116 completion_tokens => $usage->{output_tokens},
117 total_tokens => $usage->{total_tokens},
118 4 };
119 4 ▶ if ( my $rt = $usage->{output_tokens_details}{reasoning_tokens} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rt = $usage->{"output_tokens_details"}{"reasoning_tokens"}) 4 0 4
120 4 $normalized_usage->{completion_tokens_details} = { reasoning_tokens => $rt };
121 }
122  
123 4 my @tcs = map { $self->_parse_function_call($_) } @tc_data;
124  
125 return Langertha::Response->new(
126 content => $text // '',
127 raw => $data,
128 $data->{id} ? ( id => $data->{id} ) : (),
129 4 ▶ $data->{model} ? ( model => $data->{model} ) : (),
Branch: 5 branches
BranchTrueFalseTotal
$data->{'id'} ? : 4 0 4
$data->{'model'} ? : 4 0 4
defined $finish_reason ? : 4 0 4
@tcs ? : 2 2 4
defined $thinking ? : 2 2 4
Condition100%2/2
$text // ""
l!l
22
MC/DC100%1/1
$text // ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
130 defined $finish_reason ? ( finish_reason => $finish_reason ) : (),
131 usage => $normalized_usage,
132 @tcs ? ( tool_calls => \@tcs ) : (),
133 defined $thinking ? ( thinking => $thinking ) : (),
134 );
135 }
136  
137 sub _parse_function_call {
138 2 ▶ my ( $self, $block ) = @_;
Subroutine _parse_function_call: called CC=7 SCAR=23.4
139 2 ▶ my $args = $block->{arguments} // '{}';
Condition50%1/2
$block->{"arguments"} // "{}"
l!l
20
MC/DC0%0/1
$block->{"arguments"} // "{}"
$block->{"arguments"}
Truth table50%1/2
A
A= $block->{"arguments"}
Aresult
11
00
140 2 ▶ $args = $self->decode_json_text($args) if $args && !ref $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args and not ref $args 2 0 2
Condition33%1/3
$args and not ref $args
!ll&&!rl&&r
002
MC/DC0%0/2
$args and not ref $args
$args not ref $args
Truth table33%1/3
A and not(B)
A= $argsB= not ref $args
ABresult
0X0
100
111
141 return Langertha::ToolCall->new(
142 name => ( $block->{name} // '' ),
143 arguments => ( ref($args) eq 'HASH' ? $args : {} ),
144 2 ▶ id => ( $block->{call_id} // '' ),
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 2 0 2
Condition50%2/4
$block->{"name"} // ""
l!l
20
$block->{"call_id"} // ""
l!l
20
MC/DC0%0/1
$block->{"name"} // ""
$block->{"name"}
MC/DC0%0/1
$block->{"call_id"} // ""
$block->{"call_id"}
Truth table50%1/2
A
A= $block->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $block->{"call_id"}
Aresult
11
00
145 );
146 }
147  
148 sub response_tool_calls {
149 2 ▶ my ( $self, $data ) = @_;
Subroutine response_tool_calls: called CC=11 SCAR=24.9
Pod undocumented
150 2 my @calls;
151 2 ▶ for my $item ( @{ $data->{output} // [] } ) {
Condition50%1/2
$data->{"output"} // []
l!l
20
MC/DC0%0/1
$data->{"output"} // []
$data->{"output"}
Truth table50%1/2
A
A= $data->{"output"}
Aresult
11
00
152 3 ▶ next unless ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 3 3
153 3 ▶ my $type = $item->{type} // '';
Condition50%1/2
$item->{"type"} // ""
l!l
30
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
154 3 ▶ if ( $type eq 'function_call' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'function_call') { } 1 2 3
elsif ($type eq 'message') { } 1 1 2
155 1 push @calls, $item;
156 }
157 elsif ( $type eq 'message' ) {
158 1 ▶ for my $block ( @{ $item->{content} // [] } ) {
Condition50%1/2
$item->{"content"} // []
l!l
10
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
Truth table50%1/2
A
A= $item->{"content"}
Aresult
11
00
159 1 ▶ next unless ( $block->{type} // '' ) eq 'function_call';
Branch: 1 branch
BranchTrueFalseTotal
unless ($block->{"type"} // "") eq "function_call" 0 1 1
Condition50%1/2
$block->{"type"} // ""
l!l
10
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
160 1 push @calls, $block;
161 }
162 }
163 }
164 2 return \@calls;
165 }
166  
167 sub extract_tool_call {
168 2 ▶ my ( $self, $tc ) = @_;
Subroutine extract_tool_call: called CC=3 SCAR=11.0
Pod undocumented
169 2 my $args = $tc->{arguments};
170 2 ▶ $args = $self->decode_json_text($args) if $args && !ref $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args and not ref $args 1 1 2
Condition66%2/3
$args and not ref $args
!ll&&!rl&&r
011
MC/DC50%1/2
$args and not ref $args
$args not ref $args
Truth table66%2/3
A and not(B)
A= $argsB= not ref $args
ABresult
0X0
100
111
171 2 return ( $tc->{name}, $args );
172 }
173  
174 sub format_tools {
175 3 ▶ my ( $self, $mcp_tools ) = @_;
Subroutine format_tools: called CC=3 SCAR=12.0
Pod undocumented
176 # Responses API uses flat tool objects — no {type:'function',function:{...}} wrapper
177 return [
178 map {
179 3 {
180 type => 'function',
181 name => $_->{name},
182 description => $_->{description},
183 parameters => $_->{input_schema} // $_->{inputSchema} // $_->{parameters},
184 },
185 3 ▶ } @$mcp_tools
Condition50%3/6
($_->{"input_schema"} // $_->{"inputSchema"}) // $_->{"parameters"}
l!l&&r!l&&!r
300
$_->{"input_schema"} // $_->{"inputSchema"}
l!l&&r!l&&!r
120
MC/DC0%0/3
($_->{"input_schema"} // $_->{"inputSchema"}) // $_->{"parameters"}
$_->{"input_schema"} $_->{"inputSchema"} $_->{"parameters"}
Truth table50%2/4
A // B // C
A= $_->{"input_schema"}B= $_->{"inputSchema"}C= $_->{"parameters"}
ABCresult
1XX1
01X1
0011
0000
186 ];
187 }
188  
189 sub format_tool_results {
190 1 ▶ my ( $self, $data, $results ) = @_;
Subroutine format_tool_results: called CC=3 SCAR=12.0
Pod undocumented
191 # Responses API: tool results go into a new input item, not back into messages
192 return [
193 map {
194 1 my $r = $_;
195 {
196 role => 'tool',
197 call_id => $r->{tool_call}{call_id} // $r->{tool_call}{id} // '',
198 1 ▶ content => $self->json->encode( $r->{result}{content} ),
Condition40%2/5
($r->{"tool_call"}{"call_id"} // $r->{"tool_call"}{"id"}) // ""
l!l
10
$r->{"tool_call"}{"call_id"} // $r->{"tool_call"}{"id"}
l!l&&r!l&&!r
100
MC/DC0%0/2
($r->{"tool_call"}{"call_id"} // $r->{"tool_call"}{"id"}) // ""
$r->{"tool_call"}{"call_id"} $r->{"tool_call"}{"id"}
Truth table33%1/3
A // B
A= $r->{"tool_call"}{"call_id"}B= $r->{"tool_call"}{"id"}
ABresult
1X1
011
000
199 },
200 } @$results
201 ];
202 }
203  
204 sub response_text_content {
205 1 ▶ my ( $self, $data ) = @_;
Subroutine response_text_content: called CC=11 SCAR=25.9
Pod undocumented
206 1 my $text = '';
207 1 ▶ for my $item ( @{ $data->{output} // [] } ) {
Condition50%1/2
$data->{"output"} // []
l!l
10
MC/DC0%0/1
$data->{"output"} // []
$data->{"output"}
Truth table50%1/2
A
A= $data->{"output"}
Aresult
11
00
208 2 ▶ next unless ref($item) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "HASH" 0 2 2
209 2 ▶ next unless ( $item->{type} // '' ) eq 'message';
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->{"type"} // "") eq "message" 1 1 2
Condition50%1/2
$item->{"type"} // ""
l!l
20
MC/DC0%0/1
$item->{"type"} // ""
$item->{"type"}
Truth table50%1/2
A
A= $item->{"type"}
Aresult
11
00
210 1 ▶ for my $block ( @{ $item->{content} // [] } ) {
Condition50%1/2
$item->{"content"} // []
l!l
10
MC/DC0%0/1
$item->{"content"} // []
$item->{"content"}
Truth table50%1/2
A
A= $item->{"content"}
Aresult
11
00
211 1 ▶ $text .= ( $block->{text} // '' ) if ( $block->{type} // '' ) eq 'output_text';
Branch: 1 branch
BranchTrueFalseTotal
if ($block->{"type"} // "") eq "output_text" 1 0 1
Condition50%2/4
$block->{"type"} // ""
l!l
10
$block->{"text"} // ""
l!l
10
MC/DC0%0/1
$block->{"type"} // ""
$block->{"type"}
MC/DC0%0/1
$block->{"text"} // ""
$block->{"text"}
Truth table50%1/2
A
A= $block->{"type"}
Aresult
11
00
Truth table50%1/2
A
A= $block->{"text"}
Aresult
11
00
212 }
213 }
214 1 return $text;
215 }
216  
217 1 ▶sub stream_format { return undef } # Streaming not supported
Subroutine stream_format: called CC=1 SCAR=0.0
Pod undocumented
218  
219 1 __PACKAGE__->meta->make_immutable;
220  
221  
222 1 1;
223  
224 __END__
225  
226 =pod
227  
228 =encoding UTF-8
229  
230 =head1 NAME
231  
232 Langertha::Engine::OpenAIResponses - OpenAI Responses API (reasoning models like gpt-5.5-pro)
233  
234 =head1 VERSION
235  
236 version 0.502
237  
238 =head1 SYNOPSIS
239  
240 use Langertha::Engine::OpenAIResponses;
241  
242 my $engine = Langertha::Engine::OpenAIResponses->new(
243 api_key => $ENV{OPENAI_API_KEY},
244 model => 'gpt-5.5-pro', # reasoning-only model
245 );
246  
247 my $response = $engine->simple_chat('Hello');
248 print $response;
249  
250 =head1 DESCRIPTION
251  
252 Provides access to OpenAI's Responses API endpoint (C<POST /v1/responses>)
253 for reasoning-only models like C<gpt-5.5-pro>, C<o3-pro>, and future
254 C<-pro> SKUs that are not available on the Chat Completions endpoint
255 (C</v1/chat/completions>).
256  
257 Unlike L<Langertha::Engine::OpenAI> which calls C</v1/chat/completions>, this
258 engine calls C</v1/responses> with the Responses API's own request/response
259 shape. The Responses API uses:
260  
261 =over 4
262  
263 =item * C<input> instead of C<messages>
264  
265 =item * C<instructions> for system prompt (top-level, not in input array)
266  
267 =item * Flat tool objects C<{type, name, description, parameters}> instead
268 of C<{type, function, function: {...}}>
269  
270 =item * C<output[]> array with type discriminators (C<message>, C<reasoning>,
271 C<function_call>) instead of C<choices[]>
272  
273 =item * C<input_tokens>/C<output_tokens> instead of
274 C<prompt_tokens>/C<completion_tokens>
275  
276 =item * C<output_tokens_details.reasoning_tokens> instead of
277 C<completion_tokens_details.reasoning_tokens>
278  
279 =back
280  
281 This engine returns a L<Langertha::Response> that is shape-compatible with
282 the chat path, so existing consumers (including Goldmine's C<complete>
283 method) work without modification. Reasoning tokens are normalized to
284 C<completion_tokens_details.reasoning_tokens> for cost lookup compatibility.
285  
286 =head2 Function call output shape
287  
288 The Responses API emits C<function_call> in two different positions
289 depending on model and request shape:
290  
291 =over 4
292  
293 =item * B<Top-level> C<output[]> item:
294 C<< { type =E<gt> 'function_call', call_id =E<gt> 'call_abc', name =E<gt> 'foo',
295 arguments =E<gt> '{...}' } >>. This is what real reasoning models (e.g.
296 C<gpt-5.5-pro>) return for forced C<tool_choice>.
297  
298 =item * B<Nested> inside a message item:
299 C<< output[type='message'].content[type='function_call'] >>. Historically
300 seen in streaming / older fixtures.
301  
302 =back
303  
304 C<chat_response>, C<response_tool_calls> and L<Langertha::ToolCall/extract>
305 walk both shapes. Streaming is not supported.
306  
307 =head1 SEE ALSO
308  
309 =over
310  
311 =item * L<Langertha::Engine::OpenAI> - Chat Completions endpoint (for non-reasoning models)
312  
313 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
314  
315 =item * L<Langertha::ToolCall> - Tool call extraction from Responses format
316  
317 =item * L<Langertha::ToolChoice/to_responses> - Responses tool_choice serialization
318  
319 =item * L<Langertha::Tool/to_responses> - Responses tool serialization
320  
321 =back
322  
323 =head1 SUPPORT
324  
325 =head2 Issues
326  
327 Please report bugs and feature requests on GitHub at
328 L<https://github.com/Getty/langertha/issues>.
329  
330 =head2 IRC
331  
332 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
333  
334 =head1 CONTRIBUTING
335  
336 Contributions are welcome! Please fork the repository and submit a pull request.
337  
338 =head1 AUTHOR
339  
340 Torsten Raudssus <getty@cpan.org>
341  
342 =head1 COPYRIGHT AND LICENSE
343  
344 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
345  
346 This is free software; you can redistribute it and/or modify it under
347 the same terms as the Perl 5 programming language system itself.
348  
349 =cut
« Langertha/Raid.pm ↑ Summary Langertha/Engine/Ollama.pm »