Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

groq.ts

152 lines4.7 KB

1/**
2 * Groq — official OpenAPI spec bundled in Groq's Stainless-generated Python
3 * SDK: `scripts/mock` embeds it as base64+gzip `EMBEDDED_SPEC` (refreshed on
4 * every SDK codegen). `.stats.yml` stopped carrying `openapi_spec_url` on
5 * 2026-08-11. The hinted console URL is docs, not a spec.
6 */
7import type { Activity } from '#/db/schema.ts'
8import {
9 classifyOpenAiCompat,
10 listOpenAiCompatibleModels,
11} from '../openai-compat.ts'
12import { GROQ_PRICING_URL, groqModelPricing } from '../groq-pricing.ts'
13import { docsReport, docsRun, tryDocs, unavailable } from '../model-facts.ts'
14import {
15 GROQ_DEPRECATIONS_URL,
16 loadDeprecations,
17 parseGroqDeprecations,
18} from '../deprecations.ts'
19import {
20 groqModelReasoning,
21 listsReasoning,
22 overlayModelFacts,
23} from '../reasoning-config.ts'
24import { groqModelServerTools } from '../server-tools.ts'
25import { groqGenerationEndpointId, groqModelActivity } from '../model-meta.ts'
26import { fetchText, parseGzippedOpenApi } from '../types.ts'
27import { bearerConnect } from '../connect.ts'
28import { namespacedUpstreamIdentity } from '../upstream-model.ts'
29import type {
30 ListModelsResult,
31 ModelInfo,
32 ProviderConfig,
33 ProviderSecrets,
34 SpecFetchResult,
35} from '../types.ts'
36import type { OpenAiCompatModelRow } from '../openai-compat.ts'
37​
38const GROQ_MOCK_URL =
39 'https://raw.githubusercontent.com/groq/groq-python/main/scripts/mock'
40const GROQ_MODELS_URL = 'https://api.groq.com/openai/v1/models'
41​
42/**
43 * Stainless paths are `/openai/v1/...` (server is `https://api.groq.com`).
44 * Strip that prefix and reuse the OpenAI-compat path map. Batches, files,
45 * fine-tunes, models, and rerank classify null.
46 */
47function classify(path: string): Activity | null {
48 const withoutOpenAi = path.startsWith('/openai/')
49 ? path.slice('/openai'.length)
50 : path
51 return classifyOpenAiCompat(withoutOpenAi)
53​
54async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
55 const script = await fetchText(GROQ_MOCK_URL)
56 const blob = script.match(/EMBEDDED_SPEC="([A-Za-z0-9+/=\s]+)"/)?.[1]
57 if (!blob) {
58 throw new Error(`groq: no EMBEDDED_SPEC in ${GROQ_MOCK_URL}`)
59 }
60 const bytes = Uint8Array.from(atob(blob.replace(/\s/g, '')), (c) =>
61 c.charCodeAt(0),
62 )
63 return parseGzippedOpenApi(bytes, 'groq', GROQ_MOCK_URL)
65​
66/**
67 * The maker Groq's listing names in `owned_by` (issue #710), as written.
68 * A row with none states no maker.
69 */
70export function groqMakerFacts(
71 row: Pick<OpenAiCompatModelRow, 'owned_by'>,
72): Partial<ModelInfo> {
73 const maker = row.owned_by?.trim() ?? ''
74 return {
75 maker: maker === '' ? null : maker,
76 factSources: {
77 maker: {
78 derivation: 'listing',
79 sourceUrl: GROQ_MODELS_URL,
80 path: maker === '' ? 'unstated' : 'owned_by',
81 },
82 },
83 }
85​
86async function listModels(
87 env: ProviderSecrets,
88 kv?: KVNamespace,
89): Promise<ListModelsResult> {
90 const listed = await listOpenAiCompatibleModels({
91 providerId: 'groq',
92 url: GROQ_MODELS_URL,
93 env,
94 envVar: 'GROQ_API_KEY',
95 activity: groqModelActivity,
96 extend: (row) => Promise.resolve(groqMakerFacts(row)),
97 })
98 if (listed.models.length === 0) return listed
99 const docs = docsRun()
100 // Pricing reads several docs pages; one failing keeps the stored cards.
101 const [pricing, reasoning, tools] = await Promise.all([
102 tryDocs(docs, GROQ_PRICING_URL, (cached) => groqModelPricing(kv, cached)),
103 groqModelReasoning(
104 kv,
105 docs,
106 listed.models.map((model) => model.rawId),
107 ),
108 groqModelServerTools(kv),
109 ])
110 const deprecations = await loadDeprecations(
111 docs,
112 kv,
113 GROQ_DEPRECATIONS_URL,
114 parseGroqDeprecations,
115 )
116 return {
117 ...listed,
118 models: listed.models.map((model) =>
119 overlayModelFacts(
120 model,
121 pricing ? pricing(model.rawId) : unavailable('pricing'),
122 reasoning(model.rawId, listsReasoning(model.capabilities)),
123 tools(model.rawId),
124 ),
125 ),
126 ...(deprecations ? { deprecations } : {}),
127 docsFailures: docsReport(docs),
128 }
130​
131export const provider: ProviderConfig = {
132 id: 'groq',
133 // `openai/gpt-oss-120b`: the listing names the maker in the id. Groq's own
134 // and older ids (`allam-2-7b`, `whisper-large-v3`) carry no namespace.
135 upstreamModelIdentity: (rawId) =>
136 namespacedUpstreamIdentity(rawId, {
137 derivation: 'listing',
138 sourceUrl: GROQ_MODELS_URL,
139 path: 'data[].id',
140 }),
141 displayName: 'Groq',
142 authEnvVar: 'GROQ_API_KEY',
143 specSourceUrl: GROQ_MOCK_URL,
144 modelsEndpoint: GROQ_MODELS_URL,
145 defaultDerivation: 'upstream-spec',
146 connect: bearerConnect('https://api.groq.com'),
147 fetchSpec,
148 listModels,
149 classify,
150 generationEndpointId: ({ rawId, activity }) =>
151 groqGenerationEndpointId(rawId, activity),