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

Azure Functions Python中Durable Functions starter参数加载错误问题

解决Azure Functions Python集成Durable Functions的starter参数加载错误

问题背景

我有一个包含两个HTTP触发器的Azure Functions Python项目,核心代码如下:

import azure.functions as func
from func_utils.main_func import http_trigger_function
from func_utils.get_data_trigger import main_second_trigger_func

# 实例化HTTP Function应用
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="http_trigger_function")
def http_trigger_func(req: func.HttpRequest, context) -> func.HttpResponse: 
    return http_trigger_function(req, context)

# 第二个HTTP Trigger
@app.route(route="get-data")
def main_second_trigger(req: func.HttpRequest) -> func.HttpResponse:
    return main_second_trigger_func(req)

尝试集成Durable Functions时,我给HTTP触发器添加了starter参数,代码如下:

def http_trigger(req: func.HttpRequest, starter: str) -> func.HttpResponse:
    """HTTP trigger to start the orchestration."""
    client = df.DurableOrchestrationClient(starter)
    order_details = req.get_json()

    # 启动编排
    instance_id = client.start_new("orchestrator_function", None, order_details)

    return func.HttpResponse(f"Orchestration started with ID = {instance_id}")

仅添加starter参数后,启动函数时出现以下错误:

Worker failed to index functions
[2024-11-28T23:08:32.196Z] Result: Failure
Exception: FunctionLoadError: cannot load the http_trigger_func function: the following parameters are declared in Python but not in function.json: {'starter'}     
Stack:   File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 413, in _handle__functions_metadata_request
    self.load_function_metadata(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 393, in load_function_metadata
    self.index_functions(function_path, function_app_directory)) \
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 773, in index_functions   
    loader.process_indexed_function(
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\loader.py", line 139, in process_indexed_function
    function_info = functions_registry.add_indexed_function(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\functions.py", line 450, in add_indexed_function
    deferred_bindings_enabled) = self.validate_function_params(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.11\WINDOWS\X64\azure_functions_worker\functions.py", line 137, in validate_function_params
    raise FunctionLoadError(

我的host.json配置如下:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  },
  "extensions": {
    "http": {
      "routePrefix": "api",
      "hsts": {
        "isEnabled": true,
        "includeSubDomains": true,
        "maxAge": "365"
      },
      "customHeaders": {
        "X-Content-Type-Options": "nosniff",
        "X-Frame-Options": "DENY",
        "X-XSS-Protection": "1; mode=block",
        "Content-Security-Policy": "default-src 'self'",
        "X-Content-Security-Policy": "default-src 'self'"
      }
    },
    "durableTask": {
      "hubName": "DurableFunctionsHub",
      "maxConcurrentActivityFunctions": 10,
      "maxConcurrentOrchestratorFunctions": 10
      }
  }
}

项目中没有function.json文件,尝试将starter声明为starter: df.DurableOrchestrationClient也无法解决问题。

解决方案

1. 安装Durable Functions Python包

首先确保项目中安装了Azure Functions Durable扩展的Python依赖:

pip install azure-functions-durable

如果使用requirements.txt管理依赖,记得将azure-functions-durable添加到文件中。

2. 使用正确的绑定装饰器注入Durable客户端

在Python v2编程模型(无function.json)中,不能直接声明starter参数,必须通过@app.durable_client_input()装饰器显式绑定Durable客户端。修改后的HTTP触发器代码如下:

import azure.functions as func
import azure.durable_functions as df  # 导入Durable模块

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="http_trigger_function")
@app.durable_client_input(client_name="starter")  # 添加此装饰器绑定Durable客户端
def http_trigger_func(req: func.HttpRequest, starter: df.DurableOrchestrationClient) -> func.HttpResponse:
    order_details = req.get_json()
    # 直接使用starter客户端实例,无需再用字符串初始化
    instance_id = starter.start_new("orchestrator_function", None, order_details)
    return func.HttpResponse(f"Orchestration started with ID = {instance_id}")

3. 确认host.json配置有效性

你的host.json已经正确配置了durableTask扩展和兼容的extensionBundle版本(3.x),无需额外修改。

4. 重启本地函数服务

修改代码后,重启Azure Functions Core Tools,让新的绑定配置生效。

错误原因说明

这个错误的核心是Python v2编程模型中,所有函数参数都需要通过装饰器显式绑定到对应的触发器或扩展。starter是Durable Functions的特殊绑定参数,必须用@app.durable_client_input()装饰器声明绑定,否则函数运行时无法识别该参数,导致加载失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 01:01:02