Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
334 stars 103 forks source link

[Bug] Streaming example not working (even locally) #1539

Closed fabge closed 1 month ago

fabge commented 1 month ago

Expected Behavior

Setting "PYTHON_ENABLE_INIT_INDEXING": "1" and "PYTHON_ISOLATE_WORKER_DEPENDENCIES": "1" as app settings, installing the latest azurefunctions-extensions-http-fastapi streaming library, and running func start should be successful.

And i would expect the package import statements to work. The from azurefunctions.extensions.http.fastapi import Request, StreamingResponse which is mentioned here is completely wrong. It should be from azure.functions.extension.fastapi import Request, StreamingResponse

Actual Behavior

When running locally, the app throws following exception Exception: FunctionLoadError: cannot load the stream_count function: 'req' binding type "httpTrigger" and dataType "None" in function.json do not match the corresponding function parameter's Python type annotation "Request"

Steps to Reproduce

  1. Follow the steps listed in Azure Functions Support for Http Streams in Python.
  2. Run func start

Relevant code being tried

import azure.functions as func
from azure.functions.extension.fastapi import Request, StreamingResponse

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

def generate_count():
    """Generate a stream of chronological numbers."""
    count = 0
    while True:
        yield f"counting, {count}\n\n"
        count += 1

@app.route(route="stream", methods=[func.HttpMethod.GET])
async def stream_count(req: Request) -> StreamingResponse:
    """Endpoint to stream of chronological numbers."""
    return StreamingResponse(generate_count(), media_type="text/event-stream")

Relevant log output

[2024-07-22T14:39:32.701Z] Worker failed to index functions
[2024-07-22T14:39:32.702Z] Result: Failure
[2024-07-22T14:39:32.702Z] Exception: FunctionLoadError: cannot load the stream_count function: 'req' binding type "httpTrigger" and dataType "None" in function.json do not match the corresponding function parameter's Python type annotation "Request"
[2024-07-22T14:39:32.702Z] Stack:   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 342, in _handle__worker_init_request
[2024-07-22T14:39:32.702Z]     self.load_function_metadata(
[2024-07-22T14:39:32.702Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 393, in load_function_metadata
[2024-07-22T14:39:32.702Z]     self.index_functions(function_path, function_app_directory)) \
[2024-07-22T14:39:32.702Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 773, in index_functions
[2024-07-22T14:39:32.702Z]     loader.process_indexed_function(
[2024-07-22T14:39:32.702Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/loader.py", line 139, in process_indexed_function
[2024-07-22T14:39:32.702Z]     function_info = functions_registry.add_indexed_function(
[2024-07-22T14:39:32.702Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/functions.py", line 450, in add_indexed_function
[2024-07-22T14:39:32.702Z]     deferred_bindings_enabled) = self.validate_function_params(
[2024-07-22T14:39:32.702Z]   File "/usr/lib/azure-functions-core-tools-4/workers/python/3.10/LINUX/X64/azure_functions_worker/functions.py", line 243, in validate_function_params
[2024-07-22T14:39:32.702Z]     raise FunctionLoadError(
[2024-07-22T14:39:32.702Z] .

requirements.txt file

azure-functions
azure-functions-extension-fastapi==1.0.0a2

Where are you facing this problem?

Local - Core Tools

Function app name

No response

Additional Information

No response

hallvictoria commented 1 month ago

Hi @fabge, thanks for reporting.

The correct streaming package is azurefunctions-extensions-http-fastapi. azure-functions-extension-fastapi shouldn't be referenced in the function app code or requirements.txt.

What was the error you were getting when using the from azurefunctions.extensions.http.fastapi import Request, StreamingResponse import statement and including azurefunctions-extensions-http-fastapi in your requirements.txt file?

Also, what version of Core Tools do you have? If you don't have the latest version (4.0.5907), can you try installing that? Some of the older versions might be out of date and not have support for this feature.

fabge commented 1 month ago

Thank you @hallvictoria for your response and good catch!

It seems like there is a copycat which pretends to publish your package under a similiar name:

https://pypi.org/project/azure-functions-extension-fastapi/

This is also why my code did not work. Following your explanation it worked instantly. We should probably report this! Looks like a classic dependency confusion attack, the version in the fake package is slightly higher than the original one... Also google rates the fake one a lot better, making the original one impossible to find if not searching explicitly in the pypi search.

hallvictoria commented 1 month ago

Glad I could help!

Thanks for bringing awareness to the azure-functions-extension-fastapi package. We'll take a look into it to prevent any further issues or confusion.

YunchuWang commented 1 month ago

Thank you @hallvictoria for your response and good catch!

It seems like there is a copycat which pretends to publish your package under a similiar name:

https://pypi.org/project/azure-functions-extension-fastapi/

This is also why my code did not work. Following your explanation it worked instantly. We should probably report this! Looks like a classic dependency confusion attack, the version in the fake package is slightly higher than the original one... Also google rates the fake one a lot better, making the original one impossible to find if not searching explicitly in the pypi search.

sorry for this experimental legacy package causing any confusion @fabge. the package is removed now.