Langertha/Role/ResponseFormat.pm

filter: Statementstmt 86.6% 26 / 30 Branchbran 60.0% 12 / 20 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 80.0% 48 / 60 CC 12 SCAR 23.3
CC
12
Cov
77%
decode_loose_json
13.5
__ANON__
2.0
BEGIN
1.0
CRAP 10.3
« Langertha/Role/Streaming.pm ↑ Summary Langertha/Usage.pm »
1 package Langertha::Role::ResponseFormat;
2 # ABSTRACT: Role for an engine where you can specify structured output
3 34 our $VERSION = '0.502';
4 34 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 has response_format => (
8 isa => 'HashRef',
9 is => 'ro',
10 predicate => 'has_response_format',
11 );
12  
13  
14 sub decode_loose_json {
15 7 ▶ my ( $self, $text ) = @_;
Subroutine decode_loose_json: called CC=11 SCAR=26.0
Pod documented
16 7 ▶ return undef unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 2 5 7
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
115
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
17  
18 my $try = sub {
19 8 ▶ my ($s) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
20 8 my $r = eval { decode_json($s) };
21 8 ▶ return $@ ? undef : $r;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 4 4 8
22 5 };
23  
24 5 ▶ if ( my $r = $try->($text) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($text)) 1 4 5
25 1 return $r;
26 }
27 4 ▶ if ( $text =~ /```(?:json)?\s*(.*?)\s*```/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /```(?:json)?\s*(.*?)\s*```/s) 2 2 4
28 2 ▶ if ( my $r = $try->($1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($1)) 2 0 2
29 2 return $r;
30 }
31 }
32 2 ▶ if ( $text =~ /(\{.*\})/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(\{.*\})/s) 1 1 2
33 1 my $candidate = $1;
34 1 ▶ if ( my $r = $try->($candidate) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($candidate)) 1 0 1
35 1 return $r;
36 }
37 0 ▶ while ( length $candidate > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $candidate > 2) 0 0 0
38 0 ▶ $candidate =~ s/\}[^}]*$/\}/ or last;
Branch: 1 branch
BranchTrueFalseTotal
$candidate =~ s/\}[^}]*$/}/ or last 0 0 0
39 0 ▶ if ( my $r = $try->($candidate) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($candidate)) 0 0 0
40 0 return $r;
41 }
42 }
43 }
44 1 return undef;
45 }
46  
47  
48  
49 34 1;
50  
51 __END__
52  
53 =pod
54  
55 =encoding UTF-8
56  
57 =head1 NAME
58  
59 Langertha::Role::ResponseFormat - Role for an engine where you can specify structured output
60  
61 =head1 VERSION
62  
63 version 0.502
64  
65 =head2 decode_loose_json
66  
67 my $data = $engine->decode_loose_json($text);
68  
69 Tolerant JSON decoder for structured-output responses where providers
70 sometimes wrap the payload in code fences or surrounding prose. Tries:
71  
72 =over
73  
74 =item 1. Decode the whole text as JSON.
75  
76 =item 2. Strip C<```json ... ```> code fences and decode the inner block.
77  
78 =item 3. Decode the first balanced C<{...}> substring.
79  
80 =back
81  
82 Returns the decoded value (typically a HashRef) on success, or C<undef>
83 if all strategies fail. Override in an engine subclass when a provider
84 needs a custom strategy (e.g. always-prose-wrapped output).
85  
86 =head2 response_format
87  
88 A HashRef specifying the structured output format for the response. The exact
89 structure depends on the engine. For OpenAI-compatible engines this is typically
90 C<{ type => 'json_object' }> or a JSON Schema definition. Optional.
91  
92 =head1 SEE ALSO
93  
94 =over
95  
96 =item * L<Langertha::Role::Chat> - Chat functionality that uses response format
97  
98 =item * L<Langertha::Role::OpenAICompatible> - OpenAI-compatible engines that support this role
99  
100 =back
101  
102 =head1 SUPPORT
103  
104 =head2 Issues
105  
106 Please report bugs and feature requests on GitHub at
107 L<https://github.com/Getty/langertha/issues>.
108  
109 =head2 IRC
110  
111 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
112  
113 =head1 CONTRIBUTING
114  
115 Contributions are welcome! Please fork the repository and submit a pull request.
116  
117 =head1 AUTHOR
118  
119 Torsten Raudssus <getty@cpan.org>
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
124  
125 This is free software; you can redistribute it and/or modify it under
126 the same terms as the Perl 5 programming language system itself.
127  
128 =cut
« Langertha/Role/Streaming.pm ↑ Summary Langertha/Usage.pm »