Skip to content

fix: follow redirects and strip charset in URL attachment handling#1496

Open
xbrxr03 wants to merge 1 commit into
simonw:mainfrom
xbrxr03:fix/attachment-url-redirects-and-charset
Open

fix: follow redirects and strip charset in URL attachment handling#1496
xbrxr03 wants to merge 1 commit into
simonw:mainfrom
xbrxr03:fix/attachment-url-redirects-and-charset

Conversation

@xbrxr03

@xbrxr03 xbrxr03 commented Jun 21, 2026

Copy link
Copy Markdown

Summary

Fixes #1046

When attaching images via URL, two bugs caused failures:

Bug 1: HTTP redirects not followed

httpx.head() and httpx.get() in Attachment.resolve_type() and Attachment.content_bytes() did not follow HTTP redirects (301/302). Any image URL that redirects — CDNs, short links, etc. — would fail instead of fetching the actual image.

This is inconsistent with the fragment loader in cli.py which already uses follow_redirects=True (line 161).

Bug 2: Content-Type charset params not stripped

HTTP responses may return Content-Type: image/jpeg; charset=utf-8, but this full string was stored as the attachment type. When checked against model attachment_types sets like {"image/jpeg", "image/png", ...}, the charset-augmented string would not match, causing errors like:

This model does not support attachments of type image/jpeg; charset=utf-8

Changes

llm/models.py

  • Attachment.resolve_type(): Added follow_redirects=True to httpx.head() and stripped charset params from Content-Type header
  • Attachment.content_bytes(): Added follow_redirects=True to httpx.get()

llm/cli.py

  • resolve_attachment(): Added follow_redirects=True to httpx.head() and stripped charset params from Content-Type header

Testing

8 new tests in tests/test_attachment_url_fixes.py:

  • 3 tests for charset stripping in Attachment.resolve_type()
  • 2 tests for redirect following in Attachment class methods
  • 3 tests for charset stripping and redirect following in resolve_attachment()

All existing tests continue to pass.

Note on the original issue

The original report (#1046) described different OCR results between URL and filepath attachments with Ollama. Subsequent investigation by @andrejartuchov-prog found the bytes were identical between both paths, suggesting qwen2.5-VL nondeterminism rather than an attachment bug. However, this PR fixes two real, reproducible bugs in URL attachment handling that would cause failures for redirected URLs and servers returning charset params.

When attaching images via URL, two bugs caused failures:

1. httpx.head() and httpx.get() did not follow HTTP redirects
   (301/302), so any image URL that redirects (CDNs, short links,
   etc.) would fail with an error instead of fetching the actual
   image.

2. Content-Type headers from HTTP responses may include charset
   params like 'image/jpeg; charset=utf-8'. These were not stripped,
   causing the mimetype to not match against model attachment_types
   sets like {'image/jpeg', 'image/png', ...}, leading to 'This model
   does not support attachments of type' errors.

Both issues are fixed by:
- Adding follow_redirects=True to all httpx.head() and httpx.get()
  calls in Attachment.resolve_type(), Attachment.content_bytes(),
  and cli.resolve_attachment()
- Stripping Content-Type parameters by splitting on ';' and taking
  the first part

Includes 8 new tests covering redirect following and charset stripping.

Refs simonw#1046
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.

Ollama OCR with URLs fails where filepaths succeed

2 participants