Skip to content

Commit ff48d38

Browse files
committed
azure: fix credential resolution - prefer AZURE_AD_TOKEN over AZURE_API_KEY env and distinguish explicit API key
1 parent b009253 commit ff48d38

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/crewai/src/crewai/llms/providers/azure/completion.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ def __init__(
129129
model=model, temperature=temperature, stop=stop or [], **kwargs
130130
)
131131

132-
self.api_key = api_key or os.getenv("AZURE_API_KEY")
132+
explicit_api_key = api_key
133+
env_api_key = os.getenv("AZURE_API_KEY")
134+
# Preserve attribute for backwards compatibility, but keep distinction between explicit vs env API key
135+
self.api_key = explicit_api_key or env_api_key
133136
self.endpoint = (
134137
endpoint
135138
or os.getenv("AZURE_ENDPOINT")
@@ -145,14 +148,14 @@ def __init__(
145148
"Azure endpoint is required. Set AZURE_ENDPOINT environment variable or pass endpoint parameter."
146149
)
147150

148-
# Resolve credential: explicit credential -> default -> api key -> env AZURE_AD_TOKEN -> env AZURE_API_KEY
151+
# Resolve credential: explicit credential -> default -> explicit api_key -> AZURE_AD_TOKEN -> AZURE_API_KEY env
149152
self._credential: TokenCredential | None = None
150153
if credential is not None:
151154
self._credential = credential
152155
elif use_default_credential:
153156
self._credential = DefaultAzureCredential()
154-
elif self.api_key:
155-
self._credential = AzureKeyCredential(self.api_key)
157+
elif explicit_api_key:
158+
self._credential = AzureKeyCredential(explicit_api_key)
156159
else:
157160
az_ad_token = os.getenv("AZURE_AD_TOKEN")
158161
if az_ad_token:
@@ -162,7 +165,6 @@ def get_token(self, *scopes, **kwargs):
162165

163166
self._credential = _StaticToken()
164167
else:
165-
env_api_key = os.getenv("AZURE_API_KEY")
166168
if env_api_key:
167169
self._credential = AzureKeyCredential(env_api_key)
168170
else:

0 commit comments

Comments
 (0)