Skip to content

Conversation

@smart-cau
Copy link

Description

Fixes blocking I/O errors when using ChatVertexAI in async contexts (e.g., LangGraph applications). The issue occurred because importlib.metadata.version() performs blocking file I/O during async client initialization, which blocks the event loop and triggers BlockingError in async-aware environments.

Solution: Cache package version at module import time instead of reading it on every get_user_agent() call. This eliminates blocking I/O in async contexts while improving performance.

Relevant issues

Related to #1231
Related to #873

Type

🐛 Bug Fix

Changes

Core Changes

  • libs/vertexai/langchain_google_vertexai/_utils.py
    • Added module-level _LANGCHAIN_VERTEXAI_VERSION cache
    • Modified get_user_agent() to use cached version

Test Changes

  • libs/vertexai/tests/unit_tests/test_utils.py
    • Added test_version_is_cached_at_module_level()
    • Added test_get_user_agent_no_blocking_in_async_context()
    • Added test_async_context_execution()
    • Updated existing tests to use cached version

Testing

Unit Tests

  • ✅ All existing unit tests pass in libs/vertexai
  • ✅ New tests verify version caching behavior
  • ✅ New tests ensure no blocking I/O in async contexts

Test Commands Run

cd libs/vertexai && make test

Note

This PR is part of the fix for issue #1231 and #873. A companion PR (#1264) addresses the same issue in langchain-google-genai.

The solution:

  • ✅ No breaking changes to public APIs
  • ✅ Backward compatible
  • ✅ Performance improvement (version read only once)
  • ✅ Zero overhead for subsequent calls
  • ✅ Safe in all contexts (sync and async)

Cache package version at module import time to eliminate blocking I/O
during async client initialization. This fixes BlockingError when using
ChatVertexAI in async contexts like LangGraph.

- Add module-level version caching in _utils.py
- Update get_user_agent() to use cached version
- Add tests to verify caching behavior and async safety

Related to langchain-ai#1231
Related to langchain-ai#873
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