Skip to content

fix: detect attachment mime from file contents#23291

Open
kitlangton wants to merge 5 commits intodevfrom
fix/read-tool-attachment-mime
Open

fix: detect attachment mime from file contents#23291
kitlangton wants to merge 5 commits intodevfrom
fix/read-tool-attachment-mime

Conversation

@kitlangton
Copy link
Copy Markdown
Contributor

Summary

  • sniff common image and PDF signatures before building read tool attachments so the declared MIME matches the payload bytes
  • fix screenshot files that keep a .png name even when the actual file contents are JPEG, which Anthropic rejects as an invalid image payload
  • add a regression test that reproduces the mismatched-extension case and verifies the emitted data URL uses image/jpeg

Testing

  • bun run test test/tool/read.test.ts -t "prefers detected image MIME over filename extension"
  • bun run test test/tool/read.test.ts
  • bun typecheck

Some screenshot files keep a .png name even when their bytes are JPEG, which causes providers like Anthropic to reject the attachment. Sniff common binary signatures before building the data URL so the declared MIME matches the payload.
Drive read-tool attachment classification from a small file header sample so mislabeled files are treated consistently. Reuse the same sampling helper for binary detection and strengthen the regression to cover the branch decision, not just the data URL MIME.
Use the scoped FileSystem.open and File.readAlloc APIs from effect-smol instead of raw fs/promises handles. This keeps read-tool sampling and binary detection inside Effect, removes promise wrappers from the hot path, and matches the repo's existing filesystem service boundary.
Extract shared media MIME helpers so read, webfetch, and message conversion use the same image/PDF rules. Add an Anthropic regression that preserves JPEG tool-result media through model-message conversion and provider transforms.
Use one sampled buffer for both MIME sniffing and binary detection so text-file reads do not reopen the file just to classify it. Simplify the shared media helper to explicit predicates so callers do not mix overlapping kind and predicate APIs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant