Skip to content

FastAPI file upload not working with sentry after 1.9.6 #1595

@dearkafka

Description

@dearkafka

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.9.6

Steps to Reproduce

from fastapi import FastAPI, File, UploadFile
from PIL import Image
import uvicorn
import io

import sentry_sdk
from sentry_sdk.integrations.starlette import StarletteIntegration
from sentry_sdk.integrations.fastapi import FastApiIntegration

sentry_sdk.init(
    dsn=SECRET,
    release="server",
    integrations=[
        StarletteIntegration(),
        FastApiIntegration(),
    ],
    attach_stacktrace=True,
    request_bodies="always",
    send_default_pii=True,
    traces_sample_rate=0.0,
)

app = FastAPI()


@app.post("/predict")
async def predict(file: UploadFile = File(default=None)):
    contents = await file.read()
    Image.open(io.BytesIO(contents)).convert("RGB")
    return {"result": "ok"}


uvicorn.run(app, host="0.0.0.0", port=8888, loop="asyncio", lifespan="on")

Expected Result

{"result": "ok"}

Actual Result

Please remove 'SentryAsgiMiddleware' from your project.
See https://docs.sentry.io/platforms/python/guides/asgi/ for more information.

so, when disabling sentry - all works fine, when adding sentry - it breaks. apparently sentry messes up content of uploaded file.

Metadata

Metadata

Assignees

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions