Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (provider/xai): Support structured output for latest models. #4251

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-moles-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/xai': patch
---

feat (provider/xai): Support structured output for latest models.
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/xai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { z } from 'zod';

async function main() {
const result = await generateObject({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
schema: z.object({
recipe: z.object({
name: z.string(),
Expand Down
28 changes: 28 additions & 0 deletions examples/ai-core/src/generate-text/xai-structured-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'dotenv/config';
import { generateText, Output } from 'ai';
import { xai } from '@ai-sdk/xai';
import { z } from 'zod';

async function main() {
const { experimental_output } = await generateText({
model: xai('grok-2-1212'),
experimental_output: Output.object({
schema: z.object({
name: z.string(),
age: z.number().nullable().describe('Age of the person.'),
contact: z.object({
type: z.literal('email'),
value: z.string(),
}),
occupation: z.object({
type: z.literal('employed'),
company: z.string(),
position: z.string(),
}),
}),
}),
prompt: 'Generate an example person for testing.',
});
}

main().catch(console.error);
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/xai-tool-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { weatherTool } from '../tools/weather-tool';

async function main() {
const result = await generateText({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
maxTokens: 512,
tools: {
weather: weatherTool,
Expand Down
4 changes: 2 additions & 2 deletions examples/ai-core/src/generate-text/xai.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'dotenv/config';
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
import 'dotenv/config';

async function main() {
const result = await generateText({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
prompt: 'Invent a new holiday and describe its traditions.',
});

Expand Down
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-object/xai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { z } from 'zod';

async function main() {
const result = streamObject({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
schema: z.object({
characters: z.array(
z.object({
Expand Down
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/xai-chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async function main() {
messages.push({ role: 'user', content: userInput });

const result = streamText({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
tools: {
weather: tool({
description: 'Get the weather in a location',
Expand Down
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/xai-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fs from 'node:fs';

async function main() {
const result = streamText({
model: xai('grok-vision-beta'),
model: xai('grok-2-vision-1212'),
messages: [
{
role: 'user',
Expand Down
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/xai-tool-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function main() {
let toolResponseAvailable = false;

const result = streamText({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
maxTokens: 512,
tools: {
weather: weatherTool,
Expand Down
2 changes: 1 addition & 1 deletion examples/ai-core/src/stream-text/xai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'dotenv/config';

async function main() {
const result = streamText({
model: xai('grok-beta'),
model: xai('grok-2-1212'),
prompt: 'Invent a new holiday and describe its traditions.',
});

Expand Down
4 changes: 4 additions & 0 deletions packages/xai/src/xai-chat-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export type XaiChatModelId =
| (string & {});

export interface XaiChatSettings extends OpenAICompatibleChatSettings {}

export function supportsStructuredOutputs(modelId: XaiChatModelId) {
return ['grok-2-1212', 'grok-2-vision-1212'].includes(modelId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't find anything in their docs, do we know it's only for those models or should we enable for all?

}
10 changes: 8 additions & 2 deletions packages/xai/src/xai-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
loadApiKey,
withoutTrailingSlash,
} from '@ai-sdk/provider-utils';
import { XaiChatModelId, XaiChatSettings } from './xai-chat-settings';
import {
XaiChatModelId,
XaiChatSettings,
supportsStructuredOutputs,
} from './xai-chat-settings';
import { z } from 'zod';
import { ProviderErrorStructure } from '@ai-sdk/openai-compatible';

Expand Down Expand Up @@ -89,13 +93,15 @@ export function createXai(options: XaiProviderSettings = {}): XaiProvider {
modelId: XaiChatModelId,
settings: XaiChatSettings = {},
) => {
const structuredOutputs = supportsStructuredOutputs(modelId);
return new OpenAICompatibleChatLanguageModel(modelId, settings, {
provider: 'xai.chat',
url: ({ path }) => `${baseURL}${path}`,
headers: getHeaders,
fetch: options.fetch,
defaultObjectGenerationMode: 'tool',
defaultObjectGenerationMode: structuredOutputs ? 'json' : 'tool',
errorStructure: xaiErrorStructure,
supportsStructuredOutputs: structuredOutputs,
});
};

Expand Down
Loading