| 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
Condition100%3/3
defined $text and length $text
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 29 | 27 | }; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 27 | ▶ | if ( my $r = $try->($text) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 21 | return $r; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 6 | ▶ | if ( $text =~ /```(?:json)?\s*(.*?)\s*```/s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | if ( my $r = $try->($1) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | return $r; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 4 | ▶ | if ( $text =~ /(\{.*\})/s ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | my $candidate = $1; | |||||||||||||||||||||||||||
| 41 | 1 | ▶ | if ( my $r = $try->($candidate) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 1 | return $r; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | while ( length $candidate > 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | my $before = length $candidate; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | $candidate =~ s/\}[^}]*$/\}/ or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( my $r = $try->($candidate) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||