pythonazure-functionsazure-durable-functions

Azure durable function error: the "context" parameter is expected to be of type azure.functions.Context


I am getting an annoying error when I try to execute an Azure Durable Function: the "context" parameter is expected to be of type azure.functions.Context, got <class 'azure.functions._durable_functions.OrchestrationContext'>

The code is pretty straightforward.

import azure.functions as func
import azure.durable_functions as df
import logging

app = df.DFApp(http_auth_level=func.AuthLevel.FUNCTION)

#HTTP triggered function invoked by the client
@app.route(route="orchestrators/{functionName}/{county}")
@app.durable_client_input(client_name="starter")
async def HttpAsyncTest(req: func.HttpRequest, starter: str) -> func.HttpResponse:
    logging.info("CleanMatchParcel HTTP trigger function processed a request.")

    fname = req.route_params["functionName"]
    county = req.route_params["county"]
    if fname and fname.lower() in ["funca", "funcb", "funcc"]:
        if not county:
            return func.HttpResponse("Missing county parameter in the query string.", status_code=500)
        fparams = {"county": county}
        instance_id = await starter.start_new(fname, None, fparams)
        logging.info(f"Started orchestration with ID = '{instance_id}'.")
        response, respcode = starter.create_check_status_response(req, instance_id)
        return func.HttpResponse(response, respcode)
    else:
        return func.HttpResponse("Pass a valid function name in the query string.", status_code=500)

@app.orchestration_trigger(context_name="context1")
def funca(context1: df.DurableOrchestrationContext):
    inputContext = context1.get_input()
    reqCounty = inputContext.get("county")
    #other code
    return(msg, 200)

The function.json file looks like this:

{
  "scriptFile": "function_app.py",
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "route": "orchestrators/{functionName}/{county}",
      "methods": [
        "post",
        "get"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "starter",
      "type": "durableClient",
      "direction": "in"
    },
    {
      "name": "context1",
      "type": "orchestrationTrigger",
      "direction": "in"
    }
  ]
}

I've tried removing the orchestration trigger decorator and also omitting the type specification for the function parameter context1. Those two things make the error go away, but then I geta different error.

The weird thing is that the same code works fine in another http-triggered Azure function app.

What could be wrong?


Solution

  • There is no separate function.json file present in V2 python programming model. All the binding related data will be there in function_app.py file itself and your folder structure should look like below for V2 model.

    enter image description here

    I have made few modification to your code and got it working as expected.

    function_app.py-

    import azure.functions as func
    import azure.durable_functions as df
    import logging
    
    app = df.DFApp(http_auth_level=func.AuthLevel.FUNCTION)
    
    #HTTP triggered function invoked by the client
    @app.route(route="orchestrators/{functionName}/{county?}")
    @app.durable_client_input(client_name="starter")
    async def HttpAsyncTest(req: func.HttpRequest, starter: str) -> func.HttpResponse:
        logging.info("CleanMatchParcel HTTP trigger function processed a request.")
    
        fname = req.route_params["functionName"]
        county = req.route_params.get("county")
        if fname and fname.lower() in ["funca", "funcb", "funcc"]:
            if not county:
                return func.HttpResponse("Missing county parameter in the query string.", status_code=500)
            fparams = {"county": county}
            instance_id = await starter.start_new(fname, None, fparams)
            logging.info(f"Started orchestration with ID = '{instance_id}'.")
            response = starter.create_check_status_response(req, instance_id)
            return response
        else:
            return func.HttpResponse("Pass a valid function name in the query string.", status_code=500)
    
    @app.orchestration_trigger(context_name="context")
    def funca(context1: df.DurableOrchestrationContext):
        inputContext = context1.get_input()
        reqCounty = inputContext.get("county")
        return (reqCounty,200)
    

    I am getting expected response while invoking the function Url.

    enter image description here

    enter image description here

    enter image description here

    If county parameter is missing-

    enter image description here

    If invalid function name is passed-

    enter image description here