Fix sink state when instances are reopened - #2459
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2459 +/- ##
=======================================
Coverage 87% 87%
=======================================
Files 85 85
Lines 11930 11933 +3
=======================================
+ Hits 10339 10345 +6
+ Misses 1591 1588 -3 🚀 New features to boost your workflow:
|
Borda
previously approved these changes
Jul 28, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes lifecycle bugs in Supervision’s detection “sink” utilities by ensuring that reopening an existing sv.CSVSink or sv.JSONSink instance starts a clean output session (matching the fact that files are opened in write mode). This prevents stale per-instance state from leaking into subsequent outputs when a sink object is reused across multiple file paths.
Changes:
- Reset
JSONSink’s in-memory row buffer (self.data) on each successfulopen(). - Reset
CSVSink’s per-file header/schema state (header_written,field_names) on each successfulopen(). - Add regression tests that reuse a single sink instance across two output files, and document the fix in the unreleased changelog.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/supervision/detection/tools/json_sink.py |
Clears buffered JSON rows on open() so each output file contains only the rows appended in that session. |
src/supervision/detection/tools/csv_sink.py |
Clears CSV header/schema state on open() so each output file gets an appropriate header and column ordering. |
tests/detection/test_json.py |
Adds a lifecycle regression test validating that reopening a JSONSink does not carry over rows. |
tests/detection/test_csv.py |
Adds a lifecycle regression test validating that reopening a CSVSink rewrites header/schema for the new file. |
docs/changelog.md |
Documents the bug fix in the Unreleased “Fixed” section and updates date_modified. |
Borda
approved these changes
Jul 28, 2026
Draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before submitting
Description
Reset per-output state whenever an existing
sv.CSVSinkorsv.JSONSinkinstance successfully opens a new file.Type of Change
Motivation and Context
Sink instances retain format-specific state after they close. Reopening a
CSVSinkleavesheader_writtenand the prior field schema intact, so the next file can omit its header and serialize against stale columns. Reopening aJSONSinkleaves the prior data buffer intact, so the next file includes rows from the previous output session.Each successful
open()should begin an independent output session, matching the fact that the destination file is opened in write mode.Changes Made
Testing
python -m pytest -q tests/detection/test_csv.py tests/detection/test_json.py— 36 passedpython -m pytest -q tests/detection— 1,591 passed, 1 skippedAdditional Notes
No public API changes.