Skip to content

Conversation

@shreyas-dev
Copy link

Summary

This PR refactors the GZipMiddleware to make excluded content types configurable via a constructor parameter instead of using a hardcoded constant.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@shreyas-dev shreyas-dev force-pushed the main branch 2 times, most recently from 2bb9a61 to fd68214 Compare October 15, 2025 10:25
@Kludex
Copy link
Owner

Kludex commented Oct 18, 2025

What is the issue you are having?

@petoknm
Copy link

petoknm commented Nov 4, 2025

The DEFAULT_EXCLUDED_CONTENT_TYPES variable is sometimes not what users want. In my case, I want event-stream to be compressed because I'm using data-star and returning a lot of very compressible html.

A workaround I've been using for now has been to either edit the variable directly in the package in my virtual enviroment or:

from fastapi.middleware.gzip import GZipMiddleware
from starlette.middleware import gzip

app = FastAPI()
app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=5)
gzip.DEFAULT_EXCLUDED_CONTENT_TYPES = ()

which is not pretty IMO... would like this to be configurable the same way minimum_size and compresslevel are.

@Kludex
Copy link
Owner

Kludex commented Jan 1, 2026

The DEFAULT_EXCLUDED_CONTENT_TYPES variable is sometimes not what users want. In my case, I want event-stream to be compressed because I'm using data-star and returning a lot of very compressible html.

A workaround I've been using for now has been to either edit the variable directly in the package in my virtual enviroment or:

from fastapi.middleware.gzip import GZipMiddleware
from starlette.middleware import gzip

app = FastAPI()
app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=5)
gzip.DEFAULT_EXCLUDED_CONTENT_TYPES = ()

which is not pretty IMO... would like this to be configurable the same way minimum_size and compresslevel are.

Your workaround still waits for the whole content to be sent before compressing, I don't think that's what you want anyway.

@petoknm
Copy link

petoknm commented Jan 2, 2026

Yes, you're correct. Currently it needs to wait for the whole response to compress it but in my case that happened to work out because I was just sending one big SSE response (which I admit is not what most SSE usage looks like).

But besides my concrete need being a bit unusual, the general idea of making this configurable sounds pretty valid to me.

@shreyas-dev
Copy link
Author

shreyas-dev commented Jan 10, 2026

@Kludex The issue is excluded_content_types is hardcoded and there may be instances where the user doesn't want GZipMiddleware for particular content types especially for video and audio types. I also made it configurable so that user can add or remove.

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

Successfully merging this pull request may close these issues.

3 participants