Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling during response streaming of Fastapi #575

Open
bom1215 opened this issue Feb 13, 2025 · 0 comments
Open

Error handling during response streaming of Fastapi #575

bom1215 opened this issue Feb 13, 2025 · 0 comments

Comments

@bom1215
Copy link

bom1215 commented Feb 13, 2025

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? 🥺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant