| 1 | package Langertha::Engine::Groq; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: GroqCloud API | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 23 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | # StreamUsage: stream_options.include_usage is in the chat completion API | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # reference; x_groq.usage comes on the last chunk either way (ADR 0038). | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 23 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||||||||||
| 12 | Transcription | ||||||||||||||||||||||||||||||||||||||||||
| 13 | Tools | ||||||||||||||||||||||||||||||||||||||||||
| 14 | StreamUsage | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | sub _build_api_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | return $ENV{LANGERTHA_GROQ_API_KEY} | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | || croak "".(ref $self)." requires LANGERTHA_GROQ_API_KEY or api_key set"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'LANGERTHA_GROQ_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_GROQ_API_KEY or api_key set')
MC/DC0%0/1
$ENV{'LANGERTHA_GROQ_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_GROQ_API_KEY or api_key set')
$ENV{'LANGERTHA_GROQ_API_KEY'}
Truth table50%1/2
A
A= $ENV{'LANGERTHA_GROQ_API_KEY'}
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | has '+url' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 25 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 20 | ▶ | default => sub { 'https://api.groq.com/openai/v1' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 23 | ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub default_model { croak "".(ref $_[0])." requires a default_model" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | sub default_transcription_model { 'whisper-large-v3' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_transcription_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 53 | ▶ | sub _build_supported_operations {[qw( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | createChatCompletion | ||||||||||||||||||||||||||||||||||||||||||
| 35 | createTranscription | ||||||||||||||||||||||||||||||||||||||||||
| 36 | )]} | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # karr #148 / #184: Groq rejects a JSON response_format combined with tool use -- | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # BOTH json_object and json_schema 400 alongside tools, with the same "json mode | ||||||||||||||||||||||||||||||||||||||||||
| 40 | # cannot be combined with tool/function calling" message (live-verified | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # 2026-09-19). Its Structured Outputs (json_schema) additionally reject streaming; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # json_object + streaming is not disproven and is left through. This is a | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # Groq-platform property that holds across the models it serves, so an all-models | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # matcher (qr//) expresses it on the model-scoped exclusion seam | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # (Langertha::Role::Chat). There is no shared gpt-oss base rule (removed k184), | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # so this all-models rule is the only exclusion on Groq's own gpt-oss route. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # Consulted by chat_f (streaming => 0) and chat_stream_realtime_f (streaming => 1). | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 40 | ▶ | sub model_capability_exclusions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model_capability_exclusions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 40 | qr// => \&_exclude_json_schema_with_tools_or_streaming, | |||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 40 | ▶ | sub _exclude_json_schema_with_tools_or_streaming { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_exclude_json_schema_with_tools_or_streaming: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 40 | my ( $self, %request ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 40 | my $rf = $request{response_format}; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 40 | ▶ | my $type = ( ref $rf eq 'HASH' ) ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table50%1/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | # Streaming: only json_schema is live-confirmed to 400 (json_object + streaming | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # is not disproven, so it is left through). | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 40 | ▶ | if ( $request{streaming} && $type eq 'json_schema' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$request{"streaming"} and $type eq "json_schema"
MC/DC100%2/2
$request{"streaming"} and $type eq "json_schema"
$request{"streaming"}
$type eq "json_schema"
Truth table100%3/3
A and B
A= $request{"streaming"}B= $type eq "json_schema"
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | croak "".(ref $self)." cannot combine response_format json_schema with " | |||||||||||||||||||||||||||||||||||||||||
| 62 | ."streaming: Groq Structured Outputs do not support streaming and the " | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ."API rejects this with HTTP 400. Use the non-streaming chat_f for " | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ."json_schema output."; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # Tools: BOTH json_object and json_schema 400 alongside tools. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 38 | ▶ | if ( $request{has_tools} && ( $type eq 'json_schema' || $type eq 'json_object' ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$request{"has_tools"} and $type eq "json_schema" || $type eq "json_object"
$type eq "json_schema" or $type eq "json_object"
MC/DC100%3/3
$request{"has_tools"} and $type eq "json_schema" || $type eq "json_object"
$request{"has_tools"}
$type eq "json_schema"
$type eq "json_object"
Truth table100%4/4
A and B or C
A= $request{"has_tools"}B= $type eq "json_schema"C= $type eq "json_object"
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 8 | croak "".(ref $self)." cannot combine tools with a JSON response_format " | |||||||||||||||||||||||||||||||||||||||||
| 69 | ."(json_object or json_schema) in one request: Groq rejects json mode " | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ."combined with tool/function calling with HTTP 400. Send tools or a JSON " | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ."response_format, not both."; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 30 | return; | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # image_input (k266, ADR 0019 k266 Update): the only Groq vision model is | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # qwen/qwen3.8-27b (console.groq.com/docs/vision, llm-advisor, docs only, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # 2026-09-25); the catch-all first row clears the flag for every other id. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # Groq has no default model: building chat_model croaks, and the layer-3 walk | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # reads chat_model. With no model configured the table is therefore empty (so | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # supports() keeps answering instead of croaking, as it did before k266) and | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # the around below makes no image_input claim in its place. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 412 | ▶ | sub _has_configured_model { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_has_configured_model: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 412 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 412 | ▶ | return $self->has_chat_model || $self->has_model; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->has_chat_model || $self->has_model
MC/DC100%2/2
$self->has_chat_model || $self->has_model
$self->has_chat_model
$self->has_model
Truth table100%3/3
A || B
A= $self->has_chat_modelB= $self->has_model
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 206 | ▶ | sub model_capability_corrections { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
model_capability_corrections: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 206 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 206 | ▶ | return () unless $self->_has_configured_model; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 204 | qr/\A/ => { image_input => 0 }, | |||||||||||||||||||||||||||||||||||||||||
| 94 | 'qwen/qwen3.8-27b' => { image_input => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 206 | ▶ | around engine_capabilities => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 206 | my ( $orig, $self, @rest ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 206 | my $caps = $self->$orig(@rest); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 206 | ▶ | delete $caps->{image_input} unless $self->_has_configured_model; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 206 | return $caps; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Groq reports a stream's usage under x_groq.usage on its last chunk instead | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # of a top-level usage (Groq API reference / SDK chunk type; not live-verified). | ||||||||||||||||||||||||||||||||||||||||||
| 107 | # One provider's spelling, so it is read here (ADR 0018 tier 3), guarded by the | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # canonical predicate: a top-level usage, when Groq sends one, wins. -- k298 | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 14 | ▶ | around parse_stream_chunk => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 14 | my ( $orig, $self, $data, @rest ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 14 | my $chunk = $self->$orig( $data, @rest ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 14 | ▶ | return $chunk if $chunk && $chunk->has_usage; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$chunk and $chunk->has_usage
MC/DC50%1/2
$chunk and $chunk->has_usage
$chunk
$chunk->has_usage
Truth table66%2/3
A and B
A= $chunkB= $chunk->has_usage
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 13 | ▶ | my $x_groq = ref $data eq 'HASH' ? $data->{x_groq} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 13 | ▶ | my %usage = $self->_openai_stream_usage_kwargs( ref $x_groq eq 'HASH' ? $x_groq->{usage} : undef ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 13 | ▶ | return $chunk unless %usage; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | return $chunk->meta->clone_object( $chunk, %usage ) if $chunk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | require Langertha::Stream::Chunk; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | return Langertha::Stream::Chunk->new( content => '', raw => $data, is_final => 0, %usage ); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 23 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||||||||||||||||