blib/lib/Langertha/Role/ResponseFormat.pm

filter: Statementstmt 82.8% 29 / 35 Branchbran 54.5% 12 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 76.4% 52 / 68 CC 13 SCAR 25.4
CC
13
Cov
73%
decode_loose_json
17.3
__ANON__
2.0
BEGIN
1.0
CRAP 12.6
« blib/lib/Langertha/ToolChoice.pm ↑ Summary blib/lib/Langertha/HTTP/BoundedDecode.pm »
1 package Langertha::Role::ResponseFormat;
2 # ABSTRACT: Role for an engine where you can specify structured output
3 178 our $VERSION = '0.503';
4 178 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 178 ▶use JSON::MaybeXS qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 178 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 178 has response_format => (
9 isa => 'HashRef',
10 is => 'ro',
11 predicate => 'has_response_format',
12 );
13  
14  
15 29 ▶sub decode_loose_json {
Subroutine decode_loose_json: called CC=12 SCAR=28.5
Pod documented
16 29 my ( $self, $text ) = @_;
17 29 ▶ return undef unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 2 27 29
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
1127
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
18  
19 30 ▶ my $try = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
20 30 my ($s) = @_;
21 # decode_json (the JSON::MaybeXS utf8 variant) expects bytes, but $s reaches
22 # us as a Perl-Unicode string (already-decoded response content). Decoding it
23 # directly dies with "Wide character in subroutine entry" on any non-ASCII
24 # byte, so UTF-8-encode first -- same convention as ToolCall::_args_kwargs
25 # and Role::JSON's decode_json_text. All three strategies route through this
26 # closure, so the fence/substring candidates are encoded here too.
27 30 my $r = eval { decode_json( encode_utf8($s) ) };
28 30 ▶ return $@ ? undef : $r;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 6 24 30
29 27 };
30  
31 27 ▶ if ( my $r = $try->($text) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($text)) 21 6 27
32 21 return $r;
33 }
34 6 ▶ if ( $text =~ /```(?:json)?\s*(.*?)\s*```/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /```(?:json)?\s*(.*?)\s*```/s) 2 4 6
35 2 ▶ if ( my $r = $try->($1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($1)) 2 0 2
36 2 return $r;
37 }
38 }
39 4 ▶ if ( $text =~ /(\{.*\})/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(\{.*\})/s) 1 3 4
40 1 my $candidate = $1;
41 1 ▶ if ( my $r = $try->($candidate) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($candidate)) 1 0 1
42 1 return $r;
43 }
44 0 ▶ while ( length $candidate > 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $candidate > 2) 0 0 0
45 0 my $before = length $candidate;
46 0 ▶ $candidate =~ s/\}[^}]*$/\}/ or last;
Branch: 1 branch
BranchTrueFalseTotal
$candidate =~ s/\}[^}]*$/}/ or last 0 0 0
47 # The substitution collapses trailing junk after the final '}' into that
48 # '}'. When the candidate already ends at '}' (which the greedy capture
49 # above guarantees), it is a no-op that still reports a successful
50 # substitution, so `or last` never fires -- an unbalanced candidate such
51 # as '{{"a":1}' would spin here forever and wedge the async event loop.
52 # Bail the moment the length stops shrinking: the method must terminate
53 # (returning undef), never hang. -- karr k161
54 0 ▶ last unless length $candidate < $before;
Branch: 1 branch
BranchTrueFalseTotal
unless length $candidate < $before 0 0 0
55 0 ▶ if ( my $r = $try->($candidate) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = &$try($candidate)) 0 0 0
56 0 return $r;
57 }
58 }
59 }
60 3 return undef;
61 }
62  
63  
64  
65 178 1;
66  
67 __END__
68  
69 =pod
70  
71 =encoding UTF-8
72  
73 =head1 NAME
74  
75 Langertha::Role::ResponseFormat - Role for an engine where you can specify structured output
76  
77 =head1 VERSION
78  
79 version 0.503
80  
81 =head2 decode_loose_json
82  
83 my $data = $engine->decode_loose_json($text);
84  
85 Tolerant JSON decoder for structured-output responses where providers
86 sometimes wrap the payload in code fences or surrounding prose. Tries:
87  
88 =over
89  
90 =item 1. Decode the whole text as JSON.
91  
92 =item 2. Strip C<```json ... ```> code fences and decode the inner block.
93  
94 =item 3. Decode the first balanced C<{...}> substring.
95  
96 =back
97  
98 Returns the decoded value (typically a HashRef) on success, or C<undef>
99 if all strategies fail. Override in an engine subclass when a provider
100 needs a custom strategy (e.g. always-prose-wrapped output).
101  
102 =head2 response_format
103  
104 A HashRef specifying the structured output format for the response. The exact
105 structure depends on the engine. For OpenAI-compatible engines this is typically
106 C<{ type => 'json_object' }> or a JSON Schema definition. Optional.
107  
108 =head1 SEE ALSO
109  
110 =over
111  
112 =item * L<Langertha::Role::Chat> - Chat functionality that uses response format
113  
114 =item * L<Langertha::Role::OpenAICompatible> - OpenAI-compatible engines that support this role
115  
116 =back
117  
118 =head1 SUPPORT
119  
120 =head2 Issues
121  
122 Please report bugs and feature requests on GitHub at
123 L<https://github.com/Getty/langertha/issues>.
124  
125 =head2 IRC
126  
127 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
128  
129 =head1 CONTRIBUTING
130  
131 Contributions are welcome! Please fork the repository and submit a pull request.
132  
133 =head1 AUTHOR
134  
135 Torsten Raudssus <getty@cpan.org>
136  
137 =head1 COPYRIGHT AND LICENSE
138  
139 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
140  
141 This is free software; you can redistribute it and/or modify it under
142 the same terms as the Perl 5 programming language system itself.
143  
144 =cut
« blib/lib/Langertha/ToolChoice.pm ↑ Summary blib/lib/Langertha/HTTP/BoundedDecode.pm »