[@ai-sdk/openai-compatible] Support multi-part content in tool responses (like Responses API does)
Description
Problem Statement
The @ai-sdk/openai-compatible package currently stringifies all tool responses with toModelOutput that return multi-part content (images, documents, etc.), making it impossible to pass rich media from tools back to models—even when the target API explicitly supports this feature.
This limitation forces developers building multi-provider applications to either:
- Use provider-specific SDKs (losing the benefit of a unified interface)
- Abandon advanced tool features like image generation/processing
- Work around the limitation by passing media URLs instead of direct data
Use Case
I'm building an application that uses multiple LLM providers (Anthropic, Groq, OpenRouter, etc.) through their OpenAI-compatible endpoints. These providers DO support multi-modal tool responses, but @ai-sdk/openai-compatible prevents me from using this feature because it:
- Declares tool response content as
stringonly (following OpenAI's spec too strictly) - JSON.stringifies all
contenttype outputs, converting structured image data into useless JSON strings - Provides no configuration option to enable richer content types
Real-world example: A tool that fetches and returns an image should be able to pass that image directly to the model for analysis. This works perfectly with @ai-sdk/anthropic, but fails with @ai-sdk/openai-compatible even when calling Anthropic's API.
Current Behavior
Code that WORKS with native Anthropic SDK:
import { anthropic } from "@ai-sdk/anthropic";
import { generateText, tool } from "ai";
import { z } from "zod";
const useImageTool = tool({
description: "Fetches an image from a URL and returns it",
parameters: z.object({
url: z.string().url(),
}),
execute: async ({ url }) => {
const response = await fetch(url);
const contentType = response.headers.get("content-type") || "image/png";
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
return {
data: buffer.toString("base64"),
mediaType: contentType,
};
},
toModelOutput: (result) => ({
type: "content" as const,
value: [
{
type: "media" as const,
data: result.data,
mediaType: result.mediaType,
},
],
}),
});
const result = await generateText({
model: anthropic("claude-sonnet-4-5-20250929"),
prompt: "Fetch and describe this image: https://example.com/image.png",
tools: { useImage: useImageTool },
maxSteps: 5,
});
console.log(result.text); // ✅ Works! Claude can see and describe the imageSame code FAILS with OpenAI-compatible:
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText, tool } from "ai";
const anthropic = createOpenAICompatible({
name: "anthropic",
baseURL: "https://api.anthropic.com/v1/",
apiKey: process.env.ANTHROPIC_API_KEY,
});
// ... exact same tool definition ...
const result = await generateText({
model: anthropic("claude-sonnet-4-5-20250929"),
prompt: "Fetch and describe this image: https://example.com/image.png",
tools: { useImage: useImageTool },
stopWhen: stepCountIs(5),
});
console.log(result.text); // ❌ Fails! Claude receives JSON string instead of image
// Tool response sent to API: {content: "[{\"type\":\"media\",\"data\":\"...\",\"mediaType\":\"image/png\"}]"}
// Instead of: {content: [{type: "image", source: {type: "base64", media_type: "image/png", data: "..."}}]}Root Cause
In packages/openai-compatible/src/chat/convert-to-openai-compatible-chat-messages.ts (lines 116-145):
case 'tool': {
for (const toolResponse of content) {
const output = toolResponse.output;
let contentValue: string; // ❌ ALWAYS A STRING
switch (output.type) {
case 'text':
case 'error-text':
contentValue = output.value;
break;
case 'execution-denied':
contentValue = output.reason ?? 'Tool execution denied.';
break;
case 'content': // ❌ Images/media get stringified here
case 'json':
case 'error-json':
contentValue = JSON.stringify(output.value);
break;
}
messages.push({
role: 'tool',
tool_call_id: toolResponse.toolCallId,
content: contentValue, // ❌ No support for multi-part content
});
}
}Compare to how Anthropic handles it (packages/anthropic/src/convert-to-anthropic-messages-prompt.ts lines 282-350):
case 'content':
contentValue = output.value
.map(contentPart => {
switch (contentPart.type) {
case 'image-data': {
return {
type: 'image' as const,
source: {
type: 'base64' as const,
media_type: contentPart.mediaType,
data: contentPart.data,
},
};
}
// ... handles other media types properly
}
})
.filter(isNonNullable);Why This Matters
Providers that support multi-modal tool responses:
- ✅ Anthropic Claude (via OpenAI-compatible endpoint)
- ✅ Groq (with vision models)
- ✅ OpenRouter (proxying Anthropic/other providers)
- ✅ Together.ai (with vision models)
- ✅ Fireworks.ai (with vision models)
- ✅ Many other OpenAI-compatible APIs
The OpenAI API spec is a baseline, not a ceiling. Many providers implement OpenAI-compatible endpoints while extending the spec with additional capabilities. The @ai-sdk/openai-compatible package should accommodate this reality.
Proposed Solution
Option 1: Configuration Flag (Recommended)
Add an optional supportsMultiPartToolContent configuration:
const anthropic = createOpenAICompatible({
name: "anthropic",
baseURL: "https://api.anthropic.com/v1/",
apiKey: process.env.ANTHROPIC_API_KEY,
supportsMultiPartToolContent: true, // Enable extended features
});When enabled, tool responses would be formatted as multi-part content arrays (similar to user messages) instead of strings:
// Current (string only):
{
role: 'tool',
tool_call_id: 'call_123',
content: "{\"type\":\"media\",\"data\":\"...\"}" // ❌ Stringified
}
// Proposed (with flag enabled):
{
role: 'tool',
tool_call_id: 'call_123',
content: [ // ✅ Structured array
{
type: 'image_url',
image_url: {
url: 'data:image/png;base64,...'
}
}
]
}Option 2: Auto-detection
Detect content type outputs and automatically format them as multi-part arrays:
case 'content':
// Check if value contains media/images
const hasMedia = output.value.some(part =>
part.type === 'media' || part.type === 'image-data' || part.type === 'image-url'
);
if (hasMedia) {
// Format as multi-part content
contentValue = output.value.map(convertToOpenAIFormat);
} else {
// Fallback to string
contentValue = JSON.stringify(output.value);
}Option 3: Provider-specific handling
Detect known providers (Anthropic, Groq, etc.) and enable extended features automatically:
const anthropic = createOpenAICompatible({
name: "anthropic", // Auto-detected, enables multi-part tool content
baseURL: "https://api.anthropic.com/v1/",
apiKey: process.env.ANTHROPIC_API_KEY,
});Comparison to Other Packages
OpenAI Package Already Has This Feature (Responses API)
The @ai-sdk/openai package ALREADY implements this correctly in the Responses API!
See packages/openai/src/responses/convert-to-openai-responses-input.ts (lines 419-452):
case 'content':
contentValue = output.value
.map(item => {
switch (item.type) {
case 'image-data': {
return {
type: 'input_image' as const,
image_url: `data:${item.mediaType};base64,${item.data}`,
};
}
case 'file-data': {
return {
type: 'input_file' as const,
filename: item.filename ?? 'data',
file_data: `data:${item.mediaType};base64,${item.data}`,
};
}
// ... properly structured, NOT stringified!
}
})
.filter(isNonNullable);✅ OpenAI Responses API: Handles multi-part tool content correctly ❌ OpenAI Chat Completions API: Stringifies everything (correct, as Chat API doesn't support this) ❌ OpenAI-Compatible: Stringifies everything (the bug!)
Other Packages
Other packages in the SDK (like @ai-sdk/anthropic) already handle multi-part tool content correctly. This feature request is about bringing that same capability—which already exists in @ai-sdk/openai's Responses API—to @ai-sdk/openai-compatible.
Questions
- Would the maintainers be open to Option 1 (configuration flag)?
- Is there a preference between opt-in (flag) vs. auto-detection (provider name)?
- Are there concerns about TypeScript types or breaking changes I should consider?
- Would a PR implementing this be welcome? I'm happy to contribute!
Summary
Request: Add support for multi-part content in tool responses to @ai-sdk/openai-compatible, using the same implementation pattern that already exists in @ai-sdk/openai's Responses API.
Why: Many developers use openai-compatible for multi-provider applications. These providers support rich tool responses (images, files), but the package currently stringifies all structured content, breaking this functionality.
Solution: Port the content case handling from packages/openai/src/responses/convert-to-openai-responses-input.ts (lines 419-452) to openai-compatible, with an opt-in flag for backward compatibility.
Impact: Enables unified, feature-complete tooling across all OpenAI-compatible providers while maintaining backward compatibility. This isn't a new feature—it's bringing existing AI SDK functionality to more providers.
Thank you for considering this enhancement! Happy to provide more details, examples, or contribute a PR if this aligns with the project's direction.
AI SDK Version
"ai": "^6.0.0-beta.130"
"@ai-sdk/openai-compatible": "^2.0.0-beta.42"Additional Context
- Related to discussion about OpenAI's limitations: The OpenAI spec is intentionally minimal, but OpenAI-compatible providers often extend it
- Anthropic's docs confirm their OpenAI-compatible endpoint supports extended features: https://docs.anthropic.com/en/api/openai-sdk
- This mirrors how user messages already support both
stringandArray<ContentPart>formats
Code of Conduct
- I agree to follow this project's Code of Conduct
Source: vercel/ai