new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Advanced · all subjects

advanced-dependencies/yield

5 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Depends(scope='function') for early exit in yield dependencies

In FastAPI 0.121.0 and later, `Depends(scope='function')` executes the exit code after `yield` right after the path operation function finishes, before the response is sent back to the client. With `Depends(scope='request')` (the default), the exit code after `yield` executes after the response is sent.

Dependencies with yield and StreamingResponse behavior

Before FastAPI 0.118.0, exit code in dependencies with `yield` ran before sending the response, causing issues where StreamingResponse could not use database sessions closed by dependency cleanup. In 0.118.0 this was reverted so exit code runs after the response is sent, allowing StreamingResponse to use resources from dependencies throughout streaming.

Dependencies with yield and except behavior change in 0.110.0

Before FastAPI 0.110.0, if a dependency with `yield` captured an exception with `except` and didn't re-raise it, the exception was automatically forwarded to exception handlers or the internal server error handler. In 0.110.0 this changed to fix unhandled memory consumption and be consistent with regular Python behavior.

Background tasks and dependencies with yield changed in 0.106.0

Before FastAPI 0.106.0, dependencies with `yield` executed their exit code after the response was sent, allowing the same yielded objects to be used in background tasks. In 0.106.0 this changed to not hold resources while waiting for the response to travel. Resources for background tasks should now be created inside the background task itself, and you should pass only data that doesn't depend on dependency resources, such as object IDs.

Early exit use case for dependencies with yield

If a dependency with `yield` verifies something using a resource like a database session, but the path operation returns a StreamingResponse that doesn't use that resource, the session is held open unnecessarily until streaming completes. You can explicitly close the session after verification to release the connection sooner.

Give your agent this brain