Skip to content

fix(vertexai): support timeout in ImageBytesLoader URL fetches#1826

Open
Humphrey (HumphreySun98) wants to merge 1 commit into
langchain-ai:mainfrom
HumphreySun98:fix/vertexai-image-loader-media-fetch-timeout
Open

fix(vertexai): support timeout in ImageBytesLoader URL fetches#1826
Humphrey (HumphreySun98) wants to merge 1 commit into
langchain-ai:mainfrom
HumphreySun98:fix/vertexai-image-loader-media-fetch-timeout

Conversation

@HumphreySun98

Copy link
Copy Markdown

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

  • `libs/vertexai/langchain_google_vertexai/_image_utils.py`:
    • Add keyword-only `timeout: float | None = None` to `ImageBytesLoader.init`.
    • In `_bytes_from_url`, pass `timeout=self.timeout` to `requests.get` when set; otherwise fall back to the original call shape.
  • `libs/vertexai/tests/unit_tests/test_image_utils.py`:
    • `test_image_bytes_loader_url_fetch_passes_timeout`: when constructed with a timeout, that value reaches `requests.get`.
    • `test_image_bytes_loader_url_fetch_no_timeout_by_default`: default construction calls `requests.get` without the `timeout` kwarg — preserves backward compatibility.

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.

`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.
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.

1 participant