Skip to content

Image description lane intermittently returns invalid JSON #511

Open

Description

@fazpu

What happens

The vision-LLM description lane of the image route (adapters/converters/image_ocr_description.py, default model google/gemini-2.5-flash via OpenRouter) intermittently returns output that does not parse as the expected JSON. The lane then fails and the image conversion has to be retried.

Expected

The lane uses structured output / a JSON schema, or a bounded repair-and-retry inside the lane. Callers should not need to retry whole conversions, and failures should be typed and retryable, with the raw output kept for diagnosis.

Repro

A batch of 35 raster images (e-Sbírka annex scans) through ImageOcrDescriptionConverter. Several attempts failed with invalid JSON from the description lane and succeeded on a retry. The total spent was ~$0.20 for 52 calls including retries, against ~$0.12 for the stored results.

Workaround in the downstream app

LegalIt esbirka/images.py EngineRoutes.read() retries retryable lane errors up to N times. The lane checkpoints keep the OCR lane from being re-billed.

Notes

Activity

  1. fazpu commented on Oct 22, 2026

    @fazpu
    MemberAuthor

    Root cause found: Gemini's RECITATION stop truncates the JSON (2026-10-22)

    This is the same request the description lane sends (pinned baafc0ce prompt and JSON schema, google/gemini-2.5-flash via OpenRouter, max_tokens 4096). I sent it 10 times for one public scanned form.

    • Image: https://e-sbirka.gov.cz/sbr-cache/souborove-dokumenty/831501/NAHLED/STAHNI/x (JPEG, 1832×2586). It is a Czech form with long printed labels.
    • Result: 8 of the 10 calls failed, all in the same way:
      • finish_reason: "error", native_finish_reason: "RECITATION", provider "Google AI Studio", usage completion_tokens: 0;
      • message.content is a JSON object cut off mid-string, ending in the middle of a verbatim quote of the form's text, for example {"observations": "The image is a scan of a document, … The first label is \"Připojeny jednotlivé identifikace předmětů, pro které je požadováno uzavření dohody o poskytnutí náhrady z;
      • json.loads then raises "Unterminated string", and the lane reports "image description completion is not JSON".
    • The other 2 calls: finish_reason: "stop" and valid JSON.

    Expected:

    1. The lane checks finish_reason. An error/RECITATION stop becomes a typed, recognisable failure (with the raw output kept), not "not JSON".
    2. The prompt discourages verbatim quoting of long visible text. OCR already carries the text, and quoting is what trips Google's recitation filter.
    3. If a retry is the remedy, it is bounded.

    How often: in our 35-image batch only text-dense forms hit this; plain seals and maps never did. For the form above the failure rate is 80%.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions