Python: Removed DefaultAzureCredential (#490)

* Removed DefaultAzureCredential

* Renamed ad_credential to credential
This commit is contained in:
Dmytro Struk
2025-08-26 15:33:17 +00:00
committed by GitHub
parent 20d861076a
commit fa88641263
36 changed files with 230 additions and 148 deletions
@@ -12,7 +12,7 @@ from pydantic.networks import AnyUrl
from ._shared import AzureOpenAISettings
if TYPE_CHECKING:
from azure.identity import ChainedTokenCredential
from azure.core.credentials import TokenCredential
__all__ = ["AzureAssistantsClient"]
@@ -35,7 +35,7 @@ class AzureAssistantsClient(OpenAIAssistantsClient):
ad_token: str | None = None,
ad_token_provider: AsyncAzureADTokenProvider | None = None,
token_endpoint: str | None = None,
ad_credential: "ChainedTokenCredential | None" = None,
credential: "TokenCredential | None" = None,
default_headers: Mapping[str, str] | None = None,
async_client: AsyncAzureOpenAI | None = None,
env_file_path: str | None = None,
@@ -62,7 +62,7 @@ class AzureAssistantsClient(OpenAIAssistantsClient):
ad_token: The Azure Active Directory token. (Optional)
ad_token_provider: The Azure Active Directory token provider. (Optional)
token_endpoint: The token endpoint to request an Azure token. (Optional)
ad_credential: The Azure AD credential to use for authentication. (Optional)
credential: The Azure credential to use for authentication. (Optional)
default_headers: The default headers mapping of string keys to
string values for HTTP requests. (Optional)
async_client: An existing client to use. (Optional)
@@ -95,9 +95,9 @@ class AzureAssistantsClient(OpenAIAssistantsClient):
and not ad_token
and not ad_token_provider
and azure_openai_settings.token_endpoint
and ad_credential
and credential
):
ad_token = azure_openai_settings.get_azure_auth_token(ad_credential)
ad_token = azure_openai_settings.get_azure_auth_token(credential)
if not async_client and not azure_openai_settings.api_key and not ad_token and not ad_token_provider:
raise ServiceInitializationError("The Azure OpenAI API key, ad_token, or ad_token_provider is required.")
@@ -14,7 +14,7 @@ from agent_framework import (
)
from agent_framework.exceptions import ServiceInitializationError
from agent_framework.openai._chat_client import OpenAIChatClientBase
from azure.identity import ChainedTokenCredential
from azure.core.credentials import TokenCredential
from openai.lib.azure import AsyncAzureADTokenProvider, AsyncAzureOpenAI
from openai.types.chat.chat_completion import Choice
from openai.types.chat.chat_completion_chunk import Choice as ChunkChoice
@@ -50,7 +50,7 @@ class AzureChatClient(AzureOpenAIConfigBase, OpenAIChatClientBase):
ad_token: str | None = None,
ad_token_provider: AsyncAzureADTokenProvider | None = None,
token_endpoint: str | None = None,
ad_credential: ChainedTokenCredential | None = None,
credential: TokenCredential | None = None,
default_headers: Mapping[str, str] | None = None,
async_client: AsyncAzureOpenAI | None = None,
env_file_path: str | None = None,
@@ -73,7 +73,7 @@ class AzureChatClient(AzureOpenAIConfigBase, OpenAIChatClientBase):
ad_token: The Azure Active Directory token. (Optional)
ad_token_provider: The Azure Active Directory token provider. (Optional)
token_endpoint: The token endpoint to request an Azure token. (Optional)
ad_credential: The Azure Active Directory credential. (Optional)
credential: The Azure credential for authentication. (Optional)
default_headers: The default headers mapping of string keys to
string values for HTTP requests. (Optional)
async_client: An existing client to use. (Optional)
@@ -111,7 +111,7 @@ class AzureChatClient(AzureOpenAIConfigBase, OpenAIChatClientBase):
ad_token=ad_token,
ad_token_provider=ad_token_provider,
token_endpoint=azure_openai_settings.token_endpoint,
ad_credential=ad_credential,
credential=credential,
default_headers=default_headers,
client=async_client,
instruction_role=instruction_role,
@@ -7,14 +7,14 @@ from agent_framework.exceptions import ServiceInvalidAuthError
from azure.core.exceptions import ClientAuthenticationError
if TYPE_CHECKING:
from azure.identity import ChainedTokenCredential
from azure.identity.aio import ChainedTokenCredential as AsyncChainedTokenCredential
from azure.core.credentials import TokenCredential
from azure.core.credentials_async import AsyncTokenCredential
logger: logging.Logger = logging.getLogger(__name__)
def get_entra_auth_token(
credential: "ChainedTokenCredential",
credential: "TokenCredential",
token_endpoint: str,
**kwargs: Any,
) -> str | None:
@@ -25,7 +25,6 @@ def get_entra_auth_token(
Args:
credential: The Azure credential to use for authentication.
for dev, you can use `DefaultAzureCredential`, but this is not recommended for production.
token_endpoint: The token endpoint to use to retrieve the authentication token.
**kwargs: Additional keyword arguments to pass to the token retrieval method.
@@ -47,7 +46,7 @@ def get_entra_auth_token(
async def get_entra_auth_token_async(
credential: "AsyncChainedTokenCredential", token_endpoint: str, **kwargs: Any
credential: "AsyncTokenCredential", token_endpoint: str, **kwargs: Any
) -> str | None:
"""Retrieve a async Microsoft Entra Auth Token for a given token endpoint.
@@ -56,7 +55,6 @@ async def get_entra_auth_token_async(
Args:
credential: The async Azure credential to use for authentication.
for dev, you can use `DefaultAzureCredential`, but this is not recommended for production.
token_endpoint: The token endpoint to use to retrieve the authentication token.
**kwargs: Additional keyword arguments to pass to the token retrieval method.
@@ -8,7 +8,7 @@ from agent_framework import use_tool_calling
from agent_framework.exceptions import ServiceInitializationError
from agent_framework.openai._responses_client import OpenAIResponsesClientBase
from agent_framework.telemetry import use_telemetry
from azure.identity import ChainedTokenCredential
from azure.core.credentials import TokenCredential
from openai.lib.azure import AsyncAzureADTokenProvider, AsyncAzureOpenAI
from pydantic import SecretStr, ValidationError
from pydantic.networks import AnyUrl
@@ -36,7 +36,7 @@ class AzureResponsesClient(AzureOpenAIConfigBase, OpenAIResponsesClientBase):
ad_token: str | None = None,
ad_token_provider: AsyncAzureADTokenProvider | None = None,
token_endpoint: str | None = None,
ad_credential: ChainedTokenCredential | None = None,
credential: TokenCredential | None = None,
default_headers: Mapping[str, str] | None = None,
async_client: AsyncAzureOpenAI | None = None,
env_file_path: str | None = None,
@@ -59,7 +59,7 @@ class AzureResponsesClient(AzureOpenAIConfigBase, OpenAIResponsesClientBase):
ad_token: The Azure Active Directory token. (Optional)
ad_token_provider: The Azure Active Directory token provider. (Optional)
token_endpoint: The token endpoint to request an Azure token. (Optional)
ad_credential: The Azure Active Directory credential. (Optional)
credential: The Azure credential for authentication. (Optional)
default_headers: The default headers mapping of string keys to
string values for HTTP requests. (Optional)
async_client: An existing client to use. (Optional)
@@ -107,7 +107,7 @@ class AzureResponsesClient(AzureOpenAIConfigBase, OpenAIResponsesClientBase):
ad_token=ad_token,
ad_token_provider=ad_token_provider,
token_endpoint=azure_openai_settings.token_endpoint,
ad_credential=ad_credential,
credential=credential,
default_headers=default_headers,
client=async_client,
instruction_role=instruction_role,
@@ -10,7 +10,7 @@ from agent_framework._pydantic import AFBaseSettings, HttpsUrl
from agent_framework.exceptions import ServiceInitializationError
from agent_framework.openai._shared import OpenAIHandler
from agent_framework.telemetry import USER_AGENT_KEY
from azure.identity import ChainedTokenCredential
from azure.core.credentials import TokenCredential
from openai.lib.azure import AsyncAzureOpenAI
from pydantic import ConfigDict, SecretStr, model_validator, validate_call
@@ -135,7 +135,7 @@ class AzureOpenAISettings(AFBaseSettings):
default_token_endpoint: str = DEFAULT_AZURE_TOKEN_ENDPOINT
def get_azure_auth_token(
self, credential: "ChainedTokenCredential", token_endpoint: str | None = None, **kwargs: Any
self, credential: "TokenCredential", token_endpoint: str | None = None, **kwargs: Any
) -> str | None:
"""Retrieve a Microsoft Entra Auth Token for a given token endpoint for the use with Azure OpenAI.
@@ -181,7 +181,7 @@ class AzureOpenAIConfigBase(OpenAIHandler):
ad_token: str | None = None,
ad_token_provider: Callable[[], str | Awaitable[str]] | None = None,
token_endpoint: str | None = None,
ad_credential: ChainedTokenCredential | None = None,
credential: TokenCredential | None = None,
default_headers: Mapping[str, str] | None = None,
client: AsyncAzureOpenAI | None = None,
instruction_role: str | None = None,
@@ -202,7 +202,7 @@ class AzureOpenAIConfigBase(OpenAIHandler):
ad_token: Azure AD token for authentication.
ad_token_provider: A callable or coroutine function providing Azure AD tokens.
token_endpoint: Azure AD token endpoint use to get the token.
ad_credential: Azure AD credential for authentication.
credential: Azure credential for authentication.
default_headers: Default headers for HTTP requests.
client: An existing client to use.
instruction_role: The role to use for 'instruction' messages, for example, summarization
@@ -217,8 +217,8 @@ class AzureOpenAIConfigBase(OpenAIHandler):
# If the client is None, the api_key is none, the ad_token is none, and the ad_token_provider is none,
# then we will attempt to get the ad_token using the default endpoint specified in the Azure OpenAI
# settings.
if not api_key and not ad_token_provider and not ad_token and token_endpoint and ad_credential:
ad_token = get_entra_auth_token(ad_credential, token_endpoint)
if not api_key and not ad_token_provider and not ad_token and token_endpoint and credential:
ad_token = get_entra_auth_token(credential, token_endpoint)
if not api_key and not ad_token and not ad_token_provider:
raise ServiceInitializationError(