pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/fastapi/fastapi/pull/15217

ef="https://github.githubassets.com/assets/actions-0e714a98ea09295a.css" /> docs: warn about BackgroundTasks + Response background interaction by ChunkyTortoise · Pull Request #15217 · fastapi/fastapi · GitHub
Skip to content

docs: warn about BackgroundTasks + Response background interaction#15217

Open
ChunkyTortoise wants to merge 1 commit intofastapi:masterfrom
ChunkyTortoise:docs/background-tasks-response-warning
Open

docs: warn about BackgroundTasks + Response background interaction#15217
ChunkyTortoise wants to merge 1 commit intofastapi:masterfrom
ChunkyTortoise:docs/background-tasks-response-warning

Conversation

@ChunkyTortoise
Copy link
Copy Markdown

Summary

Closes #11215

Adds a warning to the "Return a Response Directly" docs page explaining that returning a Response with a background parameter silently overrides any BackgroundTasks added via dependency injection.

Problem

When an endpoint uses both BackgroundTasks dependency injection and returns a Response(background=BackgroundTask(...)), the injected tasks are silently dropped. This happens because fastapi/routing.py only assigns injected background tasks when response.background is None:

if raw_response.background is None:
    raw_response.background = solved_result.background_tasks

This is a common footgun with no runtime warning -- tasks simply don't execute.

Changes

  • Added a //github.com/ warning block to docs/en/docs/advanced/response-directly.md explaining the interaction
  • Created docs_src/response_directly/tutorial003.py with correct and incorrect patterns
  • Follows existing callout conventions used in docs/en/docs/tutorial/background-tasks.md

Motivation

I use FastAPI BackgroundTasks across 5 production APIs and nearly hit this exact issue when refactoring webhook delivery to return custom Response objects. The current docs don't mention this interaction anywhere.

Test plan

  • Warning uses //github.com/ warning block syntax (FastAPI convention)
  • Example file follows existing tutorial naming pattern
  • Example is importable Python

🤖 Generated with Claude Code

Add a warning to the "Return a Response Directly" docs page explaining
that returning a Response with a background parameter silently overrides
any BackgroundTasks added via dependency injection.

Closes fastapi#11215
@github-actions github-actions bot added the docs Documentation about how to use FastAPI label Mar 25, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 487362fd31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

background_tasks.add_task(write_log, "This task will NOT run")
return JSONResponse(
content={"message": "done"},
background=None, # If this were a BackgroundTask, it would override the line above
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a non-None background in the “wrong” example

The /wrong example is labeled as demonstrating that injected BackgroundTasks are dropped, but background=None does not trigger that behavior. FastAPI only skips injected tasks when response.background is non-None, so this snippet still runs background_tasks.add_task(...) and contradicts the warning text. Readers copying this example won’t reproduce the documented pitfall, which makes the new guidance misleading.

Useful? React with 👍 / 👎.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 25, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing ChunkyTortoise:docs/background-tasks-response-warning (487362f) with master (8238446)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (937d307) during the generation of this report, so 8238446 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@github-actions
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are waiting for Sebastian to review this issue and take a decision on how to resolve this:

  • Merge BG tasks
  • Document + Emit a warning
  • Just document

If we decide to go with 3rd approach then we can take this PR and work a bit more on it (add tests (all code examples must be covered by tests), modify text to follow the style of other pages (probably split code example into good and bad, and put explanations as text outside of code examples)).

Let's wait for Sebastian to take a decision

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation about how to use FastAPI

Projects

None yet

2 participants

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy