Skip to content

Error handling during response streaming of Fastapi #575

@bom1215

Description

@bom1215

I confronted an issue after deploying my fastapi stream app!

I use RESPONSE_STREAM mode to stream my response. It works well in normal situation. However, when I want to raise HTTP Exception error, the server does not respond and fall in the infinite loop with this endless text of log.

Server Log

...
  | 2025-02-13T06:03:47.379Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.391Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.404Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.416Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.428Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.440Z | INFO lambda_web_adapter: app is not ready after 8000ms url=http://127.0.0.1:8000/
  | 2025-02-13T06:03:47.441Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.453Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
  | 2025-02-13T06:03:47.466Z | INFO: 127.0.0.1:35402 - "GET / HTTP/1.1" 500 Internal Server Error
...

Code snippet of main.py

@app.get("/")
def read_root():
    raise HTTPException(
            status_code=500, detail="Error occured"
        )
    return {"message": "Welcome to the chatbot API!"}

template.yaml


AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Streaming Chatbot response

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 60
    EventInvokeConfig:
        MaximumEventAgeInSeconds: 60
        MaximumRetryAttempts: 1

Resources:
  FastAPIFunction:
    Type: AWS::Serverless::Function # https://docs.aws.amazon.com/ko_kr/serverless-application-model/latest/developerguide/sam-resource-function.html
    Properties:

      PackageType: Image
      MemorySize: 1024
      Architectures:
        # - arm64 #x86_64
        - x86_64
      Environment:
        Variables:
          AWS_LWA_INVOKE_MODE: RESPONSE_STREAM
      FunctionUrlConfig:
        AuthType: NONE
        InvokeMode: RESPONSE_STREAM
      Tracing: Active
    Metadata:
      Dockerfile: Dockerfile
      DockerContext: ./app
      DockerTag: v1

Outputs:
  FastAPIFunctionUrl:
    Description: "Function URL for FastAPI function"
    Value: !GetAtt FastAPIFunctionUrl.FunctionUrl
  FastAPIFunction:
    Description: "FastAPI Lambda Function ARN"
    Value: !GetAtt FastAPIFunction.Arn

Does any one know how to fix this? 🥺

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