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

FastAPi error: Response content longer than Content-Length #82

Open
ajikmr opened this issue Dec 9, 2024 · 0 comments
Open

FastAPi error: Response content longer than Content-Length #82

ajikmr opened this issue Dec 9, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ajikmr
Copy link

ajikmr commented Dec 9, 2024

Hi,

I have created a lambda API using Cloudformation. When I chat with a file attachment included, I get the following error.

_File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 161, in send
await send(message)
File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 528, in send
raise RuntimeError("Response content longer than Content-Length")
RuntimeError: Response content longer than Content-Length

Chats without attachment seems to be fine.

After some search I found this:

Best Practices for Setting Content-Length in FastAPI
Leverage Automatic Handling by FastAPI:

FastAPI automatically includes the Content-Length header for most responses, based on the response content size. This applies when using built-in response classes like JSONResponse, HTMLResponse, or custom responses with the Response class1.
Avoid manually setting the Content-Length unless absolutely necessary.

Use Appropriate Response Classes:
Use built-in response classes such as HTMLResponse, JSONResponse, or FileResponse to ensure that headers, including Content-Length, are handled correctly.

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()

@app.get("/", response_class=HTMLResponse)
async def read_items():
    return "<html><body>Hello, World!</body></html>"

There is no such error when I call other API based AI services. So, it seems something needs to be fixed.

@ajikmr ajikmr added the bug Something isn't working label Dec 9, 2024
@ajikmr ajikmr changed the title FastAPi error: FastAPi error: Response content longer than Content-Length Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant