fix(vertexai): support timeout in ImageBytesLoader URL fetches#1826
Open
Humphrey (HumphreySun98) wants to merge 1 commit into
Open
Conversation
`ImageBytesLoader._bytes_from_url` in `langchain-google-vertexai` called `requests.get(url)` without a timeout, so a slow or stalled remote URL could block request preparation indefinitely. This mirrors the bug in `langchain-google-genai` that PR langchain-ai#1693 addresses with the same fix shape. - Add a `timeout` constructor argument to `ImageBytesLoader` (keyword-only, default `None` for backward compatibility) and use it in `_bytes_from_url` when set. - Add unit-test coverage for both paths (timeout reaches `requests.get`; default behavior calls `requests.get` without a `timeout` kwarg). Callers within the package (`vision_models`, `_anthropic_utils`, `utils`, `chat_models`) are not yet wired to pass a `timeout` through; that plumbing is intentionally out of scope here and matches the genai PR's approach of landing the loader-side change first.
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.
Description
`ImageBytesLoader._bytes_from_url` in `libs/vertexai/langchain_google_vertexai/_image_utils.py` calls `requests.get(url)` without a `timeout=`, so a slow or stalled remote URL can block request preparation indefinitely before any Vertex API call is issued. The user-visible symptom matches the genai-side bug fixed in #1693: an image URL in a message can hang the whole turn.
This PR adds a `timeout` constructor argument to `ImageBytesLoader` (keyword-only, default `None` for backward compatibility) and threads it into the `requests.get` call. The shape mirrors PR #1693 exactly so the two packages stay in step.
Caller plumbing (`vision_models`, `_anthropic_utils`, `utils`, `chat_models`) is intentionally not included — the genai PR landed the same staged approach (loader first, callers later). Existing call sites (`ImageBytesLoader(project=self.project)`) continue to work unchanged.
Relevant issues
The langchain-google-genai analog — same code path, same `requests.get(url)` without timeout — is tracked as #1692 and being fixed in PR #1693. No vertexai-specific issue was open at the time of writing; this PR closes the parallel gap so both packages share the API.
Type
🐛 Bug Fix
Changes
Testing
```
$ uv run pytest libs/vertexai/tests/unit_tests/test_image_utils.py -v
... 6 passed in 9.82s
```
Reverting only the `_image_utils.py` branch while keeping the new tests makes `test_image_bytes_loader_url_fetch_passes_timeout` fail (mock_get called without `timeout=2.5`), confirming the regression coverage pins the buggy behavior.
`ruff check` and `ruff format --check` pass on both files.
Note
`timeout` is keyword-only per the `CLAUDE.md` guidance for adding new public-API parameters. The existing positional `project` argument is preserved. No deps added.
Disclaimer: this PR was prepared with the assistance of an AI agent (Claude Code). All code and test changes were reviewed by the author before submission.