From e3c24526482d49e59727465b878681ef681d5c8a Mon Sep 17 00:00:00 2001 From: RAJ KUMAR SATYA <112345098+rajkumar-prog@users.noreply.github.com> Date: Thu, 14 May 2026 03:55:06 -0500 Subject: [PATCH] Add warning about BackgroundTasks with direct Response --- docs/en/docs/advanced/response-directly.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/en/docs/advanced/response-directly.md b/docs/en/docs/advanced/response-directly.md index c9d18649fa..65bc7367b1 100644 --- a/docs/en/docs/advanced/response-directly.md +++ b/docs/en/docs/advanced/response-directly.md @@ -32,6 +32,12 @@ This gives you a lot of **flexibility**. You can return any data type, override It also gives you a lot of **responsibility**. You have to make sure that the data you return is correct, in the correct format, that it can be serialized, etc. +/// warning + +If you inject `BackgroundTasks` into a path operation and also return a `Response` directly with its own `background` parameter, the background task attached to the returned `Response` is used. To avoid unintentionally losing background tasks, keep all background work in the same `BackgroundTasks` object or attach a single combined background task to the response. + +/// + ## Using the `jsonable_encoder` in a `Response` { #using-the-jsonable-encoder-in-a-response } Because **FastAPI** doesn't make any changes to a `Response` you return, you have to make sure its contents are ready for it.