CodeSampleX

Sample

fastapi 0.141.1: Manage FastAPI dependency injection lifecycles, request caching with Depends, background task ordering, yield exception handling, and response status/model interaction

Verified sample for pypi fastapi 0.141.1: Manage FastAPI dependency injection lifecycles, request caching with Depends, background task ordering, yield…

sha256:d77e10a9943362242c5ea9fad10b53e2bb9a5f7b9cb0be1f55808d5bd36f534e

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
2
Signing keys that built it
2
Declared environment python linux x64 python python pip

Verification-run environments

Environment Contract Stages Run
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-16
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18

Case

HOW
Goal
Manage FastAPI dependency injection lifecycles, request caching with Depends, background task ordering, yield exception handling, and response status/model interaction
Packages
Symbols
  • fastapi.FastAPI
  • fastapi.Depends
  • fastapi.HTTPException
  • fastapi.BackgroundTasks
  • fastapi.Response
  • fastapi.testclient.TestClient
  • fastapi.exceptions.FastAPIError
Environment
python
Created
2026-08-16T06:22:43Z

Contract

  1. assert Depends(func) caches within request scope so sibling and child dependencies share the same instance
  2. assert Depends(func, use_cache=False) bypasses request cache so each dependency receives a fresh instance
  3. assert generator dependencies with yield unwind teardown in reverse LIFO order after the handler returns
  4. assert BackgroundTasks execute before generator dependencies exit so database sessions remain open during background jobs
  5. assert route HTTPException is forwarded into yield dependencies where except Exception catches it
  6. assert catching HTTPException in a yield dependency without re-raising triggers FastAPIError response not awaited
  7. assert app.dependency_overrides swaps dependencies for TestClient and clear restores defaults
  8. assert direct endpoint function calls receive raw Depends objects rather than resolved dependency values
  9. assert returning JSONResponse directly bypasses response_model and overrides status_code with 200
  10. assert injecting response: Response parameter modifies status code and headers while preserving response_model filtering

Files

  • NOTES.md
  • csx.json
  • requirements.txt
  • src/__init__.py
  • src/app.py
  • test/contract.py

Download the source artifact (tar.gz)

Origin Seeder

csx-seed