Azure ML Studio批量端点Invoke调用失败排查求助
Azure ML批量端点Notebook调用失败排查与解决
问题场景
已在Azure ML Studio部署批量端点,通过GUI创建任务可正常运行,但通过Notebook调用invoke方法时失败,具体操作与报错如下:
- 成功获取端点:
ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group, workspace) endpoint_name="endpoint-name" endpoint = ml_client.batch_endpoints.get(name = "loaded-models-endpoint")
- 构造输入对象后,输出显示类型为
uri_folder(但代码指定的是URI_FILE):
data_path_input = "folder/file.parquet" datastore_input = ml_client.datastores.get(name = "datastorename") input_data = Input(type=AssetTypes.URI_FILE, path=f"{datastore_input.id}/paths/{data_path_input}") print(input_data)
{'type': 'uri_folder', 'path': 'datastore_id_path.../paths/fodler/file.parquet'}
- 调用
invoke方法的报错情况:
- 使用字典传入输入:
job = ml_client.batch_endpoints.invoke( endpoint_name = endpoint.name, inputs = {"deployement-name" : input_data} )
ValidationException: Invalid input path
- 使用
input参数传入:
job = ml_client.batch_endpoints.invoke( endpoint_name = endpoint.name, input = input_data )
仍报相同错误。
- 直接传入文件URI:
job = ml_client.batch_endpoints.invoke( endpoint_name = endpoint.name, input = uri_to_file )
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Exception: BY_POLICY
原因分析
- 输入对象路径格式错误:构造
Input时使用datastore_input.id/paths/xxx的格式不符合Azure ML要求,导致路径无效,且类型自动变为uri_folder与实际文件类型不匹配。 - 权限策略限制:直接传入原始URI时,未通过Azure ML的资产或授权路径访问,触发BY_POLICY权限拦截。
- 调用参数格式问题:多部署场景下需用
inputs字典指定部署名对应的输入,但单部署场景虽可用input,但输入对象本身无效仍会报错。
解决方案
方案1:正确构造文件类型的Input对象
方式A:使用标准datastore路径格式
直接使用azureml://datastores/<datastore-name>/paths/<file-path>格式构造路径,无需拼接datastore ID:
from azure.ai.ml.entities import Input from azure.ai.ml.constants import AssetTypes data_path_input = "folder/file.parquet" datastore_name = "datastorename" input_data = Input( type=AssetTypes.URI_FILE, path=f"azureml://datastores/{datastore_name}/paths/{data_path_input}" )
方式B:将文件注册为Data Asset(更推荐)
先将目标文件注册为数据资产,再通过资产ID或名称构造输入:
# 注册数据资产(仅需执行一次) from azure.ai.ml.entities import Data data_asset = ml_client.data.create_or_update( Data( name="input-parquet-file", path=f"azureml://datastores/{datastore_name}/paths/{data_path_input}", type=AssetTypes.URI_FILE, ) ) # 构造输入对象 input_data = Input(type=AssetTypes.URI_FILE, path=data_asset.id)
方案2:正确调用invoke方法
- 单部署场景:直接使用
input参数传入正确构造的Input对象:
job = ml_client.batch_endpoints.invoke( endpoint_name=endpoint.name, input=input_data )
- 多部署场景:使用
inputs字典,key为部署名称,value为对应输入对象:
job = ml_client.batch_endpoints.invoke( endpoint_name=endpoint.name, inputs={"deployment-name": input_data} )
方案3:解决BY_POLICY权限错误
避免直接传入未经过Azure ML资产体系的原始URI,必须使用上述两种方式构造合法的Input对象,确保请求通过Azure ML的权限策略校验。
内容的提问来源于stack exchange,提问作者Alberto Bertoncini
相关产品推荐
相关产品推荐

