fix(pdf-vision): render PDFs with poppler-utils + surface a concrete admin failure reason

Merged
JA
1mo ago
+148-11

Summary

The AI status page's "PDF vision/native path" check fails on OpenAI-protocol providers (OpenAI, OpenAI-compatible endpoints, local Ollama, etc.) with a generic request_failed code, even on stock containers.

Root cause: Provider::Openai::PdfProcessor#convert_pdf_to_images shells out to pdftoppm from poppler-utils to render each page as PNG before sending it upstream as an image part. The checked-in Dockerfile (and therefore ghcr.io/we-promise/sure:stable) does not install poppler-utils. The call's return value was never checked, so the subsequent Dir.glob came back empty, the probe raised, and the admin page only showed the ambiguous request_failed code — no hint that a system binary was missing.

Changes

  1. Dockerfile — add poppler-utils to the production apt-get install line so pdftoppm is present. This is the actual fix for the failing check.
  2. app/models/provider.rb — extend Provider::Error with an optional failure_code: keyword (backwards compatible; defaults to nil).
  3. app/models/provider/openai/pdf_processor.rb — capture system("pdftoppm", …)'s return value. When it is false and a command -v pdftoppm lookup confirms the binary is genuinely absent, raise Provider::Openai::Error with failure_code: :render_missing_binary. Any other non-zero exit (corrupt input, OOM, disk-full) falls through to the existing rescue => e behaviour of logging and returning [], so we don't regress today's "bad PDF" path into a new hard error.
  4. app/models/ai_health/probe.rb — failure_code(error) now falls through to the default :timeout / :request_failed logic when the error's failure_code is nil (previously it would return nil straight off the first branch).
  5. config/locales/views/admin/system_health/en.yml — add the render_missing_binary admin-facing message: "The pdftoppm renderer (poppler-utils) is not available in this container".
  6. test/models/provider/openai/pdf_processor_test.rb — add two regression tests:
    • missing binary → Provider::Openai::Error with failure_code == :render_missing_binary and a message mentioning poppler-utils
    • binary present but system returns false for another reason → still returns [] (preserves existing degradation)

Out of scope / not changed

  • Provider::Anthropic::PdfProcessor already uploads the raw PDF as a native document source and never calls pdftoppm, so the Anthropic path is unaffected.
  • The pdf-reader gem (pure-Ruby parser) covers the text-extraction path, which is unaffected.
  • Adding poppler-utils to Gemfile.lock or any other packaging artifact is not required — only the Debian base image needs it.

Verification

Static analysis of the full diff + manual trace of the two test paths. I was unable to run the project's RSpec suite from this environment (no local Ruby toolchain; Docker Hub blocked at the registry layer). The binary_missing? helper uses a command -v probe so it correctly distinguishes binary absent (the new coded error) from pdftoppm failing at runtime (preserved [] fallback).

Notes for reviewers

  • Provider::Error#initialize keeps its existing positional/keyword signature and only adds a new optional kwarg, so existing callers (including the many Provider::Openai::Error.new(...) sites under app/models/provider/openai/**) are unaffected.
  • The new locale key is the single source of truth for the UI string; no other locale files were touched in this repo.
  • I confirmed upstream has no open issue or PR already touching this — the related search (pdftoppm, poppler, #3191) returned zero open issues and no merged PR for this specific gap.

Summary by CodeRabbit

  • Bug Fixes

    • Improved PDF processing when rendering tools are unavailable or reject a document.
    • Added clearer health reporting and diagnostic codes for PDF rendering issues.
    • Preserved graceful handling of invalid PDFs and improved consistency in provider error reporting.
  • Chores

    • Added required PDF rendering support for image-based document processing.
    • Expanded coverage for missing tools, failed conversions, and provider error details.