Skip to content

fix: guard text-only models from image read attachments#17204

Open
Harryoung wants to merge 2 commits intoanomalyco:devfrom
Harryoung:fix/read-image-context-guard
Open

fix: guard text-only models from image read attachments#17204
Harryoung wants to merge 2 commits intoanomalyco:devfrom
Harryoung:fix/read-image-context-guard

Conversation

@Harryoung
Copy link

@Harryoung Harryoung commented Mar 12, 2026

Issue for this PR

Closes #17205

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a text-only model like zai-coding-plan/glm-4.7 used the Read tool on an image, the tool still produced a data:image/...;base64,... attachment. That attachment could then be carried forward through the message pipeline before the provider layer finally rejected image input, which is the wrong place to catch it and can blow up context size.

This change fixes that in two places.

First, the Read tool now checks the current model capability before attaching image or PDF content. If the active model does not support that modality, the tool returns a short text explanation and does not attach the file at all.

Second, tool-result media is now filtered again when model messages are built. If a tool produced media that the current model cannot accept, that media is not re-injected into the prompt. Instead, the tool result gets a small text note explaining that the file was omitted. Models that do support image input keep the old behavior.

The reason this works is straightforward: the bad payload is no longer created for the failing path, and there is still a message-layer guard so other tools cannot accidentally reintroduce the same problem.

How did you verify your code works?

  • Ran bun test test/tool/read.test.ts test/session/message-v2.test.ts test/provider/transform.test.ts --timeout 30000 in packages/opencode
  • Added regression coverage for:
    • Read on images with a text-only model
    • tool-result media on openai-compatible text-only models
    • tool-result media on vision-capable models to confirm existing behavior still works
  • Ran a real request against zai-coding-plan/glm-4.7 using image.png and confirmed the outbound request body did not contain data:image or base64 media payloads

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Contributor

Hey! Your PR title Guard text-only models from image read attachments doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 12, 2026
@Harryoung Harryoung changed the title Guard text-only models from image read attachments fix: guard text-only models from image read attachments Mar 12, 2026
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions bot removed needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Mar 12, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@Harryoung Harryoung force-pushed the fix/read-image-context-guard branch from 6b01ede to c6880aa Compare March 13, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text-only models still receive image read attachments in prompt context

1 participant