Skip to content
Dashboard

Introducing image generation, non-blocking data streaming, improved tool calling, and more.

Check out Languine, an AI-powered CLI and pipeline designed to automate translations for developers

Link to headingImage generation

import { experimental_generateImage as generateImage } from 'ai';
import { replicate } from '@ai-sdk/replicate';
const { image } = await generateImage({
model: replicate.image('black-forest-labs/flux-1.1-pro-ultra'),
prompt: 'A futuristic cityscape at sunset',
});

Image generated with Replicate's black-forest-labs/flux-1.1-pro-ultra model

import { experimental_generateImage as generateImage } from 'ai';
import { fireworks } from '@ai-sdk/fireworks';
const { image } = await generateImage({
model: fireworks.image('accounts/fireworks/models/SSD-1B'),
prompt: 'A futuristic cityscape at sunset',
});

Image generated with Fireworks' SSD-1B model

const { image } = await generateImage({
model: replicate.image('black-forest-labs/flux-1.1-pro-ultra'),
prompt: 'A futuristic cityscape at sunset',
size: "16:9",
n: 3,
seed: 0,
providerOptions: {
replicate: { style: 'realistic_image' },
},
});

Link to headingStream transformation & smoothing

import { smoothStream, streamText } from 'ai';
const result = streamText({
model,
prompt,
experimental_transform: smoothStream(),
});

const result = streamText({
model,
prompt,
experimental_transform: [firstTransform, secondTransform],
});

Link to headingSimplified persistence with useChat

Link to headingNon-blocking data streaming

import { openai } from "@ai-sdk/openai";
import { createDataStreamResponse, Message, streamText } from "ai";
import { getRelevantContent } from "./get-relevant-content"; // user-defined
export async function POST(req: Request) {
const { messages }: { messages: Message[] } = await req.json();
const lastMessage = messages.pop();
return createDataStreamResponse({
execute: async (dataStream) => {
const relevantContent = await getRelevantContent(lastMessage.content);
for (const content of relevantContent) {
dataStream.writeData({
type: "source",
url: content.url,
title: content.title,
});
}
lastMessage.content =
lastMessage.content +
"\n\nUse the following information to answer the question: " +
relevantContent.join("\n");
const result = streamText({
model: openai("gpt-4o"),
messages: [...messages, lastMessage],
onFinish: async ({}) => {
dataStream.writeMessageAnnotation({ sources: relevantContent });
},
});
result.mergeIntoDataStream(dataStream);
},
});
}

"use client";
import { useChat } from "ai/react";
export default function Chat() {
const { messages, data } = useChat();
// Access streamed data
console.log(data);
// Access message annotations
messages.forEach(m => console.log(m.annotations));
return (/* ... */);
}

Link to headingTool-calling improvements

Link to headingImproved context within tool calls

const result = await generateText({
model,
abortSignal,
tools: {
weather: tool({
parameters: z.object({ location: z.string() }),
execute: async ({ location }, { toolCallId, messages, abortSignal }) => {
// Use toolCallId for tracking
data.appendMessageAnnotation({
type: 'tool-status',
toolCallId,
status: 'in-progress',
});
// Forward abort signal
const response = await fetch(
`https://api.weatherapi.com/v1/current.json?q=${location}`,
{ signal: abortSignal },
);
return response.json();
},
}),
},
});

Link to headingTool call repair

import { openai } from '@ai-sdk/openai';
import { generateObject, generateText, NoSuchToolError, tool } from 'ai';
const result = await generateText({
model,
tools,
prompt,
// example approach: use a model with structured outputs for repair.
// (you can use other strategies as well)
experimental_repairToolCall: async ({
toolCall,
tools,
parameterSchema,
error,
}) => {
if (NoSuchToolError.isInstance(error)) {
return null; // do not attempt to fix invalid tool names
}
const tool = tools[toolCall.toolName as keyof typeof tools];
const { object: repairedArgs } = await generateObject({
model: openai('gpt-4o', { structuredOutputs: true }),
schema: tool.parameters,
prompt: [
`The model tried to call the tool "${toolCall.toolName}"` +
` with the following arguments:`,
JSON.stringify(toolCall.args),
`The tool accepts the following schema:`,
JSON.stringify(parameterSchema(toolCall)),
'Please fix the arguments.',
].join('\\n'),
});
return { ...toolCall, args: JSON.stringify(repairedArgs) };
},
});

Link to headingGranular error handling

Link to headingStructured output improvements

Link to headingStructured outputs with tools

import { openai } from '@ai-sdk/openai';
import { generateText, tool, Output } from 'ai';
import { z } from 'zod';
const result = await generateText({
model: openai('gpt-4o', { structuredOutputs: true }),
prompt: "What's the weather like in London and New York?",
maxSteps: 5,
tools: {
getWeather: tool({
parameters: z.object({
city: z.string(),
units: z.enum(['celsius', 'fahrenheit']),
}),
execute: async ({ city, units }) => {
// Fetch weather data
},
}),
},
experimental_output: Output.object({
schema: z.object({
cities: z.array(
z.object({
name: z.string(),
temperature: z.number(),
conditions: z.string(),
}),
),
}),
}),
});

Currently, structured outputs with tools is only available with OpenAI models.

Link to headingEnhanced error handling

try {
const result = await generateObject({
model,
schema,
prompt,
});
} catch (error) {
if (error instanceof NoObjectGeneratedError) {
console.log('Generated text:', error.text);
console.log('Response metadata:', error.response);
console.log('Token usage:', error.usage);
console.log('Error cause:', error.cause);
}
}

Link to headingNew and updated providers

Link to headingGetting started

Link to headingContributors