Skip to content

StreamingResponse in fastapi #35

Description

@PandaWhoCodes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions