fix(genai): resolve blocking I/O in async contexts #1264
+51
−11
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
Fixes blocking I/O errors when using
ChatGoogleGenerativeAIin async contexts (e.g., LangGraph applications). The issue occurred becauseimportlib.metadata.version()performs blocking file I/O during async client initialization, which blocks the event loop and triggersBlockingErrorin 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
Fixes #1231
Type
🐛 Bug Fix
Changes
Core Changes
_LANGCHAIN_GENAI_VERSIONcacheget_user_agent()to use cached versionTest Changes
test_version_is_cached_at_module_level()test_get_user_agent_no_blocking_in_async_context()test_async_context_execution()Testing
Unit Tests
libs/genaiIntegration Testing
BlockingErrorno longer occursTest Commands Run
Note
This PR is part of the fix for issue #1231. A companion PR (#1267) addresses the same issue in
langchain-google-vertexai(related to #873).The solution: