content/docs/09-troubleshooting/70-high-memory-usage-with-images.mdx
When using generateText or streamText with many images (e.g., in a loop or batch processing), you may notice:
This is especially noticeable when using experimental_download to process images from URLs, or when sending base64-encoded images in prompts.
By default, the AI SDK includes the full request and response bodies in the step results. When processing images, the request body contains the base64-encoded image data, which can be very large (a single image can be 1MB+ when base64 encoded). If you process many images and keep references to the results, this data accumulates in memory.
For example, processing 100 images of 500KB each would include ~50MB+ of request body data in memory.
Use the experimental_include option to disable inclusion of request and/or response bodies:
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
const result = await generateText({
model: openai('gpt-4o'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe this image' },
{ type: 'image', image: imageUrl },
],
},
],
// Disable inclusion of request body to reduce memory usage
experimental_include: {
requestBody: false,
responseBody: false,
},
});
The experimental_include option accepts:
requestBody: Set to false to exclude the request body from step results. This is where base64-encoded images are stored. Default: true. Available in both generateText and streamText.responseBody: Set to false to exclude the response body from step results. Default: true. Only available in generateText.When you disable body inclusion:
result.request.body or result.response.bodyimport { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
const imageUrls = [
/* array of image URLs */
];
const results = [];
for (const imageUrl of imageUrls) {
const result = await generateText({
model: openai('gpt-4o'),
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe this image' },
{ type: 'image', image: imageUrl },
],
},
],
experimental_include: {
requestBody: false,
},
});
// Only store the text result, not the full result object
results.push(result.text);
}