blib/lib/Langertha/Engine/OpenAIResponses.pm

filter: Statementstmt 100% 16 / 16 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 50.0% 1 / 2 totaltotal 95.6% 22 / 23 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
api_key_env
1.0
stream_format
1.0
CRAP 1.0
« blib/lib/Langertha/Cost.pm ↑ Summary blib/lib/Langertha/Request/HTTP.pm »
1 package Langertha::Engine::OpenAIResponses;
2 # ABSTRACT: OpenAI Responses API (reasoning models like gpt-5.5-pro)
3 28 our $VERSION = '0.503';
4 28 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 extends 'Langertha::Engine::OpenAI';
7  
8 28 with 'Langertha::Role::ResponsesCompatible', 'Langertha::Role::ServerTools';
9  
10  
11 # Protocol variant of OpenAI: shares the vendor's API key.
12 4 ▶sub api_key_env { 'LANGERTHA_OPENAI_API_KEY' }
Subroutine api_key_env: called CC=1 SCAR=0.0
Pod documented
13  
14 # The Responses envelope role can stream (typed SSE), but this engine has never
15 # supported it. Opt out: stream_format => undef, and clear the streaming flag
16 # that Role::Streaming (inherited via Engine::OpenAI) would otherwise advertise
17 # (ADR 0002 escape hatch). Both together keep supports('streaming') honest.
18 1 ▶sub stream_format { return undef }
Subroutine stream_format: called CC=1 SCAR=0.0
Pod undocumented
19  
20 726 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 726 my ( $orig, $self, @rest ) = @_;
22 726 my $caps = $self->$orig(@rest);
23 726 delete $caps->{streaming};
24 726 return $caps;
25 28 };
26  
27 # Tool-result PDFs (karr k361): function_call_output.output takes an
28 # input_file part (API reference, FunctionCallOutput: "string or array of
29 # ResponseInputTextContent or ResponseInputImageContent or
30 # ResponseInputFileContent", developers.openai.com/api/reference/resources/
31 # responses/methods/create; data: URL + filename per the PDF-files guide,
32 # developers.openai.com/api/docs/guides/pdf-files; fetched 2026-09-30).
33 # Docs-derived, not live-verified. Role::Tools still requires image_input
34 # (the guide names vision models for PDF input). Perplexity shares the
35 # envelope but not this part, so the flag sits here, not on the role.
36 12 ▶sub _tool_result_pdf_on_wire { 1 }
Subroutine _tool_result_pdf_on_wire: called CC=1 SCAR=0.0
37  
38 28 __PACKAGE__->meta->make_immutable;
39  
40  
41 28 1;
42  
43 __END__
44  
45 =pod
46  
47 =encoding UTF-8
48  
49 =head1 NAME
50  
51 Langertha::Engine::OpenAIResponses - OpenAI Responses API (reasoning models like gpt-5.5-pro)
52  
53 =head1 VERSION
54  
55 version 0.503
56  
57 =head1 SYNOPSIS
58  
59 use Langertha::Engine::OpenAIResponses;
60  
61 my $engine = Langertha::Engine::OpenAIResponses->new(
62 api_key => $ENV{OPENAI_API_KEY},
63 model => 'gpt-5.5-pro', # reasoning-only model
64 );
65  
66 my $response = $engine->simple_chat('Hello');
67 print $response;
68  
69 =head1 DESCRIPTION
70  
71 Provides access to OpenAI's Responses API endpoint (C<POST /v1/responses>)
72 for reasoning-only models like C<gpt-5.5-pro>, C<o3-pro>, and future
73 C<-pro> SKUs that are not available on the Chat Completions endpoint
74 (C</v1/chat/completions>).
75  
76 Unlike L<Langertha::Engine::OpenAI> which calls C</v1/chat/completions>, this
77 engine speaks the Open-Responses wire envelope: C<input> instead of
78 C<messages>, top-level C<instructions>, flat tool objects, and an C<output[]>
79 array with type discriminators. That envelope lives in
80 L<Langertha::Role::ResponsesCompatible> (parallel to
81 L<Langertha::Role::OpenAICompatible>); this engine is a thin shell that inherits
82 OpenAI's Bearer auth, API key and model list from L<Langertha::Engine::OpenAI>,
83 composes the Responses envelope on top, and opts out of streaming.
84  
85 This engine returns a L<Langertha::Response> that is shape-compatible with
86 the chat path, so existing consumers (including Goldmine's C<complete>
87 method) work without modification. Reasoning tokens are normalized to
88 C<completion_tokens_details.reasoning_tokens> for cost lookup compatibility.
89  
90 =head2 Structured output
91  
92 Structured output goes under C<text.format> (a flat json_schema, not the
93 Chat-Completions nested shape); the Responses API has no C<response_format>
94 param. See L<Langertha::Role::ResponsesCompatible/_responses_format_kwargs>.
95  
96 =head2 Server-side tools
97  
98 OpenAI's hosted tools (C<web_search>, C<file_search>, C<code_interpreter>,
99 C<image_generation>, remote C<mcp>, ...) are supported: this engine composes
100 L<Langertha::Role::ServerTools>, so C<supports('server_tools')> is true. Pass
101 them per request in C<tools> (native hashes or L<Langertha::ServerTool>
102 objects, mixed freely with function tools), or once on the engine:
103  
104 my $engine = Langertha::Engine::OpenAIResponses->new(
105 api_key => $ENV{OPENAI_API_KEY},
106 model => 'gpt-5.6-luna',
107 server_tools => [ { type => 'web_search' } ],
108 );
109 my $r = $engine->simple_chat('What is the current stable Perl 5 release?');
110 say $_->{url} for @{ $r->citations // [] };
111  
112 The provider runs them within the request. What it did lands on
113 L<Langertha::Response/server_tool_calls>, the C<url_citation> annotations of
114 the answer on L<Langertha::Response/citations>; neither ever reaches
115 L<Langertha::Response/tool_calls>, so C<chat_with_tools_f> executes only
116 function calls and echoes the server items back unchanged on the next turn.
117 The search sources of a C<web_search_call> (request them with
118 C<< include => ['web_search_call.action.sources'] >>) stay on that call's
119 C<data>.
120  
121 A remote C<mcp> tool must say C<< require_approval => 'never' >>: OpenAI's
122 default is C<always>, which answers with an C<mcp_approval_request> the client
123 has to confirm, and Langertha has no approval flow yet. Anything else croaks
124 before the request is sent (checked by L<Langertha::ServerTool/to>).
125  
126 =head2 Function call output shape
127  
128 The Responses API emits C<function_call> as a top-level C<output[]> item
129 (real reasoning models) or nested inside a message item (older fixtures);
130 C<chat_response> and L<Langertha::ToolCall/extract> walk both. Streaming is
131 not supported — L<Langertha::Role::ResponsesCompatible> can stream the
132 envelope, but this engine opts out (see below).
133  
134 =head1 SEE ALSO
135  
136 =over
137  
138 =item * L<Langertha::Role::ResponsesCompatible> - the Open-Responses wire envelope
139  
140 =item * L<Langertha::Role::ServerTools> / L<Langertha::ServerTool> - server-side tools
141  
142 =item * L<Langertha::Engine::OpenAI> - Chat Completions endpoint (for non-reasoning models)
143  
144 =item * L<Langertha::Engine::Perplexity> - the other Responses-envelope consumer (Agent API)
145  
146 =item * L<Langertha::ToolCall> - Tool call extraction from Responses format
147  
148 =item * L<Langertha::ToolChoice/to_responses> - Responses tool_choice serialization
149  
150 =item * L<Langertha::Tool/to_responses> - Responses tool serialization
151  
152 =back
153  
154 =head1 SUPPORT
155  
156 =head2 Issues
157  
158 Please report bugs and feature requests on GitHub at
159 L<https://github.com/Getty/langertha/issues>.
160  
161 =head2 IRC
162  
163 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
164  
165 =head1 CONTRIBUTING
166  
167 Contributions are welcome! Please fork the repository and submit a pull request.
168  
169 =head1 AUTHOR
170  
171 Torsten Raudssus <getty@cpan.org>
172  
173 =head1 COPYRIGHT AND LICENSE
174  
175 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
176  
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179  
180 =cut
« blib/lib/Langertha/Cost.pm ↑ Summary blib/lib/Langertha/Request/HTTP.pm »