Azure-Samples / fastapi-on-azure-functions

A sample to run a FastAPI app on Azure Functions
MIT License
88 stars 69 forks source link

StreamingResponse in fastapi #35

Open PandaWhoCodes opened 2 months ago

PandaWhoCodes commented 2 months ago

StreamingResponse in fastapi is not working

This issue is for a: (mark with an x)

- [ ] bug report -> please search issues before submitting
- [x] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

Minimal steps to reproduce


from fastapi.responses import StreamingResponse
import time
def generate_sensor_data():
"""Generate real-time sensor data."""
for i in range(10):
# Simulate temperature and humidity readings
temperature = 20 + i
humidity = 50 + i
yield f"data: {{'temperature': {temperature}, 'humidity': {humidity}}}\n\n"
time.sleep(1)

@app.post("/api/stream") async def chat_completion_stream(request): try: return StreamingResponse( generate_sensor_data(), media_type="text/event-stream" ) except Exception as e: logger.error(f"Error in chat_completion: {str(e)}", exc_info=True) raise HTTPException(status_code=500, detail=str(e)) from e


### Any log messages given by the failure
>

[2024-06-24T18:18:55.183Z] Worker failed to index functions [2024-06-24T18:18:55.185Z] Result: Failure Exception: FunctionLoadError: cannot load the http_app_func function: 'req' binding type "httpTrigger" and dataType "None" in function.json do not match the corresponding function parameter's Python type annotation "HttpRequest" Stack: File "C:\Users\ashis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 413, in _handle__functions_metadata_request self.load_function_metadata( File "C:\Users\ashis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\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:\Users\ashis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.11\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 773, in index_functions loader.process_indexed_function( File "C:\Users\ashis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\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:\Users\ashis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\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:\Users\ashis\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.11\WINDOWS\X64\azure_functions_worker\functions.py", line 243, in validate_function_params
raise FunctionLoadError(



### Expected/desired behavior
>
Streaming should work with fastapi

### OS and Version?
> Windows 10

### Versions
>
Azure Functions Core Tools
Core Tools Version:       4.0.5801 Commit hash: N/A +5ac2f09758b98257e728dd1b5576ce5ea9ef68ff (64-bit)
Function Runtime Version: 4.34.1.22669
mycaule commented 1 month ago

Have you tried importing,

from azurefunctions.extensions.http.fastapi import Request, StreamingResponse

source code is here at Azure/azurefunctions-extensions-http-fastapi and it is relying on wrapper in the azurefunctions.extensions.base module, instead of just reusing fastapi natives classes like you did.

I am also interested in the answer to the original question, as I had trouble running the samples for the beta extension, see https://github.com/Azure/azure-functions-python-worker/discussions/1349#discussioncomment-10084283