Repository navigation
Turn off grafting and partially address the Textract overwrite problem - #529
Merged
Merged
Conversation
…s with visible text like the Django path does
eyeseast
approved these changes
Nov 4, 2026
mitchelljkotler
approved these changes
Nov 5, 2026
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.
Changes
Adds a DISABLE_GRAFTING environment variable (default False )in the Django app so grafting can be turned off so that the memory issues in the workers can be addressed. This path runs when someone updates a document’s page text through the API with word positions, and grafts them into the PDF when the pages have no visible text layer. Position JSON is still always written. The flag check now comes before the PDF is downloaded and opened, so a disabled run skips that work entirely.
Adds a parameter in lambda (AWS SSM) called DISABLE_TEXTRACT_GRAFTING to disable the textract grafting branch entirely, which can be useful if we catch issues on production later. This code path is called when someone runs Force OCR -> Textract in the front-end. We've never had an issue with grafting with Tesseract, which is the legacy code path, so I didn't feel the need to disable that path.
In the textract grafting branch that runs on lambda, we now check if the page we are grafting into has a visible text layer so that we don't overwrite it. This is the root cause of Textract grafting strips visible text on some PDFs #336
This now leaves the PDF in a usable state and allows us more time to fix the larger issue captured in that issue on staging while allowing for Textract to work on production.
While I was here Claude noticed another issue captured here, which I patched in this PR as it is an trivial oversight and aligns with the implementation in the Django path.
This matches Django’s _merge_overlay, which already uses base_page.trimbox here:
documentcloud/documentcloud/documents/models/document.py
Line 579 in 39e07b1
What I tested on staging
Set the DISABLE_TEXTRACT_GRAFTING variable to True in the info_and_image_staging lambda and confirmed it now skips grafting in the Textract path
[GRAFT] doc_id 20015521 textract grafting disabled, skipping
Set the DISABLE_TEXTRACT_GRAFTING variable to False in the lambda and confirmed that now Textract checks if the page has visible text before trying to graft, which means we no longer destroy PDFs.
[GRAFT] doc_id 20015522 page 0 has visible text, skipping
The resulting PDF is functional, no visible text is destroyed (as it currently is on production for these 2 docs):
https://www.staging.documentcloud.org/documents/20015522-0001-requests20to20follow20up20-20sheet1/
https://www.staging.documentcloud.org/documents/20015523-0001-20tif01614030tif-8-oakn-street-1/
Set the DISABLE_GRAFTING env var on Heroku to True and confirmed that an API call to update the page text skips grafting on the worker dyno and the page text is still updated.
[SET PAGE TEXT] 20015524 - grafting disabled, skipping.
https://www.staging.documentcloud.org/documents/20015524-gpo-j6-doc-ctrl0000917052-00001-gcv/?mode=text
Set the DISABLE_GRAFTING env var on Heroku to False and confirmed that an API call to update the page text grafts on the worker dyno and the page text is updated.
[SET PAGE TEXT] 20015525 - graft page 0
https://www.staging.documentcloud.org/documents/20015525-gpo-j6-doc-ctrl0000917052-00001-gcv/?mode=text
Related issues closed
#336
https://github.com/MuckRock/issues/issues/96
Later
The data destruction concerns captured in #336 are fixed in this issue by bypassing grafting if visible text is detected, aligning this behavior with what the other path does already. This allows for Textract to continue working on production, but the ultimate fix requires more scoping and is captured in #531. That is out of scope for this PR which seeks to address immediate memory and data destruction concerns.