sarvam-ai-sdk 0.3.0 → 0.3.1

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/README.md CHANGED
@@ -18,6 +18,15 @@ npm i sarvam-ai-sdk ai@6
18
18
  > [!WARNING]
19
19
  > This package only works with Vercel AI-SDK v6, not v7. Make sure to install `ai@6` in your project.
20
20
 
21
+ ### Version Compatibility
22
+
23
+ | Sarvam AI SDK Version | Vercel AI SDK Version |
24
+ |-----------------------|-----------------------|
25
+ | 0.4.x (beta) | 7.x.x (beta) |
26
+ | 0.3.x (current) | 6.x.x (current) |
27
+ | 0.2.x | 5.x.x |
28
+ | 0.1.x | 4.x.x |
29
+
21
30
  ## Provider Instance
22
31
 
23
32
  You can import the default provider instance `sarvam` from `sarvam-ai-sdk`:
@@ -160,29 +169,56 @@ console.log(result.toolResults);
160
169
  ```
161
170
 
162
171
  ## Generate JSON object
172
+ > NB: `generateObject` is deprecated, use `generateText` with `Output` tool.
163
173
 
164
174
  ```ts
165
175
  import { z } from "zod";
166
176
  import { sarvam } from "sarvam-ai-sdk";
167
- import { generateObject } from 'ai';
177
+ import { generateObject } from "ai";
168
178
 
169
179
  const { object } = await generateObject({
170
- model: sarvam("sarvam-30b"),
171
- schemaName: "Recipe",
172
- schemaDescription: "A recipe with a name, ingredients and steps",
173
- schema: z.object({
174
- recipe: z.object({
175
- name: z.string(),
176
- ingredients: z.array(z.string()),
177
- steps: z.array(z.string()),
178
- }),
179
- }),
180
- prompt: 'Generate a South Indian recipe, in Malayalam',
180
+ model: sarvam("sarvam-30b"),
181
+ schemaName: "Recipe",
182
+ schemaDescription: "A recipe with a name, ingredients and steps",
183
+ schema: z.object({
184
+ recipe: z.object({
185
+ name: z.string(),
186
+ ingredients: z.array(z.string()),
187
+ steps: z.array(z.string()),
188
+ }),
189
+ }),
190
+ prompt: "Generate a South Indian recipe, in Malayalam",
181
191
  });
182
192
 
183
193
  console.log(object);
184
194
  ```
185
195
 
196
+ ## Generating Structured Outputs
197
+
198
+ ```ts
199
+ import { z } from "zod";
200
+ import { sarvam } from "sarvam-ai-sdk";
201
+ import { generateText, Output } from "ai";
202
+
203
+ const { output } = await generateText({
204
+ model: sarvam("sarvam-105b"),
205
+ output: Output.object({
206
+ name: "Recipe",
207
+ description: "A recipe with a name, ingredients and steps",
208
+ schema: z.object({
209
+ recipe: z.object({
210
+ name: z.string(),
211
+ ingredients: z.array(z.string()),
212
+ steps: z.array(z.string()),
213
+ }),
214
+ }),
215
+ }),
216
+ prompt: "Generate a South Indian recipe, in Malayalam",
217
+ });
218
+
219
+ console.log(output);
220
+ ```
221
+
186
222
  ## All APIs
187
223
 
188
224
  ```ts
package/dist/index.cjs CHANGED
@@ -135,8 +135,8 @@ function prepareTools({ tools, toolChoice }) {
135
135
  };
136
136
  const sarvamTools = [];
137
137
  for (const tool of finalTools) if (tool.type === "provider") toolWarnings.push({
138
- type: "unsupported-tool",
139
- tool
138
+ type: "unsupported",
139
+ feature: tool.name
140
140
  });
141
141
  else sarvamTools.push({
142
142
  type: "function",
@@ -196,7 +196,6 @@ const chatResponseSchema = zod.default.object({
196
196
  logprobs: zod.default.object({}).nullish(),
197
197
  message: zod.default.object({
198
198
  content: zod.default.string().nullish(),
199
- reasoning: zod.default.string().nullish(),
200
199
  reasoning_content: zod.default.string().nullish(),
201
200
  refusal: zod.default.string().nullish(),
202
201
  tool_calls: zod.default.array(zod.default.object({
@@ -341,7 +340,7 @@ var SarvamChatLanguageModel = class {
341
340
  ...options,
342
341
  stream: false
343
342
  });
344
- const body = JSON.stringify(args);
343
+ const isJSON = options.responseFormat?.type === "json";
345
344
  const { responseHeaders, value: response, rawValue: rawResponse } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
346
345
  url: this.config.url({
347
346
  path: "/chat/completions",
@@ -364,17 +363,16 @@ var SarvamChatLanguageModel = class {
364
363
  type: "text",
365
364
  text: choice.message.content
366
365
  });
367
- const reasoningText = choice.message.reasoning ?? choice.message.reasoning_content;
368
- if (reasoningText) content.push({
366
+ if (choice.message.reasoning_content) content.push({
369
367
  type: "reasoning",
370
- text: reasoningText
368
+ text: choice.message.reasoning_content
371
369
  });
372
- if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
373
- if (options.responseFormat?.type === "json") content.push({
370
+ if (choice.message.tool_calls && choice.message.tool_calls.length > 0) for (const toolCall of choice.message.tool_calls) {
371
+ if (isJSON) content.push({
374
372
  type: "text",
375
- text: choice.message.tool_calls[0].function.arguments
373
+ text: toolCall.function.arguments
376
374
  });
377
- for (const toolCall of choice.message.tool_calls) content.push({
375
+ content.push({
378
376
  type: "tool-call",
379
377
  toolCallId: toolCall.id ?? (this.config.generateId ?? __ai_sdk_provider_utils.generateId)(),
380
378
  toolName: toolCall.function.name,
@@ -384,7 +382,7 @@ var SarvamChatLanguageModel = class {
384
382
  return {
385
383
  content,
386
384
  finishReason: {
387
- unified: mapFinishReason(choice.finish_reason),
385
+ unified: isJSON ? "stop" : mapFinishReason(choice.finish_reason),
388
386
  raw: choice.finish_reason ?? void 0
389
387
  },
390
388
  usage: {
@@ -400,11 +398,18 @@ var SarvamChatLanguageModel = class {
400
398
  reasoning: void 0
401
399
  }
402
400
  },
401
+ providerMetadata: { sarvam: {
402
+ system_fingerprint: response.system_fingerprint,
403
+ service_tier: response.service_tier
404
+ } },
403
405
  warnings,
404
- request: { body },
406
+ request: { body: args },
405
407
  response: {
406
408
  headers: responseHeaders,
407
- body: rawResponse
409
+ body: rawResponse,
410
+ id: response.id ?? void 0,
411
+ modelId: response.model ?? void 0,
412
+ timestamp: response.created ? /* @__PURE__ */ new Date(response.created * 1e3) : void 0
408
413
  }
409
414
  };
410
415
  }
@@ -413,10 +418,6 @@ var SarvamChatLanguageModel = class {
413
418
  ...options,
414
419
  stream: true
415
420
  });
416
- const body = JSON.stringify({
417
- ...args,
418
- stream: true
419
- });
420
421
  const { responseHeaders, value: response } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
421
422
  url: this.config.url({
422
423
  path: "/chat/completions",
@@ -582,7 +583,7 @@ var SarvamChatLanguageModel = class {
582
583
  });
583
584
  }
584
585
  })),
585
- request: { body },
586
+ request: { body: args },
586
587
  response: { headers: responseHeaders }
587
588
  };
588
589
  }
@@ -731,7 +732,12 @@ var SarvamTranscriptionModel = class {
731
732
  language: response.language_code ?? void 0,
732
733
  durationInSeconds: response.timestamps?.end_time_seconds[response.timestamps.end_time_seconds.length - 1] ?? void 0,
733
734
  warnings,
734
- providerMetadata: { sarvam: { languageProbability: response.language_probability ?? void 0 } },
735
+ providerMetadata: { sarvam: {
736
+ request_id: response.request_id,
737
+ transcript: response.transcript,
738
+ language_code: response.language_code,
739
+ language_probability: response.language_probability
740
+ } },
735
741
  response: {
736
742
  timestamp: currentDate,
737
743
  modelId: this.modelId,
@@ -880,7 +886,8 @@ var SarvamSpeechModel = class {
880
886
  return {
881
887
  audio,
882
888
  warnings,
883
- request: { body: JSON.stringify(requestBody) },
889
+ providerMetadata: { sarvam: { request_id: value.request_id } },
890
+ request: { body: requestBody },
884
891
  response: {
885
892
  timestamp: currentDate,
886
893
  modelId: this.modelId,
@@ -931,7 +938,7 @@ var SarvamLidModel = class {
931
938
  ...options,
932
939
  stream: false
933
940
  });
934
- const { value: response } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
941
+ const { responseHeaders, value: response, rawValue: rawResponse } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
935
942
  url: this.config.url({
936
943
  path: "/text-lid",
937
944
  modelId: this.modelId
@@ -965,6 +972,17 @@ var SarvamLidModel = class {
965
972
  reasoning: void 0
966
973
  }
967
974
  },
975
+ request: { body: args },
976
+ response: {
977
+ id: response.request_id ?? void 0,
978
+ headers: responseHeaders,
979
+ body: rawResponse
980
+ },
981
+ providerMetadata: { sarvam: {
982
+ request_id: response.request_id,
983
+ script_code: response.script_code,
984
+ language_code: response.language_code
985
+ } },
968
986
  warnings: []
969
987
  };
970
988
  }
@@ -1052,7 +1070,7 @@ var SarvamTranslationModel = class {
1052
1070
  ...options,
1053
1071
  stream: false
1054
1072
  });
1055
- const { value: response } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
1073
+ const { responseHeaders, value: response, rawValue: rawResponse } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
1056
1074
  url: this.config.url({
1057
1075
  path: "/translate",
1058
1076
  modelId: this.modelId
@@ -1086,6 +1104,17 @@ var SarvamTranslationModel = class {
1086
1104
  reasoning: void 0
1087
1105
  }
1088
1106
  },
1107
+ request: { body: args },
1108
+ response: {
1109
+ id: response.request_id ?? void 0,
1110
+ headers: responseHeaders,
1111
+ body: rawResponse
1112
+ },
1113
+ providerMetadata: { sarvam: {
1114
+ request_id: response.request_id,
1115
+ source_language_code: response.source_language_code,
1116
+ translated_text: response.translated_text
1117
+ } },
1089
1118
  warnings: []
1090
1119
  };
1091
1120
  }
@@ -1158,7 +1187,7 @@ var SarvamTransliterateModel = class {
1158
1187
  ...options,
1159
1188
  stream: false
1160
1189
  });
1161
- const { value: response } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
1190
+ const { responseHeaders, value: response, rawValue: rawResponse } = await (0, __ai_sdk_provider_utils.postJsonToApi)({
1162
1191
  url: this.config.url({
1163
1192
  path: "/transliterate",
1164
1193
  modelId: this.modelId
@@ -1192,6 +1221,17 @@ var SarvamTransliterateModel = class {
1192
1221
  reasoning: void 0
1193
1222
  }
1194
1223
  },
1224
+ request: { body: args },
1225
+ response: {
1226
+ id: response.request_id ?? void 0,
1227
+ headers: responseHeaders,
1228
+ body: rawResponse
1229
+ },
1230
+ providerMetadata: { sarvam: {
1231
+ request_id: response.request_id,
1232
+ source_language_code: response.source_language_code,
1233
+ transliterated_text: response.transliterated_text
1234
+ } },
1195
1235
  warnings: []
1196
1236
  };
1197
1237
  }
package/dist/index.js CHANGED
@@ -109,8 +109,8 @@ function prepareTools({ tools, toolChoice }) {
109
109
  };
110
110
  const sarvamTools = [];
111
111
  for (const tool of finalTools) if (tool.type === "provider") toolWarnings.push({
112
- type: "unsupported-tool",
113
- tool
112
+ type: "unsupported",
113
+ feature: tool.name
114
114
  });
115
115
  else sarvamTools.push({
116
116
  type: "function",
@@ -170,7 +170,6 @@ const chatResponseSchema = z$1.object({
170
170
  logprobs: z$1.object({}).nullish(),
171
171
  message: z$1.object({
172
172
  content: z$1.string().nullish(),
173
- reasoning: z$1.string().nullish(),
174
173
  reasoning_content: z$1.string().nullish(),
175
174
  refusal: z$1.string().nullish(),
176
175
  tool_calls: z$1.array(z$1.object({
@@ -315,7 +314,7 @@ var SarvamChatLanguageModel = class {
315
314
  ...options,
316
315
  stream: false
317
316
  });
318
- const body = JSON.stringify(args);
317
+ const isJSON = options.responseFormat?.type === "json";
319
318
  const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
320
319
  url: this.config.url({
321
320
  path: "/chat/completions",
@@ -338,17 +337,16 @@ var SarvamChatLanguageModel = class {
338
337
  type: "text",
339
338
  text: choice.message.content
340
339
  });
341
- const reasoningText = choice.message.reasoning ?? choice.message.reasoning_content;
342
- if (reasoningText) content.push({
340
+ if (choice.message.reasoning_content) content.push({
343
341
  type: "reasoning",
344
- text: reasoningText
342
+ text: choice.message.reasoning_content
345
343
  });
346
- if (choice.message.tool_calls && choice.message.tool_calls.length > 0) {
347
- if (options.responseFormat?.type === "json") content.push({
344
+ if (choice.message.tool_calls && choice.message.tool_calls.length > 0) for (const toolCall of choice.message.tool_calls) {
345
+ if (isJSON) content.push({
348
346
  type: "text",
349
- text: choice.message.tool_calls[0].function.arguments
347
+ text: toolCall.function.arguments
350
348
  });
351
- for (const toolCall of choice.message.tool_calls) content.push({
349
+ content.push({
352
350
  type: "tool-call",
353
351
  toolCallId: toolCall.id ?? (this.config.generateId ?? generateId)(),
354
352
  toolName: toolCall.function.name,
@@ -358,7 +356,7 @@ var SarvamChatLanguageModel = class {
358
356
  return {
359
357
  content,
360
358
  finishReason: {
361
- unified: mapFinishReason(choice.finish_reason),
359
+ unified: isJSON ? "stop" : mapFinishReason(choice.finish_reason),
362
360
  raw: choice.finish_reason ?? void 0
363
361
  },
364
362
  usage: {
@@ -374,11 +372,18 @@ var SarvamChatLanguageModel = class {
374
372
  reasoning: void 0
375
373
  }
376
374
  },
375
+ providerMetadata: { sarvam: {
376
+ system_fingerprint: response.system_fingerprint,
377
+ service_tier: response.service_tier
378
+ } },
377
379
  warnings,
378
- request: { body },
380
+ request: { body: args },
379
381
  response: {
380
382
  headers: responseHeaders,
381
- body: rawResponse
383
+ body: rawResponse,
384
+ id: response.id ?? void 0,
385
+ modelId: response.model ?? void 0,
386
+ timestamp: response.created ? /* @__PURE__ */ new Date(response.created * 1e3) : void 0
382
387
  }
383
388
  };
384
389
  }
@@ -387,10 +392,6 @@ var SarvamChatLanguageModel = class {
387
392
  ...options,
388
393
  stream: true
389
394
  });
390
- const body = JSON.stringify({
391
- ...args,
392
- stream: true
393
- });
394
395
  const { responseHeaders, value: response } = await postJsonToApi({
395
396
  url: this.config.url({
396
397
  path: "/chat/completions",
@@ -556,7 +557,7 @@ var SarvamChatLanguageModel = class {
556
557
  });
557
558
  }
558
559
  })),
559
- request: { body },
560
+ request: { body: args },
560
561
  response: { headers: responseHeaders }
561
562
  };
562
563
  }
@@ -705,7 +706,12 @@ var SarvamTranscriptionModel = class {
705
706
  language: response.language_code ?? void 0,
706
707
  durationInSeconds: response.timestamps?.end_time_seconds[response.timestamps.end_time_seconds.length - 1] ?? void 0,
707
708
  warnings,
708
- providerMetadata: { sarvam: { languageProbability: response.language_probability ?? void 0 } },
709
+ providerMetadata: { sarvam: {
710
+ request_id: response.request_id,
711
+ transcript: response.transcript,
712
+ language_code: response.language_code,
713
+ language_probability: response.language_probability
714
+ } },
709
715
  response: {
710
716
  timestamp: currentDate,
711
717
  modelId: this.modelId,
@@ -854,7 +860,8 @@ var SarvamSpeechModel = class {
854
860
  return {
855
861
  audio,
856
862
  warnings,
857
- request: { body: JSON.stringify(requestBody) },
863
+ providerMetadata: { sarvam: { request_id: value.request_id } },
864
+ request: { body: requestBody },
858
865
  response: {
859
866
  timestamp: currentDate,
860
867
  modelId: this.modelId,
@@ -905,7 +912,7 @@ var SarvamLidModel = class {
905
912
  ...options,
906
913
  stream: false
907
914
  });
908
- const { value: response } = await postJsonToApi({
915
+ const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
909
916
  url: this.config.url({
910
917
  path: "/text-lid",
911
918
  modelId: this.modelId
@@ -939,6 +946,17 @@ var SarvamLidModel = class {
939
946
  reasoning: void 0
940
947
  }
941
948
  },
949
+ request: { body: args },
950
+ response: {
951
+ id: response.request_id ?? void 0,
952
+ headers: responseHeaders,
953
+ body: rawResponse
954
+ },
955
+ providerMetadata: { sarvam: {
956
+ request_id: response.request_id,
957
+ script_code: response.script_code,
958
+ language_code: response.language_code
959
+ } },
942
960
  warnings: []
943
961
  };
944
962
  }
@@ -1026,7 +1044,7 @@ var SarvamTranslationModel = class {
1026
1044
  ...options,
1027
1045
  stream: false
1028
1046
  });
1029
- const { value: response } = await postJsonToApi({
1047
+ const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
1030
1048
  url: this.config.url({
1031
1049
  path: "/translate",
1032
1050
  modelId: this.modelId
@@ -1060,6 +1078,17 @@ var SarvamTranslationModel = class {
1060
1078
  reasoning: void 0
1061
1079
  }
1062
1080
  },
1081
+ request: { body: args },
1082
+ response: {
1083
+ id: response.request_id ?? void 0,
1084
+ headers: responseHeaders,
1085
+ body: rawResponse
1086
+ },
1087
+ providerMetadata: { sarvam: {
1088
+ request_id: response.request_id,
1089
+ source_language_code: response.source_language_code,
1090
+ translated_text: response.translated_text
1091
+ } },
1063
1092
  warnings: []
1064
1093
  };
1065
1094
  }
@@ -1132,7 +1161,7 @@ var SarvamTransliterateModel = class {
1132
1161
  ...options,
1133
1162
  stream: false
1134
1163
  });
1135
- const { value: response } = await postJsonToApi({
1164
+ const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
1136
1165
  url: this.config.url({
1137
1166
  path: "/transliterate",
1138
1167
  modelId: this.modelId
@@ -1166,6 +1195,17 @@ var SarvamTransliterateModel = class {
1166
1195
  reasoning: void 0
1167
1196
  }
1168
1197
  },
1198
+ request: { body: args },
1199
+ response: {
1200
+ id: response.request_id ?? void 0,
1201
+ headers: responseHeaders,
1202
+ body: rawResponse
1203
+ },
1204
+ providerMetadata: { sarvam: {
1205
+ request_id: response.request_id,
1206
+ source_language_code: response.source_language_code,
1207
+ transliterated_text: response.transliterated_text
1208
+ } },
1169
1209
  warnings: []
1170
1210
  };
1171
1211
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "sarvam-ai-sdk",
3
3
  "type": "module",
4
4
  "description": "Sarvam provider support for Vercel's AI-SDK",
5
- "version": "0.3.0",
5
+ "version": "0.3.1",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",