Fix: gate supportsMediaInToolResult on model capabilities
I ran into the same breakage (text-only model receiving base64 images from tool results, 400'ing the session). The root cause described above is spot-on. Here's a minimal fix that makes supportsMediaInToolResult honor model.capabilities.input so unsupported media gets extracted into a synthetic user message where unsupportedParts can catch it — exactly what the function's own comment already says it should do:
// Only apply this workaround if the model actually supports that media input - otherwise unsupportedParts() will turn it into a user-visible error.
Why it's npm-specific
supportsMediaInToolResult decides whether media in a tool result stays in the tool-role message or is extracted into a synthetic user message. The decision is based only on model.api.npm, never on model.capabilities.input. For @ai-sdk/openai it returns true unconditionally, so the image stays in the tool message — and unsupportedParts only inspects user-role messages (if (msg.role !== "user") return msg), so it never gets filtered.
With the default @ai-sdk/openai-compatible, the function returns false, the image is extracted to a user message, and unsupportedParts correctly replaces it with an error. That's why the bug only surfaces when npm: "@ai-sdk/openai" is set explicitly.
npm |
supportsMediaInToolResult(image) |
image stays in |
unsupportedParts catches it? |
sent to text-only model? |
@ai-sdk/openai |
true (unconditional) |
tool msg |
no |
yes (bug) |
@ai-sdk/openai-compatible (default) |
false |
user msg |
yes |
no (error text) |
Change 1 — packages/opencode/src/provider/transform.ts
Export the existing mimeToModality helper so it can be reused:
- function mimeToModality(mime: string): Modality | undefined {
+ export function mimeToModality(mime: string): Modality | undefined {
if (mime.startsWith("image/")) return "image"
if (mime.startsWith("audio/")) return "audio"
if (mime.startsWith("video/")) return "video"
if (mime === "application/pdf") return "pdf"
return undefined
}
Change 2 — packages/opencode/src/session/message-v2.ts
Add the capability check at the top of supportsMediaInToolResult:
import { isMedia } from "@/util/media"
+ import { mimeToModality } from "@/provider/transform"
...
const supportsMediaInToolResult = (attachment: { mime: string }) => {
+ // If the model lacks this modality, extract it so unsupportedParts() turns it
+ // into a user-visible error instead of sending unsupported media to the model.
+ const modality = mimeToModality(attachment.mime)
+ if (modality && !model.capabilities.input[modality]) return false
if (model.api.npm === "@ai-sdk/anthropic") return true
if (model.api.npm === "@ai-sdk/openai") return true
...
}
Result
Decision order becomes: model capabilities first, then SDK package name.
| scenario |
modality |
capabilities.input[modality] |
returns |
outcome |
@ai-sdk/openai + text-only model + image |
image |
false |
false |
extracted to user msg → unsupportedParts turns into error ✓ |
@ai-sdk/openai + image-capable model + image |
image |
true |
true |
stays in tool msg, sent normally ✓ |
@ai-sdk/openai-compatible (default) + text-only + image |
image |
false |
false |
unchanged ✓ |
| non-media attachment |
undefined |
— |
falls through to npm logic |
unchanged ✓ |
This also covers the other unconditional-true branches (@ai-sdk/anthropic, @ai-sdk/amazon-bedrock/mantle, @ai-sdk/google-vertex/anthropic) which had the same latent issue.
Type safety
mimeToModality returns Modality | undefined where Modality = "text"|"audio"|"image"|"video"|"pdf" (from models-dev.ts). model.capabilities.input is ProviderModalities (provider.ts) with exactly those keys, so model.capabilities.input[modality] typechecks as boolean. The @/provider/transform import adds no circular dependency (transform.ts only depends on ai, remeda, provider and models-dev types).
Happy to open a PR with this if it's useful.