Python azure sdk v2 download models from pipeline

Anonymous
2023-07-14T13:44:26.2766667+00:00

Hello, I've been trying to make a machine learning pipeline in azure python sdk v2, where I want to download some models inside the pipeline.

This is the code I've used so far:

# Create ml_client
subscription_id = "..."
resource_group  = "..."
workspace_name  = "..."

ml_client = MLClient(
    DefaultAzureCredential(),
    subscription_id,
    resource_group,
    workspace_name,
)

model_name = '...'
download_path = '...'

ml_client.models.download(name=model_name, version=1, download_path=download_path)

But when I run it as a pipeline, I get the following error message:

DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
  EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.
  ManagedIdentityCredential: No token received.
To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.
Cleaning up all outstanding Run operations, waiting 300.0 seconds
0 items cleaning up...
Cleanup took 7.152557373046875e-07 seconds
Traceback (most recent call last):
  File "Scripts/deploy_assess.py", line 123, in <module>
    download_model(model_name, model_path)
  File "Scripts/deploy_assess.py", line 111, in download_model
    ml_client.models.download(name=model_name, version=1, download_path=download_path)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/ai/ml/_telemetry/activity.py", line 263, in wrapper
    return f(*args, **kwargs)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/ai/ml/operations/_model_operations.py", line 316, in download
    model_uri = self.get(name=name, version=version).path
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/ai/ml/_telemetry/activity.py", line 263, in wrapper
    return f(*args, **kwargs)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/ai/ml/operations/_model_operations.py", line 301, in get
    model_version_resource = self._get(name, version)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/ai/ml/operations/_model_operations.py", line 247, in _get
    else self._model_versions_operation.get(
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/tracing/decorator.py", line 76, in wrapper_use_tracer
    return func(*args, **kwargs)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/ai/ml/_restclient/v2023_04_01_preview/operations/_model_versions_operations.py", line 563, in get
    pipeline_response = self._client._pipeline.run(  # pylint: disable=protected-access
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/_base.py", line 213, in run
    return first_node.send(pipeline_request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/_base.py", line 70, in send
    response = self.next.send(request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/_base.py", line 70, in send
    response = self.next.send(request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/_base.py", line 70, in send
    response = self.next.send(request)
  [Previous line repeated 2 more times]
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/mgmt/core/policies/_base.py", line 46, in send
    response = self.next.send(request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/policies/_redirect.py", line 181, in send
    response = self.next.send(request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/policies/_retry.py", line 467, in send
    response = self.next.send(request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/policies/_authentication.py", line 113, in send
    self.on_request(request)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/core/pipeline/policies/_authentication.py", line 90, in on_request
    self._token = self._credential.get_token(*self._scopes)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/identity/_credentials/default.py", line 214, in get_token
    return super(DefaultAzureCredential, self).get_token(*scopes, **kwargs)
  File "/azureml-envs/azureml_4bed098370e0b93d72c499e8bdcce325/lib/python3.10/site-packages/azure/identity/_credentials/chained.py", line 111, in get_token
    raise ClientAuthenticationError(message=message)
azure.core.exceptions.ClientAuthenticationError: DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
  EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.
  ManagedIdentityCredential: No token received.
To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.

The websites the error message redirects me to were of no help...

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,112 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 53,871 Reputation points
    2023-07-15T19:24:19.9133333+00:00

    Hello @Anonymous

    Thanks for reaching out to us, it seems like you are encountering an authentication error while using the Azure Machine Learning SDK for Python v2. The error message indicates that the DefaultAzureCredential failed to retrieve a token from the included credentials. Based on the sample you shared to me, I would like to let you try below sample to see if that works.

    The DefaultAzureCredential class tries to authenticate using a chain of credentials, including environment variables, managed identity, and service principal. However, in your case, it seems that none of the included credentials were able to retrieve a token.

    from azure.ai.ml import MLClient
    from azure.identity import DefaultAzureCredential
    
    # authenticate
    credential = DefaultAzureCredential()
    
    # Get a handle to the workspace
    ml_client = MLClient(
        credential=credential,
        subscription_id="<SUBSCRIPTION_ID>",
        resource_group_name="<RESOURCE_GROUP>",
        workspace_name="<AML_WORKSPACE_NAME>",
    )
    
    
    

    Screenshot: find the credentials for your code in the upper right of the toolbar.

    Please have a try and let me know how it goes, I hope this helps.

    Regards,

    Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.