Skip to content

deepseek-ocr, deepseek-vl2: parse model output with ast.literal_eval - #3820

Open
fjvbn2003 wants to merge 1 commit into
openvinotoolkit:latestfrom
fjvbn2003:ocr-literal-eval
Open

fjvbn2003 wants to merge 1 commit into
openvinotoolkit:latestfrom
fjvbn2003:ocr-literal-eval

Conversation

@fjvbn2003

@fjvbn2003 fjvbn2003 commented Oct 29, 2026 •

Copy link
Copy Markdown

What

Replace eval() with ast.literal_eval() where the notebooks parse text produced by the model, and add import ast:

  • notebooks/deepseek-ocr/ov_deepseek_ocr_helper.py, ov_deepseek_ocr2_helper.py: <|det|> coordinates in extract_coordinates_and_label, and the geometry (Line) output
  • notebooks/deepseek-ocr/gradio_helper.py: coordinates in draw_bounding_boxes
  • notebooks/deepseek-vl2/gradio_helper.py and the same parse_ref_bbox cell in deepseek-vl2.ipynb: grounding boxes

No other changes; lines stay within the 160-column Black limit.

Why

The model's output depends on the input image or document, so it is untrusted input. These spots only need lists, tuples and numbers, which ast.literal_eval parses without evaluating expressions. This matches what notebooks/unlimited-ocr already does for the same <|det|> format. Text that isn't a literal now raises ValueError and takes the existing error path: the except in extract_coordinates_and_label / parse_ref_bbox, or an exception to the caller in the Gradio draw_bounding_boxes, as malformed coordinates already do.

Testing

No model or GPU needed. I loaded the affected functions from the original and the patched files (and from the notebook cell) into a small script (Python 3.12, Pillow 12.3) and compared them:

  • [[10, 20, 30, 40]], [10, 20, 30, 40], several boxes, floats/negatives, tuples and non-list literals return the same values as before.
  • draw_bounding_boxes and parse_ref_bbox produce byte-identical images (and the same crops).
  • Coordinate text containing a Python expression is not evaluated; it goes through the existing error handling described above.
  • A sample geometry dict parses to the same lines and endpoints.

I did not run the notebooks end to end.

Prepared with help from Claude (AI), including the local test described above.

The DeepSeek-OCR helpers and the DeepSeek-VL2 grounding helper parse
box coordinates (and, in the OCR helpers, the geometry output) from the
model's text with eval(). That text depends on the input image or
document, so it should be treated as untrusted. Only Python literals
are needed, and ast.literal_eval() parses them without evaluating
expressions, as the unlimited-ocr helpers already do.

Valid coordinates parse to the same values as before. Text that is not
a literal now raises ValueError and goes through the existing error
handling.

Assisted-by: Claude <[email protected]>
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions Bot added the python Pull requests that update Python code label Oct 29, 2026
@akashchi
akashchi requested a balanced review from Copilot November 2, 2026 09:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the DeepSeek output-parsing hardening changes.

No open issues 🎉

What changed in this PR

This PR hardens DeepSeek OCR and VL2 output parsing by replacing eval() with ast.literal_eval(), preventing model-generated text from executing arbitrary Python expressions.

Changes:

  • Adds ast imports and literal parsing for OCR coordinates and geometry output.
  • Updates VL2 grounding-box parsing in both the Gradio helper and notebook cell.
File Description
notebooks/​deepseek-vl2/​gradio_helper.py Uses literal parsing for grounding boxes.
notebooks/​deepseek-vl2/​deepseek-vl2.ipynb Applies the same grounding-box change in the notebook.
notebooks/​deepseek-ocr/​ov_deepseek_ocr2_helper.py Uses literal parsing for OCR2 coordinates and geometry.
notebooks/​deepseek-ocr/​ov_deepseek_ocr_helper.py Uses literal parsing for OCR coordinates and geometry.
notebooks/​deepseek-ocr/​gradio_helper.py Uses literal parsing for bounding-box coordinates.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants