Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(ocr): address review comments — quality_flag misreport, magic constants, nit

shadowinlife606f20d3c24b
diff --git a/agent/src/tools/doc_reader_tool.py b/agent/src/tools/doc_reader_tool.py
index 596e1c852..4814b2912 100644
--- a/agent/src/tools/doc_reader_tool.py
+++ b/agent/src/tools/doc_reader_tool.py
@@ -133,6 +133,7 @@ def _read_pdf(path: Path, pages: str, min_text_per_page: int = _MIN_TEXT_PER_PAG
total_targets = len(targets)
chunks: list[str] = []
ocr_pages = 0
+ ocr_attempted_pages = 0 # pages where OCR ran but returned empty
skipped_pages = 0
for idx, i in enumerate(targets, start=1):
if not 0 <= i < total_pages:
@@ -164,7 +165,12 @@ def _read_pdf(path: Path, pages: str, min_text_per_page: int = _MIN_TEXT_PER_PAG
chunks.append(f"--- Page {i + 1} [OCR] ---\n{ocr_text}")
ocr_pages += 1
elif text:
+ # OCR empty but page had some (sub-threshold) native text
chunks.append(f"--- Page {i + 1} ---\n{text}")
+ ocr_attempted_pages += 1
+ else:
+ # OCR attempted, returned empty, no native text either
+ ocr_attempted_pages += 1
emit_progress(
"reading_pdf",
current=idx,
@@ -186,7 +192,13 @@ def _read_pdf(path: Path, pages: str, min_text_per_page: int = _MIN_TEXT_PER_PAG
text_density = len(full) / max(pages_read, 1)
if ocr_pages == 0:
- quality_flag = "no_ocr_needed" if not skipped_pages else "no_ocr_engine"
+ if ocr_attempted_pages > 0:
+ # OCR was attempted but returned no usable text
+ quality_flag = "degraded"
+ elif skipped_pages:
+ quality_flag = "no_ocr_engine"
+ else:
+ quality_flag = "no_ocr_needed"
elif skipped_pages > 0:
quality_flag = "degraded"
else:
@@ -315,7 +327,7 @@ def _read_image(path: Path) -> str:
# Image = 1 page; text_density is total chars (matches PDF chars/page).
ocr_quality = {
"ocr_pages": 1,
- "text_density": round(len(text) / 1, 1),
+ "text_density": float(len(text)), # single page; no division needed
"quality_flag": "good",
}
return _envelope(
diff --git a/agent/src/tools/ocr/llm_vision_ocr.py b/agent/src/tools/ocr/llm_vision_ocr.py
index b995eb34d..9c2a3fd49 100644
--- a/agent/src/tools/ocr/llm_vision_ocr.py
+++ b/agent/src/tools/ocr/llm_vision_ocr.py
@@ -87,7 +87,23 @@ Do not translate.
_OCR_USER_PROMPT = "Extract all text and structured content from this image."
-_OCR_TIMEOUT = 90 # seconds — vision models are slower than text
+# Vision models are 3-10x slower than text-only; 90s covers network latency
+# and complex multi-column layouts (typical single-page OCR: 5-30s).
+_OCR_TIMEOUT = 90 # seconds
+
+# Dense A4 page ≈ 2000-4000 tokens; tables/formulas push higher.
+# 8192 is a safe upper bound supported by all mainstream vision models
+# (GPT-4o, Claude, Gemini, Qwen-VL).
+_OCR_MAX_TOKENS = 8192
+
+# OCR is faithful extraction requiring maximum determinism.
+# Temperature 0 is the industry standard for extraction/structured-output tasks.
+_OCR_TEMPERATURE = 0.0
+
+# Industry-standard "high quality" threshold. JPEG quality ≥70 has negligible
+# impact on OCR accuracy; 85 balances file size (5-10x smaller than PNG) with
+# text readability. Higher values (90+) increase upload cost with marginal gain.
+_OCR_JPEG_QUALITY = 85
def _resolve_provider_config() -> dict[str, str]:
@@ -216,8 +232,8 @@ class LlmVisionOcrEngine:
],
},
],
- max_tokens=8192,
- temperature=0.0,
+ max_tokens=_OCR_MAX_TOKENS,
+ temperature=_OCR_TEMPERATURE,
)
text = response.choices[0].message.content or ""
return text.strip()
@@ -244,7 +260,7 @@ class LlmVisionOcrEngine:
pil_img = Image.fromarray(image)
buffer = io.BytesIO()
- pil_img.save(buffer, format="JPEG", quality=85)
+ pil_img.save(buffer, format="JPEG", quality=_OCR_JPEG_QUALITY)
return base64.b64encode(buffer.getvalue()).decode("utf-8")