Not able to run structured extraction
Hi team, I was trying to get the project running, but it failed at the template prediction step. Here are the logs:
$ python3 structured_extraction.py
--- Processing ./ExtractFromPDF.pdf ---
2025-05-04 09:56:07,079 - INFO - Starting TWIX processing for: ./ExtractFromPDF.pdf
2025-05-04 09:56:07,079 - INFO - Running twix.transform...
Phrase extraction starts...
Phrase extraction for the merged file starts...
Phrase extraction for individual files starts...
Field prediction starts...
2025-05-04 09:56:17,536 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
perfect match starts...
cluster pruning starts...
2025-05-04 09:56:22,187 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
re-clustering starts...
2025-05-04 09:56:41,467 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
Template prediction starts...
There does not exist an input such that every predict key exists at least twice.
2025-05-04 09:56:41,476 - ERROR - An error occurred during transaction extraction: list index out of range
Traceback (most recent call last):
File "/Users/devkrishna/Desktop/Playground/TWIX/twix-ui/backend/structured_extraction.py", line 267, in extract_credit_card_transactions
fields, template, extraction_objects, cost = twix.transform(
^^^^^^^^^^^^^^^
File "/Users/devkrishna/Desktop/Playground/TWIX/twix/transform.py", line 11, in transform
template, cost = pattern.predict_template(pdf_paths, result_folder_path, LLM_model_name=LLM_model_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/devkrishna/Desktop/Playground/TWIX/twix/pattern.py", line 1583, in predict_template
template = predict_template_docs(phrases_bb, keywords, phrases, metadata)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/devkrishna/Desktop/Playground/TWIX/twix/pattern.py", line 513, in predict_template_docs
row_mp = seperate_rows(sample_phrases_bb)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/devkrishna/Desktop/Playground/TWIX/twix/pattern.py", line 1165, in seperate_rows
p_pre = pv[0][0]
~~^^^
IndexError: list index out of range
--- Extraction Results ---
{
"Transactions": []
}
--- Estimated Cost ---
Total estimated cost: $0.000000
Intermediate files saved in: ./twix_output/ExtractFromPDF
Another thing I noticed is that although it prints out total cost is $0, there was usage of the OpenAI APIs which I could confirm from my dashboard.
Hi Shimanta,
Thank you so much for trying TWIX! Would you be able to share a sample of your document so we can investigate the bugs and improve the codebase? Thanks again!
Regarding the cost, in many cases, if it’s very low, say less than $0.001, the UI might round it down and display it as 0. But we'll double check the decimal precision of cost it returns in UI. Thanks!
On Sat, May 3, 2025 at 9:36 PM Shimanta Krishna Bhuyan < @.***> wrote:
ShimantaBhuyan created an issue (ucbepic/TWIX#1) https://urldefense.com/v3/__https://github.com/ucbepic/TWIX/issues/1__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Mqr41gWR6WWUTJX2_q21vdDoWNE4KJBTYXXGzp3x2ayYcxVn8zJ5NZn7kpQ_TU5dTT8yoM98rnjrOlflTT_CXxJ7vw$
Hi team, I was trying to get the project running, but it failed at the template prediction step. Here are the logs:
$ python3 structured_extraction.py
--- Processing ./ExtractFromPDF.pdf --- 2025-05-04 09:56:07,079 - INFO - Starting TWIX processing for: ./ExtractFromPDF.pdf 2025-05-04 09:56:07,079 - INFO - Running twix.transform... Phrase extraction starts... Phrase extraction for the merged file starts... Phrase extraction for individual files starts... Field prediction starts... 2025-05-04 09:56:17,536 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions https://urldefense.com/v3/__https://api.openai.com/v1/chat/completions__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Mqr41gWR6WWUTJX2_q21vdDoWNE4KJBTYXXGzp3x2ayYcxVn8zJ5NZn7kpQ_TU5dTT8yoM98rnjrOlflTT_T98-CeA$ "HTTP/1.1 200 OK" perfect match starts... cluster pruning starts... 2025-05-04 09:56:22,187 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions https://urldefense.com/v3/__https://api.openai.com/v1/chat/completions__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Mqr41gWR6WWUTJX2_q21vdDoWNE4KJBTYXXGzp3x2ayYcxVn8zJ5NZn7kpQ_TU5dTT8yoM98rnjrOlflTT_T98-CeA$ "HTTP/1.1 200 OK" re-clustering starts... 2025-05-04 09:56:41,467 - INFO - HTTP Request: POST https://api.openai.com/v1/chat/completions https://urldefense.com/v3/__https://api.openai.com/v1/chat/completions__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Mqr41gWR6WWUTJX2_q21vdDoWNE4KJBTYXXGzp3x2ayYcxVn8zJ5NZn7kpQ_TU5dTT8yoM98rnjrOlflTT_T98-CeA$ "HTTP/1.1 200 OK" Template prediction starts... There does not exist an input such that every predict key exists at least twice. 2025-05-04 09:56:41,476 - ERROR - An error occurred during transaction extraction: list index out of range Traceback (most recent call last): File "/Users/devkrishna/Desktop/Playground/TWIX/twix-ui/backend/structured_extraction.py", line 267, in extract_credit_card_transactions fields, template, extraction_objects, cost = twix.transform( ^^^^^^^^^^^^^^^ File "/Users/devkrishna/Desktop/Playground/TWIX/twix/transform.py", line 11, in transform template, cost = pattern.predict_template(pdf_paths, result_folder_path, LLM_model_name=LLM_model_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/devkrishna/Desktop/Playground/TWIX/twix/pattern.py", line 1583, in predict_template template = predict_template_docs(phrases_bb, keywords, phrases, metadata) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/devkrishna/Desktop/Playground/TWIX/twix/pattern.py", line 513, in predict_template_docs row_mp = seperate_rows(sample_phrases_bb) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/devkrishna/Desktop/Playground/TWIX/twix/pattern.py", line 1165, in seperate_rows p_pre = pv[0][0] ~~^^^ IndexError: list index out of range
--- Extraction Results --- { "Transactions": [] }
--- Estimated Cost --- Total estimated cost: $0.000000
Intermediate files saved in: ./twix_output/ExtractFromPDF
Another thing I noticed is that although it prints out total cost is $0, there was usage of the OpenAI APIs which I could confirm from my dashboard.
— Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/ucbepic/TWIX/issues/1__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Mqr41gWR6WWUTJX2_q21vdDoWNE4KJBTYXXGzp3x2ayYcxVn8zJ5NZn7kpQ_TU5dTT8yoM98rnjrOlflTT_CXxJ7vw$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AHUJPRVZ5A3TJXGUARTHEVT24WKOPAVCNFSM6AAAAAB4MOW3HKVHI2DSMVQWIX3LMV43ASLTON2WKOZTGAZTONZXGE4DSMI__;!!CzAuKJ42GuquVTTmVmPViYEvSg!Mqr41gWR6WWUTJX2_q21vdDoWNE4KJBTYXXGzp3x2ayYcxVn8zJ5NZn7kpQ_TU5dTT8yoM98rnjrOlflTT_bnDPE8w$ . You are receiving this because you are subscribed to this thread.Message ID: @.***>
-- Sincerely, Yiming
Hey Yiming, thanks for the update. I have shared the details with you on your email.
Hi Shimanta,
We’ve identified the bug you reported and have implemented a fix. Could you please try it again and let us know if it works?
As a reminder, TWIX is currently recommended for use on documents with multiple pages created from the same template, so it can learn the shared structure to infer fields and the template effectively. We’re actively working on extending TWIX to support single-page PDFs.
The bug occurred in the part of the code that selects a minimal subportion of the document for template inference, rather than processing the entire document (which would be more expensive and slower). Specifically, TWIX tries to find the smallest document segment in which each predicted field appears at least twice. The issue in the previous version arose because some phrases spanned multiple lines. TWIX uses a vision_feature to automatically merge multi-line phrases into a single phrase; however, the local file still contained the original, unmerged phrases, leading to inconsistencies. We’ve fixed this inconsistency in the new version.
Additionally, we’ve added a fallback: if TWIX fails to identify a minimal document portion, it will default to inferring the template from the first page.
Thanks again—we really appreciate your feedback!
Hey Yiming,
Thanks for getting back. However it still didn't work. I tweaked the get_first_page_from_doc method a bit, and was able to get something running, but the extracted JSON was not at all what I was looking at. From my earlier email, what I was looking to extract are the transactions.
I have noted your other observations, and am working with a multi-page document itself. However I do accept that the transactions in the document start from the second half of the first page, which might be messing things up.