fix: follow redirects and strip charset in URL attachment handling#1496
Open
xbrxr03 wants to merge 1 commit into
Open
fix: follow redirects and strip charset in URL attachment handling#1496xbrxr03 wants to merge 1 commit into
xbrxr03 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1046
When attaching images via URL, two bugs caused failures:
Bug 1: HTTP redirects not followed
httpx.head()andhttpx.get()inAttachment.resolve_type()andAttachment.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.pywhich already usesfollow_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 modelattachment_typessets like{"image/jpeg", "image/png", ...}, the charset-augmented string would not match, causing errors like:Changes
llm/models.pyAttachment.resolve_type(): Addedfollow_redirects=Truetohttpx.head()and stripped charset params from Content-Type headerAttachment.content_bytes(): Addedfollow_redirects=Truetohttpx.get()llm/cli.pyresolve_attachment(): Addedfollow_redirects=Truetohttpx.head()and stripped charset params from Content-Type headerTesting
8 new tests in
tests/test_attachment_url_fixes.py:Attachment.resolve_type()Attachmentclass methodsresolve_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.