You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure ML端点调用报错:run()缺少必填参数'method',求解决

Azure ML端点调用报错:run()缺失method参数排查

问题场景

我在Azure Notebook中调用部署在Azure ML端点的模型,已通过Swagger JSON确认JSON提交格式,Python代码如下:

import json

data =  {
  'data': [36,0,0,1,1,1],
  'method': 'predict_proba'
}

body = str.encode(json.dumps(data))

output = service.run(body)

print(output)

运行时收到以下错误:

Received bad response from service. More information can be found by calling `.get_logs()` on the webservice object.
Response Code: 502
Headers: {'Connection': 'keep-alive', 'Content-Length': '54', 'Content-Type': 'text/html; charset=utf-8', 'Date': 'Mon, 27 Mar 2023 04:10:40 GMT', 'Server': 'nginx/1.18.0 (Ubuntu)', 'X-Ms-Client-Request-Id': '8058cc94-29ae-474b-adc8-e1487ecebf03', 'X-Ms-Request-Id': '8058cc94-29ae-474b-adc8-e1487ecebf03', 'X-Ms-Run-Function-Failed': 'True', 'X-Ms-Server-Version': 'azmlinfsrv/0.7.7', 'X-Request-Id': '6c539f4c-1b61-4b2d-8406-258cb6e5335d'}
Content: b"run() missing 1 required positional argument: 'method'"

我排查后发现本地调用的run()方法并没有method参数,请问为何会出现该错误?

问题原因与解决

这个错误不是来自你本地代码里的service.run(),而是来自**Azure ML端点上部署的模型推理脚本(score.py)**中的run()函数定义。

你的请求JSON里包含了method字段,但部署在端点的score.py里的run()函数被定义成了需要接收method作为独立的位置参数,而不是从请求体的JSON结构中解析这个参数。

举个错误的示例,如果score.py里的run函数是这么写的:

def run(data, method):
    # 直接要求传入data和method两个参数
    # 推理逻辑

当服务接收到你的请求时,会尝试把请求体内容映射到函数参数,自然会找不到method参数从而报错。

正确的做法是让run()函数接收单个参数(即原始请求体),然后自己解析JSON中的字段:

import json
# 假设model是已经加载好的训练模型

def run(raw_data):
    # 解析请求体的JSON
    request_data = json.loads(raw_data)
    input_data = request_data['data']
    # 可选:设置method的默认值,避免未传参时出错
    method = request_data.get('method', 'predict')
    
    # 根据method选择对应的模型方法
    if method == 'predict_proba':
        result = model.predict_proba([input_data])
    else:
        result = model.predict([input_data])
    
    # 返回JSON格式的结果
    return json.dumps(result.tolist())

另外也需要核对Swagger文档的定义和实际score.py的逻辑是否一致——你参考的Swagger里的请求格式是正确的,但部署的服务脚本并没有按照这个格式来处理请求,才导致了参数不匹配的问题。

内容的提问来源于stack exchange,提问作者Dzartz94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 20:10:37