Open History to see the last commit
1import type { Modality, ModelReasoning } from '@tanstack/ai'
2import type { ModelReasoningCapability } from '@tanstack/ai/adapter-internals'
3import type { GroqTextProviderOptions } from './text/text-provider-options'
4import type { GroqTTSProviderOptions } from './audio/tts-provider-options'
5
6// Reasoning shapes: the levels a model has for `chat({ reasoning })`, and
7// the provider value to send for each. Each model points at one of them.
8const EFFORT_LOW_TO_HIGH = {
9 levels: { low: 'low', medium: 'medium', high: 'high' },
10 budget: false,
11} as const
12const EFFORT_NONE_DEFAULT = {
13 levels: { off: 'none', high: 'default' },
14 budget: false,
15} as const
16
17/**
18 * Internal metadata structure describing a Groq model's capabilities and pricing.
19 */
20interface ModelMeta<TProviderOptions = unknown> {
21 name: string
22 context_window?: number
23 max_completion_tokens?: number
24 pricing: {
25 input?: { normal: number; cached?: number }
26 output?: { normal: number }
27 }
28 supports: {
29 input: Array<'text' | 'image' | 'audio'>
30 output: Array<'text' | 'audio'>
31 endpoints: Array<'chat' | 'tts' | 'transcription' | 'batch'>
32
33 features: Array<
34 | 'streaming'
35 | 'tools'
36 | 'json_object'
37 | 'browser_search'
38 | 'code_execution'
39 | 'reasoning'
40 | 'content_moderation'
41 | 'json_schema'
42 | 'vision'
43 >
44 tools?: ReadonlyArray<never>
45 }
46 /**
47 * Type-level description of which provider options this model supports.
48 */
49 providerOptions?: TProviderOptions
50 /** The model's data for `chat({ reasoning })`. None: it does not reason. */
51 reasoning?: ModelReasoning
52}
53
54const LLAMA_3_3_70B_VERSATILE = {
55 name: 'llama-3.3-70b-versatile',
56 context_window: 131_072,
57 max_completion_tokens: 32_768,
58 pricing: {
59 input: {
60 normal: 0.59,
61 },
62 output: {
63 normal: 0.79,
64 },
65 },
66 supports: {
67 input: ['text'],
68 output: ['text'],
69 endpoints: ['chat'],
70 features: ['streaming', 'tools', 'json_object'],
71 tools: [] as const,
72 },
73} as const satisfies ModelMeta<GroqTextProviderOptions>
74
75const LLAMA_4_MAVERICK_17B_128E_INSTRUCT = {
76 name: 'meta-llama/llama-4-maverick-17b-128e-instruct',
77 context_window: 131_072,
78 max_completion_tokens: 8_192,
79 pricing: {
80 input: {
81 normal: 0.2,
82 },
83 output: {
84 normal: 0.6,
85 },
86 },
87 supports: {
88 input: ['text', 'image'],
89 output: ['text'],
90 endpoints: ['chat'],
91 features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
92 tools: [] as const,
93 },
94} as const satisfies ModelMeta<GroqTextProviderOptions>
95
96const LLAMA_4_SCOUT_17B_16E_INSTRUCT = {
97 name: 'meta-llama/llama-4-scout-17b-16e-instruct',
98 context_window: 131_072,
99 max_completion_tokens: 8_192,
100 pricing: {
101 input: {
102 normal: 0.05,
103 },
104 output: {
105 normal: 0.08,
106 },
107 },
108 supports: {
109 input: ['text', 'image'],
110 output: ['text'],
111 endpoints: ['chat'],
112 features: ['streaming', 'tools', 'json_object'],
113 tools: [] as const,
114 },
115} as const satisfies ModelMeta<GroqTextProviderOptions>
116
117const LLAMA_GUARD_4_12B = {
118 name: 'meta-llama/llama-guard-4-12b',
119 context_window: 131_072,
120 max_completion_tokens: 1024,
121 pricing: {
122 input: {
123 normal: 0.2,
124 },
125 output: {
126 normal: 0.2,
127 },
128 },
129 supports: {
130 input: ['text', 'image'],
131 output: ['text'],
132 endpoints: ['chat'],
133 features: ['streaming', 'json_object', 'content_moderation', 'vision'],
134 tools: [] as const,
135 },
136} as const satisfies ModelMeta<GroqTextProviderOptions>
137
138const LLAMA_PROMPT_GUARD_2_86M = {
139 name: 'meta-llama/llama-prompt-guard-2-86m',
140 context_window: 512,
141 max_completion_tokens: 512,
142 pricing: {
143 input: {
144 normal: 0.04,
145 },
146 output: {
147 normal: 0.04,
148 },
149 },
150 supports: {
151 input: ['text'],
152 output: ['text'],
153 endpoints: ['chat'],
154 features: ['streaming', 'content_moderation', 'json_object'],
155 tools: [] as const,
156 },
157} as const satisfies ModelMeta<GroqTextProviderOptions>
158
159const LLAMA_3_1_8B_INSTANT = {
160 name: 'llama-3.1-8b-instant',
161 context_window: 131_072,
162 max_completion_tokens: 131_072,
163 pricing: {
164 input: {
165 normal: 0.05,
166 },
167 output: {
168 normal: 0.08,
169 },
170 },
171 supports: {
172 input: ['text'],
173 output: ['text'],
174 endpoints: ['chat'],
175 features: ['streaming', 'json_object', 'tools'],
176 tools: [] as const,
177 },
178} as const satisfies ModelMeta<GroqTextProviderOptions>
179
180const LLAMA_PROMPT_GUARD_2_22M = {
181 name: 'meta-llama/llama-prompt-guard-2-22m',
182 context_window: 512,
183 max_completion_tokens: 512,
184 pricing: {
185 input: {
186 normal: 0.03,
187 },
188 output: {
189 normal: 0.03,
190 },
191 },
192 supports: {
193 input: ['text'],
194 output: ['text'],
195 endpoints: ['chat'],
196 features: ['streaming', 'content_moderation'],
197 tools: [] as const,
198 },
199} as const satisfies ModelMeta<GroqTextProviderOptions>
200
201const GPT_OSS_120B = {
202 name: 'openai/gpt-oss-120b',
203 context_window: 131_072,
204 max_completion_tokens: 65_536,
205 pricing: {
206 input: {
207 normal: 0.15,
208 cached: 0.075,
209 },
210 output: {
211 normal: 0.6,
212 },
213 },
214 supports: {
215 input: ['text'],
216 output: ['text'],
217 endpoints: ['chat'],
218 features: [
219 'streaming',
220 'json_object',
221 'json_schema',
222 'tools',
223 'browser_search',
224 'code_execution',
225 'reasoning',
226 ],
227 tools: [] as const,
228 },
229 reasoning: EFFORT_LOW_TO_HIGH,
230} as const satisfies ModelMeta<GroqTextProviderOptions>
231
232const GPT_OSS_SAFEGUARD_20B = {
233 name: 'openai/gpt-oss-safeguard-20b',
234 context_window: 131_072,
235 max_completion_tokens: 65_536,
236 pricing: {
237 input: {
238 normal: 0.075,
239 cached: 0.037,
240 },
241 output: {
242 normal: 0.3,
243 },
244 },
245 supports: {
246 input: ['text'],
247 output: ['text'],
248 endpoints: ['chat'],
249 features: [
250 'streaming',
251 'tools',
252 'browser_search',
253 'code_execution',
254 'json_object',
255 'json_schema',
256 'reasoning',
257 'content_moderation',
258 ],
259 tools: [] as const,
260 },
261 reasoning: EFFORT_LOW_TO_HIGH,
262} as const satisfies ModelMeta<GroqTextProviderOptions>
263
264const GPT_OSS_20B = {
265 name: 'openai/gpt-oss-20b',
266 context_window: 131_072,
267 max_completion_tokens: 65_536,
268 pricing: {
269 input: {
270 normal: 0.075,
271 cached: 0.037,
272 },
273 output: {
274 normal: 0.3,
275 },
276 },
277 supports: {
278 input: ['text'],
279 output: ['text'],
280 endpoints: ['chat'],
281 features: [
282 'streaming',
283 'browser_search',
284 'code_execution',
285 'json_object',
286 'json_schema',
287 'reasoning',
288 'tools',
289 ],
290 tools: [] as const,
291 },
292 reasoning: EFFORT_LOW_TO_HIGH,
293} as const satisfies ModelMeta<GroqTextProviderOptions>
294
295const KIMI_K2_INSTRUCT_0905 = {
296 name: 'moonshotai/kimi-k2-instruct-0905',
297 context_window: 262_144,
298 max_completion_tokens: 16_384,
299 pricing: {
300 input: {
301 normal: 1,
302 cached: 0.5,
303 },
304 output: {
305 normal: 3,
306 },
307 },
308 supports: {
309 input: ['text'],
310 output: ['text'],
311 endpoints: ['chat'],
312 features: ['streaming', 'tools', 'json_object', 'json_schema'],
313 tools: [] as const,
314 },
315} as const satisfies ModelMeta<GroqTextProviderOptions>
316
317const QWEN3_32B = {
318 name: 'qwen/qwen3-32b',
319 context_window: 131_072,
320 max_completion_tokens: 40_960,
321 pricing: {
322 input: {
323 normal: 0.29,
324 },
325 output: {
326 normal: 0.59,
327 },
328 },
329 supports: {
330 input: ['text'],
331 output: ['text'],
332 endpoints: ['chat'],
333 features: ['streaming', 'json_object', 'tools', 'reasoning'],
334 tools: [] as const,
335 },
336 reasoning: EFFORT_NONE_DEFAULT,
337} as const satisfies ModelMeta<GroqTextProviderOptions>
338
339const GROQ_CHAT_MODEL_META = [
340 LLAMA_3_1_8B_INSTANT,
341 LLAMA_3_3_70B_VERSATILE,
342 LLAMA_4_MAVERICK_17B_128E_INSTRUCT,
343 LLAMA_4_SCOUT_17B_16E_INSTRUCT,
344 LLAMA_GUARD_4_12B,
345 LLAMA_PROMPT_GUARD_2_86M,
346 LLAMA_PROMPT_GUARD_2_22M,
347 GPT_OSS_20B,
348 GPT_OSS_120B,
349 GPT_OSS_SAFEGUARD_20B,
350 KIMI_K2_INSTRUCT_0905,
351 QWEN3_32B,
352] as const
353
354/**
355 * `true` when structured output must send JSON mode (`json_object`).
356 * The model declares `json_object` and does not declare `json_schema`.
357 */
358export function groqModelPrefersJsonObject(model: string): boolean {
359 const found = GROQ_CHAT_MODEL_META.find((item) => item.name === model)
360 if (!found) return false
361 const features: ReadonlyArray<string> = found.supports.features
362 return features.includes('json_object') && !features.includes('json_schema')
363}
364
365/**
366 * All supported Groq chat model identifiers.
367 */
368export const GROQ_CHAT_MODELS = [
369 LLAMA_3_1_8B_INSTANT.name,
370 LLAMA_3_3_70B_VERSATILE.name,
371 LLAMA_4_MAVERICK_17B_128E_INSTRUCT.name,
372 LLAMA_4_SCOUT_17B_16E_INSTRUCT.name,
373 LLAMA_GUARD_4_12B.name,
374 LLAMA_PROMPT_GUARD_2_86M.name,
375 LLAMA_PROMPT_GUARD_2_22M.name,
376 GPT_OSS_20B.name,
377 GPT_OSS_120B.name,
378 GPT_OSS_SAFEGUARD_20B.name,
379 KIMI_K2_INSTRUCT_0905.name,
380 QWEN3_32B.name,
381] as const
382
383/**
384 * Union type of all supported Groq chat model names.
385 */
386export type GroqChatModels = (typeof GROQ_CHAT_MODELS)[number]
387
388/**
389 * Type-only map from Groq chat model name to its supported input modalities.
390 */
391export type GroqModelInputModalitiesByName = {
392 [LLAMA_3_1_8B_INSTANT.name]: typeof LLAMA_3_1_8B_INSTANT.supports.input
393 [LLAMA_3_3_70B_VERSATILE.name]: typeof LLAMA_3_3_70B_VERSATILE.supports.input
394 [LLAMA_4_MAVERICK_17B_128E_INSTRUCT.name]: typeof LLAMA_4_MAVERICK_17B_128E_INSTRUCT.supports.input
395 [LLAMA_4_SCOUT_17B_16E_INSTRUCT.name]: typeof LLAMA_4_SCOUT_17B_16E_INSTRUCT.supports.input
396 [LLAMA_GUARD_4_12B.name]: typeof LLAMA_GUARD_4_12B.supports.input
397 [LLAMA_PROMPT_GUARD_2_86M.name]: typeof LLAMA_PROMPT_GUARD_2_86M.supports.input
398 [LLAMA_PROMPT_GUARD_2_22M.name]: typeof LLAMA_PROMPT_GUARD_2_22M.supports.input
399 [GPT_OSS_20B.name]: typeof GPT_OSS_20B.supports.input
400 [GPT_OSS_120B.name]: typeof GPT_OSS_120B.supports.input
401 [GPT_OSS_SAFEGUARD_20B.name]: typeof GPT_OSS_SAFEGUARD_20B.supports.input
402 [KIMI_K2_INSTRUCT_0905.name]: typeof KIMI_K2_INSTRUCT_0905.supports.input
403 [QWEN3_32B.name]: typeof QWEN3_32B.supports.input
404}
405
406/**
407 * Runtime map from chat model name to its supported input modalities, for the
408 * text adapter's `inputModalities`. `satisfies` keeps it equal to
409 * {@link GroqModelInputModalitiesByName}. An unknown name gives `undefined`.
410 */
411export const GROQ_MODEL_INPUT_MODALITIES: Readonly<
412 Record<string, ReadonlyArray<Modality>>
413> = {
414 [LLAMA_3_1_8B_INSTANT.name]: LLAMA_3_1_8B_INSTANT.supports.input,
415 [LLAMA_3_3_70B_VERSATILE.name]: LLAMA_3_3_70B_VERSATILE.supports.input,
416 [LLAMA_4_MAVERICK_17B_128E_INSTRUCT.name]:
417 LLAMA_4_MAVERICK_17B_128E_INSTRUCT.supports.input,
418 [LLAMA_4_SCOUT_17B_16E_INSTRUCT.name]:
419 LLAMA_4_SCOUT_17B_16E_INSTRUCT.supports.input,
420 [LLAMA_GUARD_4_12B.name]: LLAMA_GUARD_4_12B.supports.input,
421 [LLAMA_PROMPT_GUARD_2_86M.name]: LLAMA_PROMPT_GUARD_2_86M.supports.input,
422 [LLAMA_PROMPT_GUARD_2_22M.name]: LLAMA_PROMPT_GUARD_2_22M.supports.input,
423 [GPT_OSS_20B.name]: GPT_OSS_20B.supports.input,
424 [GPT_OSS_120B.name]: GPT_OSS_120B.supports.input,
425 [GPT_OSS_SAFEGUARD_20B.name]: GPT_OSS_SAFEGUARD_20B.supports.input,
426 [KIMI_K2_INSTRUCT_0905.name]: KIMI_K2_INSTRUCT_0905.supports.input,
427 [QWEN3_32B.name]: QWEN3_32B.supports.input,
428} satisfies GroqModelInputModalitiesByName
429
430/**
431 * Type-only map from Groq chat model name to its provider options type.
432 */
433export type GroqChatModelProviderOptionsByName = {
434 [K in (typeof GROQ_CHAT_MODELS)[number]]: GroqTextProviderOptions
435}
436
437/**
438 * Type-only map from Groq chat model name to its supported provider tools.
439 * Groq exposes no provider-specific tool factories, so every model gets an
440 * empty tuple. This ensures that passing an Anthropic/OpenAI ProviderTool to
441 * a Groq adapter produces a compile-time type error.
442 */
443export type GroqChatModelToolCapabilitiesByName = {
444 [LLAMA_3_1_8B_INSTANT.name]: typeof LLAMA_3_1_8B_INSTANT.supports.tools
445 [LLAMA_3_3_70B_VERSATILE.name]: typeof LLAMA_3_3_70B_VERSATILE.supports.tools
446 [LLAMA_4_MAVERICK_17B_128E_INSTRUCT.name]: typeof LLAMA_4_MAVERICK_17B_128E_INSTRUCT.supports.tools
447 [LLAMA_4_SCOUT_17B_16E_INSTRUCT.name]: typeof LLAMA_4_SCOUT_17B_16E_INSTRUCT.supports.tools
448 [LLAMA_GUARD_4_12B.name]: typeof LLAMA_GUARD_4_12B.supports.tools
449 [LLAMA_PROMPT_GUARD_2_86M.name]: typeof LLAMA_PROMPT_GUARD_2_86M.supports.tools
450 [LLAMA_PROMPT_GUARD_2_22M.name]: typeof LLAMA_PROMPT_GUARD_2_22M.supports.tools
451 [GPT_OSS_20B.name]: typeof GPT_OSS_20B.supports.tools
452 [GPT_OSS_120B.name]: typeof GPT_OSS_120B.supports.tools
453 [GPT_OSS_SAFEGUARD_20B.name]: typeof GPT_OSS_SAFEGUARD_20B.supports.tools
454 [KIMI_K2_INSTRUCT_0905.name]: typeof KIMI_K2_INSTRUCT_0905.supports.tools
455 [QWEN3_32B.name]: typeof QWEN3_32B.supports.tools
456}
457
458/**
459 * Type-only map from Groq TTS model name to its provider options type.
460 */
461export type GroqTTSModelProviderOptionsByName = {
462 [K in GroqTTSModel]: GroqTTSProviderOptions
463}
464
465/**
466 * Resolves the provider options type for a specific Groq model.
467 * Checks TTS models first, then chat models, then falls back to generic options.
468 */
469export type ResolveProviderOptions<TModel extends string> =
470 TModel extends GroqTTSModel
471 ? GroqTTSProviderOptions
472 : TModel extends keyof GroqChatModelProviderOptionsByName
473 ? GroqChatModelProviderOptionsByName[TModel]
474 : GroqTextProviderOptions
475
476/**
477 * Resolve input modalities for a specific model.
478 * If the model has explicit modalities in the map, use those; otherwise use text only.
479 */
480export type ResolveInputModalities<TModel extends string> =
481 TModel extends keyof GroqModelInputModalitiesByName
482 ? GroqModelInputModalitiesByName[TModel]
483 : readonly ['text']
484
485/**
486 * All supported Groq transcription model identifiers.
487 */
488export const GROQ_TRANSCRIPTION_MODELS = [
489 'whisper-large-v3-turbo',
490 'whisper-large-v3',
491] as const
492
493/**
494 * Union type of all supported Groq transcription model names.
495 */
496export type GroqTranscriptionModel = (typeof GROQ_TRANSCRIPTION_MODELS)[number]
497
498// ============================================================================
499// TTS Models
500// ============================================================================
501
502const ORPHEUS_V1_ENGLISH = {
503 name: 'canopylabs/orpheus-v1-english',
504 pricing: {
505 input: {
506 normal: 22,
507 },
508 },
509 supports: {
510 input: ['text'],
511 output: ['audio'],
512 endpoints: ['tts'],
513 features: [],
514 },
515} as const satisfies ModelMeta<GroqTTSProviderOptions>
516
517const ORPHEUS_ARABIC_SAUDI = {
518 name: 'canopylabs/orpheus-arabic-saudi',
519 pricing: {
520 input: {
521 normal: 40,
522 },
523 },
524 supports: {
525 input: ['text'],
526 output: ['audio'],
527 endpoints: ['tts'],
528 features: [],
529 },
530} as const satisfies ModelMeta<GroqTTSProviderOptions>
531
532/**
533 * All supported Groq TTS model identifiers.
534 */
535export const GROQ_TTS_MODELS = [
536 ORPHEUS_V1_ENGLISH.name,
537 ORPHEUS_ARABIC_SAUDI.name,
538] as const
539
540/**
541 * Union type of all supported Groq TTS model names.
542 */
543export type GroqTTSModel = (typeof GROQ_TTS_MODELS)[number]
544
545// Reasoning
546//
547// Each model's `reasoning` field holds its data for `chat({ reasoning })`: the
548// levels it has, with the provider value for each, and whether it takes a
549// thinking token budget. The data comes from models.dev. The generator that
550// writes it comes with the model catalog later. Until then, the sync scripts
551// keep these fields as they are.
552
553const GROQ_REASONING = {
554 [GPT_OSS_20B.name]: GPT_OSS_20B.reasoning,
555 [GPT_OSS_120B.name]: GPT_OSS_120B.reasoning,
556 [GPT_OSS_SAFEGUARD_20B.name]: GPT_OSS_SAFEGUARD_20B.reasoning,
557 [QWEN3_32B.name]: QWEN3_32B.reasoning,
558}
559
560/**
561 * Each chat model's reasoning levels, and whether it takes a token budget,
562 * for `chat({ reasoning })`. Derived from the `reasoning` field. A model
563 * that is not here does not reason.
564 */
565export type GroqModelReasoningByName = {
566 [K in keyof typeof GROQ_REASONING]: ModelReasoningCapability<
567 (typeof GROQ_REASONING)[K]
568 >
569}
570
571/**
572 * Runtime map from chat model name to its reasoning data, for the text adapter.
573 * An unknown name gives `undefined`: the adapter sends no reasoning field.
574 */
575export const GROQ_MODEL_REASONING: Readonly<Record<string, ModelReasoning>> =
576 GROQ_REASONING