@ai-sdk/groq 2.0.0-canary.9 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  parseProviderOptions,
21
21
  postJsonToApi
22
22
  } from "@ai-sdk/provider-utils";
23
- import { z as z3 } from "zod";
23
+ import { z as z3 } from "zod/v4";
24
24
 
25
25
  // src/convert-to-groq-chat-messages.ts
26
26
  import {
@@ -80,7 +80,7 @@ function convertToGroqChatMessages(prompt) {
80
80
  type: "function",
81
81
  function: {
82
82
  name: part.toolName,
83
- arguments: JSON.stringify(part.args)
83
+ arguments: JSON.stringify(part.input)
84
84
  }
85
85
  });
86
86
  break;
@@ -96,10 +96,23 @@ function convertToGroqChatMessages(prompt) {
96
96
  }
97
97
  case "tool": {
98
98
  for (const toolResponse of content) {
99
+ const output = toolResponse.output;
100
+ let contentValue;
101
+ switch (output.type) {
102
+ case "text":
103
+ case "error-text":
104
+ contentValue = output.value;
105
+ break;
106
+ case "content":
107
+ case "json":
108
+ case "error-json":
109
+ contentValue = JSON.stringify(output.value);
110
+ break;
111
+ }
99
112
  messages.push({
100
113
  role: "tool",
101
114
  tool_call_id: toolResponse.toolCallId,
102
- content: JSON.stringify(toolResponse.result)
115
+ content: contentValue
103
116
  });
104
117
  }
105
118
  break;
@@ -127,22 +140,28 @@ function getResponseMetadata({
127
140
  }
128
141
 
129
142
  // src/groq-chat-options.ts
130
- import { z } from "zod";
143
+ import { z } from "zod/v4";
131
144
  var groqProviderOptions = z.object({
132
- reasoningFormat: z.enum(["parsed", "raw", "hidden"]).nullish(),
145
+ reasoningFormat: z.enum(["parsed", "raw", "hidden"]).optional(),
133
146
  /**
134
147
  * Whether to enable parallel function calling during tool use. Default to true.
135
148
  */
136
- parallelToolCalls: z.boolean().nullish(),
149
+ parallelToolCalls: z.boolean().optional(),
137
150
  /**
138
151
  * A unique identifier representing your end-user, which can help OpenAI to
139
152
  * monitor and detect abuse. Learn more.
140
153
  */
141
- user: z.string().nullish()
154
+ user: z.string().optional(),
155
+ /**
156
+ * Whether to use structured outputs.
157
+ *
158
+ * @default true
159
+ */
160
+ structuredOutputs: z.boolean().optional()
142
161
  });
143
162
 
144
163
  // src/groq-error.ts
145
- import { z as z2 } from "zod";
164
+ import { z as z2 } from "zod/v4";
146
165
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
147
166
  var groqErrorDataSchema = z2.object({
148
167
  error: z2.object({
@@ -178,7 +197,7 @@ function prepareTools({
178
197
  function: {
179
198
  name: tool.name,
180
199
  description: tool.description,
181
- parameters: tool.parameters
200
+ parameters: tool.inputSchema
182
201
  }
183
202
  });
184
203
  }
@@ -231,21 +250,18 @@ function mapGroqFinishReason(finishReason) {
231
250
 
232
251
  // src/groq-chat-language-model.ts
233
252
  var GroqChatLanguageModel = class {
234
- constructor(modelId, settings, config) {
253
+ constructor(modelId, config) {
235
254
  this.specificationVersion = "v2";
236
- this.supportsStructuredOutputs = false;
237
- this.defaultObjectGenerationMode = "json";
255
+ this.supportedUrls = {
256
+ "image/*": [/^https?:\/\/.*$/]
257
+ };
238
258
  this.modelId = modelId;
239
- this.settings = settings;
240
259
  this.config = config;
241
260
  }
242
261
  get provider() {
243
262
  return this.config.provider;
244
263
  }
245
- get supportsImageUrls() {
246
- return !this.settings.downloadImages;
247
- }
248
- getArgs({
264
+ async getArgs({
249
265
  prompt,
250
266
  maxOutputTokens,
251
267
  temperature,
@@ -261,25 +277,27 @@ var GroqChatLanguageModel = class {
261
277
  toolChoice,
262
278
  providerOptions
263
279
  }) {
280
+ var _a, _b;
264
281
  const warnings = [];
282
+ const groqOptions = await parseProviderOptions({
283
+ provider: "groq",
284
+ providerOptions,
285
+ schema: groqProviderOptions
286
+ });
287
+ const structuredOutputs = (_a = groqOptions == null ? void 0 : groqOptions.structuredOutputs) != null ? _a : true;
265
288
  if (topK != null) {
266
289
  warnings.push({
267
290
  type: "unsupported-setting",
268
291
  setting: "topK"
269
292
  });
270
293
  }
271
- if (responseFormat != null && responseFormat.type === "json" && responseFormat.schema != null) {
294
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !structuredOutputs) {
272
295
  warnings.push({
273
296
  type: "unsupported-setting",
274
297
  setting: "responseFormat",
275
- details: "JSON response format schema is not supported"
298
+ details: "JSON response format schema is only supported with structuredOutputs"
276
299
  });
277
300
  }
278
- const groqOptions = parseProviderOptions({
279
- provider: "groq",
280
- providerOptions,
281
- schema: groqProviderOptions
282
- });
283
301
  const {
284
302
  tools: groqTools,
285
303
  toolChoice: groqToolChoice,
@@ -301,10 +319,14 @@ var GroqChatLanguageModel = class {
301
319
  stop: stopSequences,
302
320
  seed,
303
321
  // response format:
304
- response_format: (
305
- // json object response format is not supported for streaming:
306
- stream === false && (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0
307
- ),
322
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? structuredOutputs && responseFormat.schema != null ? {
323
+ type: "json_schema",
324
+ json_schema: {
325
+ schema: responseFormat.schema,
326
+ name: (_b = responseFormat.name) != null ? _b : "response",
327
+ description: responseFormat.description
328
+ }
329
+ } : { type: "json_object" } : void 0,
308
330
  // provider options:
309
331
  reasoning_format: groqOptions == null ? void 0 : groqOptions.reasoningFormat,
310
332
  // messages:
@@ -317,8 +339,11 @@ var GroqChatLanguageModel = class {
317
339
  };
318
340
  }
319
341
  async doGenerate(options) {
320
- var _a, _b, _c, _d, _e;
321
- const { args, warnings } = this.getArgs({ ...options, stream: false });
342
+ var _a, _b, _c, _d, _e, _f, _g;
343
+ const { args, warnings } = await this.getArgs({
344
+ ...options,
345
+ stream: false
346
+ });
322
347
  const body = JSON.stringify(args);
323
348
  const {
324
349
  responseHeaders,
@@ -348,7 +373,6 @@ var GroqChatLanguageModel = class {
348
373
  if (reasoning != null && reasoning.length > 0) {
349
374
  content.push({
350
375
  type: "reasoning",
351
- reasoningType: "text",
352
376
  text: reasoning
353
377
  });
354
378
  }
@@ -356,10 +380,9 @@ var GroqChatLanguageModel = class {
356
380
  for (const toolCall of choice.message.tool_calls) {
357
381
  content.push({
358
382
  type: "tool-call",
359
- toolCallType: "function",
360
383
  toolCallId: (_a = toolCall.id) != null ? _a : generateId(),
361
384
  toolName: toolCall.function.name,
362
- args: toolCall.function.arguments
385
+ input: toolCall.function.arguments
363
386
  });
364
387
  }
365
388
  }
@@ -368,7 +391,8 @@ var GroqChatLanguageModel = class {
368
391
  finishReason: mapGroqFinishReason(choice.finish_reason),
369
392
  usage: {
370
393
  inputTokens: (_c = (_b = response.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
371
- outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0
394
+ outputTokens: (_e = (_d = response.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
395
+ totalTokens: (_g = (_f = response.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0
372
396
  },
373
397
  response: {
374
398
  ...getResponseMetadata(response),
@@ -380,7 +404,7 @@ var GroqChatLanguageModel = class {
380
404
  };
381
405
  }
382
406
  async doStream(options) {
383
- const { args, warnings } = this.getArgs({ ...options, stream: true });
407
+ const { args, warnings } = await this.getArgs({ ...options, stream: true });
384
408
  const body = JSON.stringify({ ...args, stream: true });
385
409
  const { responseHeaders, value: response } = await postJsonToApi({
386
410
  url: this.config.url({
@@ -401,9 +425,12 @@ var GroqChatLanguageModel = class {
401
425
  let finishReason = "unknown";
402
426
  const usage = {
403
427
  inputTokens: void 0,
404
- outputTokens: void 0
428
+ outputTokens: void 0,
429
+ totalTokens: void 0
405
430
  };
406
431
  let isFirstChunk = true;
432
+ let isActiveText = false;
433
+ let isActiveReasoning = false;
407
434
  let providerMetadata;
408
435
  return {
409
436
  stream: response.pipeThrough(
@@ -412,7 +439,10 @@ var GroqChatLanguageModel = class {
412
439
  controller.enqueue({ type: "stream-start", warnings });
413
440
  },
414
441
  transform(chunk, controller) {
415
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
442
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
443
+ if (options.includeRawChunks) {
444
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
445
+ }
416
446
  if (!chunk.success) {
417
447
  finishReason = "error";
418
448
  controller.enqueue({ type: "error", error: chunk.error });
@@ -434,6 +464,7 @@ var GroqChatLanguageModel = class {
434
464
  if (((_a = value.x_groq) == null ? void 0 : _a.usage) != null) {
435
465
  usage.inputTokens = (_b = value.x_groq.usage.prompt_tokens) != null ? _b : void 0;
436
466
  usage.outputTokens = (_c = value.x_groq.usage.completion_tokens) != null ? _c : void 0;
467
+ usage.totalTokens = (_d = value.x_groq.usage.total_tokens) != null ? _d : void 0;
437
468
  }
438
469
  const choice = value.choices[0];
439
470
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
@@ -444,16 +475,28 @@ var GroqChatLanguageModel = class {
444
475
  }
445
476
  const delta = choice.delta;
446
477
  if (delta.reasoning != null && delta.reasoning.length > 0) {
478
+ if (!isActiveReasoning) {
479
+ controller.enqueue({
480
+ type: "reasoning-start",
481
+ id: "reasoning-0"
482
+ });
483
+ isActiveReasoning = true;
484
+ }
447
485
  controller.enqueue({
448
- type: "reasoning",
449
- reasoningType: "text",
450
- text: delta.reasoning
486
+ type: "reasoning-delta",
487
+ id: "reasoning-0",
488
+ delta: delta.reasoning
451
489
  });
452
490
  }
453
491
  if (delta.content != null && delta.content.length > 0) {
492
+ if (!isActiveText) {
493
+ controller.enqueue({ type: "text-start", id: "txt-0" });
494
+ isActiveText = true;
495
+ }
454
496
  controller.enqueue({
455
- type: "text",
456
- text: delta.content
497
+ type: "text-delta",
498
+ id: "txt-0",
499
+ delta: delta.content
457
500
  });
458
501
  }
459
502
  if (delta.tool_calls != null) {
@@ -472,39 +515,45 @@ var GroqChatLanguageModel = class {
472
515
  message: `Expected 'id' to be a string.`
473
516
  });
474
517
  }
475
- if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
518
+ if (((_e = toolCallDelta.function) == null ? void 0 : _e.name) == null) {
476
519
  throw new InvalidResponseDataError({
477
520
  data: toolCallDelta,
478
521
  message: `Expected 'function.name' to be a string.`
479
522
  });
480
523
  }
524
+ controller.enqueue({
525
+ type: "tool-input-start",
526
+ id: toolCallDelta.id,
527
+ toolName: toolCallDelta.function.name
528
+ });
481
529
  toolCalls[index] = {
482
530
  id: toolCallDelta.id,
483
531
  type: "function",
484
532
  function: {
485
533
  name: toolCallDelta.function.name,
486
- arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
534
+ arguments: (_f = toolCallDelta.function.arguments) != null ? _f : ""
487
535
  },
488
536
  hasFinished: false
489
537
  };
490
538
  const toolCall2 = toolCalls[index];
491
- if (((_f = toolCall2.function) == null ? void 0 : _f.name) != null && ((_g = toolCall2.function) == null ? void 0 : _g.arguments) != null) {
539
+ if (((_g = toolCall2.function) == null ? void 0 : _g.name) != null && ((_h = toolCall2.function) == null ? void 0 : _h.arguments) != null) {
492
540
  if (toolCall2.function.arguments.length > 0) {
493
541
  controller.enqueue({
494
- type: "tool-call-delta",
495
- toolCallType: "function",
496
- toolCallId: toolCall2.id,
497
- toolName: toolCall2.function.name,
498
- argsTextDelta: toolCall2.function.arguments
542
+ type: "tool-input-delta",
543
+ id: toolCall2.id,
544
+ delta: toolCall2.function.arguments
499
545
  });
500
546
  }
501
547
  if (isParsableJson(toolCall2.function.arguments)) {
548
+ controller.enqueue({
549
+ type: "tool-input-end",
550
+ id: toolCall2.id
551
+ });
502
552
  controller.enqueue({
503
553
  type: "tool-call",
504
- toolCallType: "function",
505
- toolCallId: (_h = toolCall2.id) != null ? _h : generateId(),
554
+ toolCallId: (_i = toolCall2.id) != null ? _i : generateId(),
506
555
  toolName: toolCall2.function.name,
507
- args: toolCall2.function.arguments
556
+ input: toolCall2.function.arguments
508
557
  });
509
558
  toolCall2.hasFinished = true;
510
559
  }
@@ -515,23 +564,24 @@ var GroqChatLanguageModel = class {
515
564
  if (toolCall.hasFinished) {
516
565
  continue;
517
566
  }
518
- if (((_i = toolCallDelta.function) == null ? void 0 : _i.arguments) != null) {
519
- toolCall.function.arguments += (_k = (_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null ? _k : "";
567
+ if (((_j = toolCallDelta.function) == null ? void 0 : _j.arguments) != null) {
568
+ toolCall.function.arguments += (_l = (_k = toolCallDelta.function) == null ? void 0 : _k.arguments) != null ? _l : "";
520
569
  }
521
570
  controller.enqueue({
522
- type: "tool-call-delta",
523
- toolCallType: "function",
524
- toolCallId: toolCall.id,
525
- toolName: toolCall.function.name,
526
- argsTextDelta: (_l = toolCallDelta.function.arguments) != null ? _l : ""
571
+ type: "tool-input-delta",
572
+ id: toolCall.id,
573
+ delta: (_m = toolCallDelta.function.arguments) != null ? _m : ""
527
574
  });
528
- if (((_m = toolCall.function) == null ? void 0 : _m.name) != null && ((_n = toolCall.function) == null ? void 0 : _n.arguments) != null && isParsableJson(toolCall.function.arguments)) {
575
+ if (((_n = toolCall.function) == null ? void 0 : _n.name) != null && ((_o = toolCall.function) == null ? void 0 : _o.arguments) != null && isParsableJson(toolCall.function.arguments)) {
576
+ controller.enqueue({
577
+ type: "tool-input-end",
578
+ id: toolCall.id
579
+ });
529
580
  controller.enqueue({
530
581
  type: "tool-call",
531
- toolCallType: "function",
532
- toolCallId: (_o = toolCall.id) != null ? _o : generateId(),
582
+ toolCallId: (_p = toolCall.id) != null ? _p : generateId(),
533
583
  toolName: toolCall.function.name,
534
- args: toolCall.function.arguments
584
+ input: toolCall.function.arguments
535
585
  });
536
586
  toolCall.hasFinished = true;
537
587
  }
@@ -539,6 +589,12 @@ var GroqChatLanguageModel = class {
539
589
  }
540
590
  },
541
591
  flush(controller) {
592
+ if (isActiveReasoning) {
593
+ controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
594
+ }
595
+ if (isActiveText) {
596
+ controller.enqueue({ type: "text-end", id: "txt-0" });
597
+ }
542
598
  controller.enqueue({
543
599
  type: "finish",
544
600
  finishReason,
@@ -579,7 +635,8 @@ var groqChatResponseSchema = z3.object({
579
635
  ),
580
636
  usage: z3.object({
581
637
  prompt_tokens: z3.number().nullish(),
582
- completion_tokens: z3.number().nullish()
638
+ completion_tokens: z3.number().nullish(),
639
+ total_tokens: z3.number().nullish()
583
640
  }).nullish()
584
641
  });
585
642
  var groqChatChunkSchema = z3.union([
@@ -611,7 +668,8 @@ var groqChatChunkSchema = z3.union([
611
668
  x_groq: z3.object({
612
669
  usage: z3.object({
613
670
  prompt_tokens: z3.number().nullish(),
614
- completion_tokens: z3.number().nullish()
671
+ completion_tokens: z3.number().nullish(),
672
+ total_tokens: z3.number().nullish()
615
673
  }).nullish()
616
674
  }).nullish()
617
675
  }),
@@ -626,7 +684,7 @@ import {
626
684
  parseProviderOptions as parseProviderOptions2,
627
685
  postFormDataToApi
628
686
  } from "@ai-sdk/provider-utils";
629
- import { z as z4 } from "zod";
687
+ import { z as z4 } from "zod/v4";
630
688
  var groqProviderOptionsSchema = z4.object({
631
689
  language: z4.string().nullish(),
632
690
  prompt: z4.string().nullish(),
@@ -638,19 +696,19 @@ var GroqTranscriptionModel = class {
638
696
  constructor(modelId, config) {
639
697
  this.modelId = modelId;
640
698
  this.config = config;
641
- this.specificationVersion = "v1";
699
+ this.specificationVersion = "v2";
642
700
  }
643
701
  get provider() {
644
702
  return this.config.provider;
645
703
  }
646
- getArgs({
704
+ async getArgs({
647
705
  audio,
648
706
  mediaType,
649
707
  providerOptions
650
708
  }) {
651
709
  var _a, _b, _c, _d, _e;
652
710
  const warnings = [];
653
- const groqOptions = parseProviderOptions2({
711
+ const groqOptions = await parseProviderOptions2({
654
712
  provider: "groq",
655
713
  providerOptions,
656
714
  schema: groqProviderOptionsSchema
@@ -682,7 +740,7 @@ var GroqTranscriptionModel = class {
682
740
  async doGenerate(options) {
683
741
  var _a, _b, _c, _d, _e;
684
742
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
685
- const { formData, warnings } = this.getArgs(options);
743
+ const { formData, warnings } = await this.getArgs(options);
686
744
  const {
687
745
  value: response,
688
746
  responseHeaders,
@@ -756,19 +814,19 @@ function createGroq(options = {}) {
756
814
  })}`,
757
815
  ...options.headers
758
816
  });
759
- const createChatModel = (modelId, settings = {}) => new GroqChatLanguageModel(modelId, settings, {
817
+ const createChatModel = (modelId) => new GroqChatLanguageModel(modelId, {
760
818
  provider: "groq.chat",
761
819
  url: ({ path }) => `${baseURL}${path}`,
762
820
  headers: getHeaders,
763
821
  fetch: options.fetch
764
822
  });
765
- const createLanguageModel = (modelId, settings) => {
823
+ const createLanguageModel = (modelId) => {
766
824
  if (new.target) {
767
825
  throw new Error(
768
826
  "The Groq model function cannot be called with the new keyword."
769
827
  );
770
828
  }
771
- return createChatModel(modelId, settings);
829
+ return createChatModel(modelId);
772
830
  };
773
831
  const createTranscriptionModel = (modelId) => {
774
832
  return new GroqTranscriptionModel(modelId, {
@@ -778,8 +836,8 @@ function createGroq(options = {}) {
778
836
  fetch: options.fetch
779
837
  });
780
838
  };
781
- const provider = function(modelId, settings) {
782
- return createLanguageModel(modelId, settings);
839
+ const provider = function(modelId) {
840
+ return createLanguageModel(modelId);
783
841
  };
784
842
  provider.languageModel = createLanguageModel;
785
843
  provider.chat = createChatModel;