FIX Auto-wrap synchronous token providers for AsyncOpenAI compatibility #1327
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.
Users attempting to use
get_bearer_token_provider(DefaultAzureCredential(), scope)with OpenAI targets encounterTypeError: object str can't be used in 'await' expression. The OpenAI SDK'sAsyncOpenAIrequires async token providers, but Azure'sget_bearer_token_providerreturns synchronous callables.Changes
Added
_ensure_async_token_provider()inopenai_target.pyasyncio.iscoroutinefunction()Integrated into
OpenAITarget.__init__()api_keyparameter after environment/parameter resolutionUpdated type hints and docstrings
api_key: Optional[str | Callable[[], str | Awaitable[str]]]Added test coverage
Result
Both sync and async token providers now work transparently:
Original prompt
This section details on the original issue you should resolve
<issue_title>BUG Entra auth with synchronous token providers</issue_title>
<issue_description>#### Is your feature request related to a problem? Please describe.
Since we now support auth token providers via the
api_keyargument just like the openai SDK it is somewhat unintuitive that synchronous ones won't work. There's a cryptic error being shown if you try that.E.g., people may try the following with any openai target:
which for me yields