azure-functions-python-worker: [BUG] routePrefix cannot be non-empty for FastAPI to work with ASGI Function on Python v2

Investigative information

Please provide the following:
  • Timestamp: 2023-08-28T22:49:33+00:00
  • Function App name:
  • Function name(s) (as appropriate):
  • Core Tools version: 4.0.5274

Repro steps

Provide the steps required to reproduce the problem:
  1. Start a new Azure Function app with the following command: func init <> --worker-runtime python --model V2
  2. In function_app.py, create a new ASGI Function App with the following code:
import azure.functions as func
import logging

from fastapi import FastAPI

api = FastAPI()

@api.get("/")
def root():
    return {"message": "Hello World"}

@api.get("/healthcheck")
def healthcheck():
    return 1

app = func.AsgiFunctionApp(
    app=api,
    http_auth_level=func.AuthLevel.ANONYMOUS
)
  1. Add fastapi to requirements.txt (currently on 0.103.0)
  2. Add the following to host.json:
"extensions": {
    "http": {
      "routePrefix": "api"
    }
  }
  1. Run func start using core tools from cli

Expected behavior

Provide a description of the expected behavior.

Should be able to access http://localhost:7071/api with no issue.

Actual behavior

Provide a description of the actual behavior observed.

Following error is received:

❯ func start
Found Python version 3.10.11 (py).

Azure Functions Core Tools
Core Tools Version:       4.0.5274 Commit hash: N/A  (64-bit)
Function Runtime Version: 4.23.0.20886

[2023-08-28T23:02:24.316Z] Worker process started and initialized.
[2023-08-28T23:02:24.385Z] A host error has occurred during startup operation 'a87fe4da-9c2c-4dd7-958f-c9b73021ea2c'.
[2023-08-28T23:02:24.387Z] Microsoft.AspNetCore.Routing: An error occurred while creating the route with name 'http_app_func' and template 'api//{*route}'. Microsoft.AspNetCore.Routing: The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value. (Parameter 'routeTemplate'). Microsoft.AspNetCore.Routing: The route template separator character '/' cannot appear consecutively. It must be separated by either a parameter or a literal value.
[2023-08-28T23:02:24.399Z] Failed to stop host instance '4b8b0863-dcb1-4a41-9b70-0beaad496108'.
[2023-08-28T23:02:24.401Z] Microsoft.Azure.WebJobs.Host: The host has not yet started.
Value cannot be null. (Parameter 'provider')
[2023-08-28T23:02:24.409Z] Host startup operation has been canceled

Known workarounds

Provide a description of any known workarounds.

Only workaround is to make the routePrefix in host.json an empty string.

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  },
  "extensions": {
    "http": {
      "routePrefix": ""
    }
  }
}

Contents of the requirements.txt file:

Provide the requirements.txt file to help us find out module related issues.
azure-functions
fastapi==0.103.0

Related information

Provide any related information
  • Links to source
  • Bindings used
Source
# __init__.py

import azure.functions as func
import logging

from fastapi import FastAPI

api = FastAPI()

@api.get("/")
def root():
    return {"message": "Hello World"}

@api.get("/healthcheck")
def healthcheck():
    return 1

app = func.AsgiFunctionApp(
    app=api,
    http_auth_level=func.AuthLevel.ANONYMOUS
)
azure-functions
fastapi==0.103.0

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 9
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Can confirm this behaviour. Only works with an empty string for routePrefix. However, if routePrefix is empty in the host.json, then the GitHub action static-web-apps-deploy@v1 will throw:

Error in processing api build artifacts: the host.json file cannot specify a http.routePrefix value other than ‘api’.

Because all endpoints should be defined with api/.

In other words:

  • Azure Functions + fastAPI + v2 Python model + Azure Static Web App does currently not work.
  • Azure Functions + v2 Python model + Azure Static Web App works.

Confirm, Error is still present.

Azure Functions + fastAPI + v2 Python model + Azure Static Web App

Wanted to deploy a demo Angular + FastAPI & GenAI.

@eddison-shyi

Are you using CI/CD such as github actions or Azure Pipelines? In which way?

The problem is that the deployment task doesn’t allow “routePrefix”: “”, as explained above.

I just ignore routePrefix in host.json, and use ‘route_path’ in FastAPI or ‘prefix’ in APIRouter hope this helps

For the time being I manually patched this by removing the excess “/” in route=“/{*route}” in http_app_func’s decorator under the AsgiFunctionApp class. It’s a terrible solution. I hope this is fixed soon.

Can this issue get more attention please? Not being able to lift & shift your fastAPI (or any other?) application is such an bummer…

Moved from https://github.com/Azure/azure-functions-python-worker/issues/1614:

I would like to do the following: Azure Functions + fastAPI + v2 Python model + Azure Static Web App

For static webapp to deploy along with an API the host.json needs routePrefix api otherwise static-web-apps-deploy@v1 will throw:

Error in processing api build artifacts: the host.json file cannot specify a http.routePrefix value other than ‘api’.

But if you do add api to the host file you’ll get the following as FastAPI prepends with a slash.

*An error occurred while creating the route with name ‘http_app_func’ and template ‘api//{route}’.

I’m seeing this same issue with all ASGI Functions Apps (not just FastAPI).

If I don’t change routePrefix in host.json, I get the error:

An error occurred while creating the route with name ‘http_app_func’ and template ‘api//{*route}’. Microsoft.AspNetCore.Routing: The route template separator character ‘/’ cannot appear consecutively.

I don’t know where the extra slash is coming from, it seems it has something to do with the route template in the _add_http_app as @gawronA and @seidnerj pointed out. However I think that’s just a side-effect.

Note, the workaround to change routePrefix to empty string "" does work, however I see this in the verbose logs:

Functions:

http_app_func: [GET,POST,DELETE,HEAD,PATCH,PUT,OPTIONS] http://localhost:7071//{*route}

Notice the double slash // is still there. This causes problems when integrating the the API into Azure API Manager (APIM), because APIM can query the HTTP triggers from the Function App, it gets these triggers with the double slash and it auto-generates broken APIM routes.

Not sure this helps anyone but if you deploy the backend as a separate function app, you can set the prefix to an empty string and make everything work.

I think the easiest way to fix this would be to remove the thrown error from the GitHub workflow. As far as I understand it should be a warning anyway. Simply rely on users setting all api paths under /api. All other routes will simply not work.