Allow casting between sync and async versions of TokenCredentials
Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] I have either a async or sync TokenCredential, for instance a AzureCliCredential, but in some cases I need the sync version, while in others I need the async.
For example:
Using the AIProjectClient from azure.ai.projects.aio (because using non-async with these kinds of services doesn't make sense) needs a async TokenCredential, but if I then want to configure azure monitor, I need a sync Credential to set that up, so I would need to either import and create credentials twice, or do some trickery to make a derived version myself.
Describe the solution you'd like A clear and concise description of what you want to happen.
from azure.identity.aio import AzureCliCredential
credential = AzureCliCredential()
sync_cred = credential.as_sync()
or the inverse:
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
async_cred = credential.as_async()
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
from azure.identity.aio import AsyncAzureCliCredential
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
async_cred = AsyncAzureCliCredential.from_credential(credential)
Additional context Add any other context or screenshots about the feature request here.
Hi @eavanvalkenburg, I'm an AI Support assistant here to help with your issue. While the Azure SDK team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.
- Current Azure.Identity implementations focus on distinct sync and async credential types, following our SDK design guidelines (see sync and async guidelines).
- A built‐in conversion method (e.g. an as_sync() or as_async() method) would require ensuring that underlying state (such as any caching or concurrent use) remains consistent across both paths.
- This topic has come up before in our discussions about how to balance API surface complexity with clarity, and we continue to evaluate scenarios where a conversion helper could simplify user code without compromising thread safety or introducing subtle bugs.
- Until such a helper is available, using separate credential instances or wrapping the credential logic in your own abstraction remains the recommended approach.
References:
- Azure.Identity README: Azure.Identity/README.md
- Azure SDK design guidelines – Sync & Async: azure-sdk/dotnet_introduction
The Azure SDK team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!