feat: Add native async authentication for Vertex AI with aiohttp #15888
+630
−2
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.
Title
feat: Add native async authentication for Vertex AI with aiohttp
Relevant issues
Addresses scalability and resource utilization issues with Vertex AI authentication in high-concurrency async environments.
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🆕 New Feature
✅ Test
Changes
Summary
Implement truly async token retrieval for Vertex AI credentials using aiohttp instead of running sync code in thread pools via asyncify. This provides better scalability and resource utilization under high concurrent load.
Implementation Details
New Async Methods:
refresh_auth_async()- Usesgoogle.auth.transport._aiohttp_requests.Requestwith aiohttp for non-blocking token refreshload_auth_async()- Async version of credential loading supporting all credential types (service accounts, authorized users, identity pools)get_access_token_async()- Async token retrieval with proper credential caching_handle_reauthentication_async()- Handles "Reauthentication is needed" errors in async contextFeature Flag:
LITELLM_USE_ASYNC_VERTEX_AUTHenvironment variable (default:false)litellm.use_async_vertex_auth = TrueFiles Modified:
litellm/__init__.py- Added feature flag declarationlitellm/llms/vertex_ai/vertex_llm_base.py- Added all async authentication methodstests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py- Added 8 comprehensive test casesBenefits
Performance:
Reliability:
async withcontext managerScalability:
Compatibility:
false)Testing
New Tests Added (8 comprehensive test cases):
test_async_auth_with_feature_flag_enabled- Verifies async methods are used when flag is enabledtest_async_auth_with_feature_flag_disabled- Verifies fallback to asyncify when flag is disabledtest_refresh_auth_async_with_aiohttp- Tests async token refreshtest_load_auth_async_service_account- Tests async credential loading for service accountstest_async_token_refresh_when_expired- Tests expired token refresh in async pathtest_async_caching_with_new_implementation- Verifies credential caching works correctlytest_async_and_sync_share_same_cache- Confirms sync and async share credential cachetest_load_auth_async_authorized_user- Tests async loading for authorized user credentialsTest Results:
Usage
Enable via environment variable:
export LITELLM_USE_ASYNC_VERTEX_AUTH=trueEnable programmatically:
Technical Notes
Why aiohttp?
asyncifywhich runs syncrequestslibrary in a thread poolSession Management:
Credential Types Supported:
Backward Compatibility
LITELLM_USE_ASYNC_VERTEX_AUTH=false)