@ai-sdk/xai 4.0.8 → 4.0.9

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/docs/01-xai.mdx CHANGED
@@ -212,6 +212,42 @@ const { text } = await generateText({
212
212
  });
213
213
  ```
214
214
 
215
+ You can control the resolution at which the model processes an image with
216
+ the `imageDetail` provider option on the image part:
217
+
218
+ ```ts
219
+ import { xai } from '@ai-sdk/xai';
220
+ import { generateText } from 'ai';
221
+
222
+ const { text } = await generateText({
223
+ model: xai('grok-4.3'),
224
+ messages: [
225
+ {
226
+ role: 'user',
227
+ content: [
228
+ { type: 'text', text: 'What do you see in this image?' },
229
+ {
230
+ type: 'file',
231
+ mediaType: 'image/png',
232
+ data: fs.readFileSync('./image.png'),
233
+ providerOptions: {
234
+ xai: { imageDetail: 'low' },
235
+ },
236
+ },
237
+ ],
238
+ },
239
+ ],
240
+ });
241
+ ```
242
+
243
+ The following image detail values are supported:
244
+
245
+ - **low**: processes the image at reduced resolution and consumes fewer input tokens.
246
+ - **high**: processes the image at full resolution.
247
+ - **auto**: lets the xAI API decide.
248
+
249
+ When not set, the image is processed at full resolution.
250
+
215
251
  ### Web Search Tool
216
252
 
217
253
  The web search tool enables autonomous web research with optional domain filtering and image understanding:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/xai",
3
- "version": "4.0.8",
3
+ "version": "4.0.9",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -29,17 +29,17 @@
29
29
  }
30
30
  },
31
31
  "dependencies": {
32
- "@ai-sdk/openai-compatible": "3.0.5",
32
+ "@ai-sdk/openai-compatible": "3.0.6",
33
33
  "@ai-sdk/provider": "4.0.2",
34
- "@ai-sdk/provider-utils": "5.0.5"
34
+ "@ai-sdk/provider-utils": "5.0.6"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "22.19.19",
38
38
  "tsup": "^8.5.1",
39
39
  "typescript": "5.8.3",
40
40
  "zod": "3.25.76",
41
- "@ai-sdk/test-server": "2.0.0",
42
- "@vercel/ai-tsconfig": "0.0.0"
41
+ "@vercel/ai-tsconfig": "0.0.0",
42
+ "@ai-sdk/test-server": "2.0.0"
43
43
  },
44
44
  "peerDependencies": {
45
45
  "zod": "^3.25.76 || ^4.1.8"
@@ -6,15 +6,19 @@ import {
6
6
  import {
7
7
  convertToBase64,
8
8
  getTopLevelMediaType,
9
+ parseProviderOptions,
9
10
  resolveFullMediaType,
10
11
  resolveProviderReference,
11
12
  } from '@ai-sdk/provider-utils';
12
- import type { XaiChatPrompt } from './xai-chat-prompt';
13
+ import type { XaiChatPrompt, XaiUserMessageContent } from './xai-chat-prompt';
14
+ import { xaiFilePartProviderOptions } from './xai-file-part-options';
13
15
 
14
- export function convertToXaiChatMessages(prompt: LanguageModelV4Prompt): {
16
+ export async function convertToXaiChatMessages(
17
+ prompt: LanguageModelV4Prompt,
18
+ ): Promise<{
15
19
  messages: XaiChatPrompt;
16
20
  warnings: Array<SharedV4Warning>;
17
- } {
21
+ }> {
18
22
  const messages: XaiChatPrompt = [];
19
23
  const warnings: Array<SharedV4Warning> = [];
20
24
 
@@ -31,54 +35,68 @@ export function convertToXaiChatMessages(prompt: LanguageModelV4Prompt): {
31
35
  break;
32
36
  }
33
37
 
34
- messages.push({
35
- role: 'user',
36
- content: content.map(part => {
37
- switch (part.type) {
38
- case 'text': {
39
- return { type: 'text', text: part.text };
40
- }
41
- case 'file': {
42
- switch (part.data.type) {
43
- case 'reference': {
44
- return {
45
- type: 'file',
46
- file: {
47
- file_id: resolveProviderReference({
48
- reference: part.data.reference,
49
- provider: 'xai',
38
+ const userContent: Array<XaiUserMessageContent> = [];
39
+
40
+ for (const part of content) {
41
+ switch (part.type) {
42
+ case 'text': {
43
+ userContent.push({ type: 'text', text: part.text });
44
+ break;
45
+ }
46
+ case 'file': {
47
+ switch (part.data.type) {
48
+ case 'reference': {
49
+ userContent.push({
50
+ type: 'file',
51
+ file: {
52
+ file_id: resolveProviderReference({
53
+ reference: part.data.reference,
54
+ provider: 'xai',
55
+ }),
56
+ },
57
+ });
58
+ break;
59
+ }
60
+ case 'text': {
61
+ throw new UnsupportedFunctionalityError({
62
+ functionality: 'text file parts',
63
+ });
64
+ }
65
+ case 'url':
66
+ case 'data': {
67
+ if (getTopLevelMediaType(part.mediaType) === 'image') {
68
+ const filePartOptions = await parseProviderOptions({
69
+ provider: 'xai',
70
+ providerOptions: part.providerOptions,
71
+ schema: xaiFilePartProviderOptions,
72
+ });
73
+
74
+ userContent.push({
75
+ type: 'image_url',
76
+ image_url: {
77
+ url:
78
+ part.data.type === 'url'
79
+ ? part.data.url.toString()
80
+ : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`,
81
+ ...(filePartOptions?.imageDetail != null && {
82
+ detail: filePartOptions.imageDetail,
50
83
  }),
51
84
  },
52
- };
53
- }
54
- case 'text': {
85
+ });
86
+ } else {
55
87
  throw new UnsupportedFunctionalityError({
56
- functionality: 'text file parts',
88
+ functionality: `file part media type ${part.mediaType}`,
57
89
  });
58
90
  }
59
- case 'url':
60
- case 'data': {
61
- if (getTopLevelMediaType(part.mediaType) === 'image') {
62
- return {
63
- type: 'image_url',
64
- image_url: {
65
- url:
66
- part.data.type === 'url'
67
- ? part.data.url.toString()
68
- : `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`,
69
- },
70
- };
71
- } else {
72
- throw new UnsupportedFunctionalityError({
73
- functionality: `file part media type ${part.mediaType}`,
74
- });
75
- }
76
- }
91
+ break;
77
92
  }
78
93
  }
94
+ break;
79
95
  }
80
- }),
81
- });
96
+ }
97
+ }
98
+
99
+ messages.push({ role: 'user', content: userContent });
82
100
 
83
101
  break;
84
102
  }
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ export type {
4
4
  XaiLanguageModelChatOptions as XaiProviderOptions,
5
5
  } from './xai-chat-language-model-options';
6
6
  export type { XaiErrorData } from './xai-error';
7
+ export type { XaiFilePartProviderOptions } from './xai-file-part-options';
7
8
  export type {
8
9
  XaiLanguageModelResponsesOptions,
9
10
  /** @deprecated Use `XaiLanguageModelResponsesOptions` instead. */
@@ -6,9 +6,11 @@ import {
6
6
  import {
7
7
  convertToBase64,
8
8
  getTopLevelMediaType,
9
+ parseProviderOptions,
9
10
  resolveFullMediaType,
10
11
  resolveProviderReference,
11
12
  } from '@ai-sdk/provider-utils';
13
+ import { xaiFilePartProviderOptions } from '../xai-file-part-options';
12
14
  import type {
13
15
  XaiResponsesInput,
14
16
  XaiResponsesUserMessageContentPart,
@@ -71,9 +73,18 @@ export async function convertToXaiResponsesInput({
71
73
  ? block.data.url.toString()
72
74
  : `data:${resolveFullMediaType({ part: block })};base64,${convertToBase64(block.data.data)}`;
73
75
 
76
+ const filePartOptions = await parseProviderOptions({
77
+ provider: 'xai',
78
+ providerOptions: block.providerOptions,
79
+ schema: xaiFilePartProviderOptions,
80
+ });
81
+
74
82
  contentParts.push({
75
83
  type: 'input_image',
76
84
  image_url: imageUrl,
85
+ ...(filePartOptions?.imageDetail != null && {
86
+ detail: filePartOptions.imageDetail,
87
+ }),
77
88
  });
78
89
  } else if (block.data.type === 'url') {
79
90
  // xAI's Responses API accepts non-image documents (PDF, text, CSV, etc.)
@@ -26,7 +26,11 @@ export type XaiResponsesSystemMessage = {
26
26
 
27
27
  export type XaiResponsesUserMessageContentPart =
28
28
  | { type: 'input_text'; text: string }
29
- | { type: 'input_image'; image_url: string }
29
+ | {
30
+ type: 'input_image';
31
+ image_url: string;
32
+ detail?: 'low' | 'high' | 'auto';
33
+ }
30
34
  | { type: 'input_file'; file_id: string }
31
35
  | { type: 'input_file'; file_url: string };
32
36
 
@@ -126,7 +126,7 @@ export class XaiChatLanguageModel implements LanguageModelV4 {
126
126
 
127
127
  // convert ai sdk messages to xai format
128
128
  const { messages, warnings: messageWarnings } =
129
- convertToXaiChatMessages(prompt);
129
+ await convertToXaiChatMessages(prompt);
130
130
  warnings.push(...messageWarnings);
131
131
 
132
132
  // prepare tools for xai
@@ -18,7 +18,10 @@ export interface XaiUserMessage {
18
18
 
19
19
  export type XaiUserMessageContent =
20
20
  | { type: 'text'; text: string }
21
- | { type: 'image_url'; image_url: { url: string } }
21
+ | {
22
+ type: 'image_url';
23
+ image_url: { url: string; detail?: 'low' | 'high' | 'auto' };
24
+ }
22
25
  | { type: 'file'; file: { file_id: string } };
23
26
 
24
27
  export interface XaiAssistantMessage {
@@ -0,0 +1,21 @@
1
+ import { z } from 'zod/v4';
2
+
3
+ // provider options for file parts (images) in user messages
4
+ export const xaiFilePartProviderOptions = z.object({
5
+ /**
6
+ * Controls the resolution at which the model processes the image.
7
+ * `low` processes the image at reduced resolution and consumes fewer
8
+ * input tokens, `high` processes the image at full resolution, and
9
+ * `auto` lets the API decide. Defaults to full resolution when not set.
10
+ *
11
+ * Note: the xAI API silently ignores invalid values, so the value is
12
+ * validated client-side.
13
+ *
14
+ * @see https://docs.x.ai/developers/model-capabilities/images/understanding
15
+ */
16
+ imageDetail: z.enum(['low', 'high', 'auto']).optional(),
17
+ });
18
+
19
+ export type XaiFilePartProviderOptions = z.infer<
20
+ typeof xaiFilePartProviderOptions
21
+ >;