Skip to content

fix(media): raise ToolError on redirect response missing Location header (#616) - #633

Closed
Tiktokaiagent wants to merge 3 commits into
use-agent-os:mainfrom
Tiktokaiagent:fix/616-media-redirect-crash
Closed

fix(media): raise ToolError on redirect response missing Location header (#616)#633
Tiktokaiagent wants to merge 3 commits into
use-agent-os:mainfrom
Tiktokaiagent:fix/616-media-redirect-crash

Conversation

@Tiktokaiagent

@Tiktokaiagent Tiktokaiagent commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Summary

When _fetch_image_url encounters a redirect response (301, 302, 303, 307, 308) but the response has no Location header, the code previously break out of the redirect loop and continued to process the response as a non-redirect. This caused httpx.StreamClosed errors when trying to read the body of an already-closed response.

Fix

  • Raise a ToolError immediately when a redirect response is missing the Location header
  • Provides a clear, actionable error message instead of a cryptic StreamClosed crash

Changes

  • src/agentos/tools/builtin/media.py: 3 lines changed

Tests

3 new regression tests in tests/test_tools/test_media_image_download_cap.py:

$ uv run pytest tests/test_tools/test_media_image_download_cap.py -v --tb=short
...
tests/test_tools/test_media_image_download_cap.py::test_fetch_image_redirect_missing_location_raises_tool_error PASSED
tests/test_tools/test_media_image_download_cap.py::test_fetch_image_redirect_follows_location PASSED
tests/test_tools/test_media_image_download_cap.py::test_fetch_image_too_many_redirects_raises_tool_error PASSED
============================== 5 passed in 1.07s ===============================
  • This pull request fully resolves the linked issue.
  • Bug fix: no breaking changes to existing behavior
  • Includes regression tests for redirect edge cases

Fixes #616

@Tiktokaiagent Tiktokaiagent changed the title fix(media): raise ToolError on redirect response missing Location header fix(media): raise ToolError on redirect response missing Location header (#616) Aug 31, 2026
@Tiktokaiagent
Tiktokaiagent force-pushed the fix/616-media-redirect-crash branch from 5ecd1e2 to 274f729 Compare September 1, 2026 01:28
Tiktokaiagent added a commit to Tiktokaiagent/agent-os that referenced this pull request Sep 1, 2026
The new test_fetch_image_redirect_missing_location_raises_tool_error and
test_fetch_image_too_many_redirects_raises_tool_error use pytest.raises(ToolError)
but ToolError was not imported. Added the import from agentos.tools.types.
Tiktokaiagent and others added 3 commits September 1, 2026 17:30
When _fetch_image_url encounters a redirect (301, 302, 303, 307, 308)
but the response has no Location header, it previously broke out of the
redirect loop and tried to process the response as a non-redirect. This
caused httpx.StreamClosed errors when it tried to read the body of an
already-closed response.

Instead, raise a clear ToolError immediately so the caller gets a
usable error message instead of a cryptic StreamClosed crash.

Fixes use-agent-os#616
The new test_fetch_image_redirect_missing_location_raises_tool_error and
test_fetch_image_too_many_redirects_raises_tool_error use pytest.raises(ToolError)
but ToolError was not imported. Added the import from agentos.tools.types.
@Tiktokaiagent
Tiktokaiagent force-pushed the fix/616-media-redirect-crash branch from aa67b64 to bdf5deb Compare September 1, 2026 17:30
@andreapn

andreapn commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded. Reviewed alongside the other three PRs on issue #616; #773 was picked.

Your fix is in the right place. The difference is the message: "Redirect response missing Location header — cannot follow redirect without a target URL" does not say which URL returned it, and on a multi-hop chain that is the one piece of information an operator needs. The issue asks for Redirect response from <url> missing Location header, which #773 emits.

Thanks for the contribution.

@andreapn andreapn closed this Sep 2, 2026
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.

[Bug]: media._fetch_image_url crashes with httpx.StreamClosed on redirect missing Location header

2 participants