LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
"""Bridge helpers for the cloud-only VLM image-answer feature. No-ops in OSS.
Image output mode is answered by a vision LLM through the cloud-only
``vlm-image-answer`` plugin. The backend touch points below (profile-save
vision warning, deploy-time validation, answer-cache invalidation on
re-extraction) delegate to ``plugins.vlm_image_answer.backend_hooks`` when
that cloud package is present and degrade to no-ops when it is not — OSS
additionally hides the image output mode entirely via
``adapter_processor_v2.image_output_gating``.
"""
import logging
from typing import Any
logger = logging.getLogger(__name__)
try:
from plugins.vlm_image_answer import backend_hooks as _hooks
VLM_IMAGE_ANSWER_AVAILABLE = True
VLM_HOOKS_BROKEN = False
except ImportError:
_hooks = None
VLM_IMAGE_ANSWER_AVAILABLE = False
# Distinguish "running OSS" (package absent — expected, silent) from
# "cloud hooks are broken" (package present but backend_hooks failed
# to import). The broken state is exported so the consumers of image
# mode fail closed instead of quietly degrading: the adapter gating
# disables the mode and ``dynamic_extractor`` rejects image-mode
# extraction — otherwise a re-extraction would rewrite the page
# images while the stored answers derived from the old pages are
# never invalidated.
try:
import plugins.vlm_image_answer # noqa: F401
except ImportError:
VLM_HOOKS_BROKEN = False
else:
VLM_HOOKS_BROKEN = True
logger.error(
"plugins.vlm_image_answer is present but backend_hooks failed "
"to import — image output mode is disabled (new saves rejected, "
"image-mode extraction blocked) until the install is repaired"
)
def get_profile_vision_warning(profile_manager: Any) -> str | None:
"""Non-blocking warning when an image-mode profile's LLM lacks vision.
Returns a human-readable warning string, or None (always None in OSS).
Never raises — a warning must not break profile save/read.
"""
if not VLM_IMAGE_ANSWER_AVAILABLE:
return None
try:
return _hooks.get_profile_vision_warning(profile_manager)
except Exception:
logger.exception("VLM vision warning check failed; skipping warning")
return None
def validate_workflow_for_deployment(workflow: Any) -> None:
"""Deploy-time guard: reject deployments that cannot serve image mode.
The cloud hook raises ``rest_framework.serializers.ValidationError``
for a definitive misconfiguration (e.g. image-mode profile with a
known non-vision LLM); OSS is a no-op (image mode is gated off).
"""
if not VLM_IMAGE_ANSWER_AVAILABLE:
return
_hooks.validate_workflow_for_deployment(workflow)
def invalidate_vlm_answers_on_reextraction(
document_id: str, profile_manager: Any, extract_file_path: str
) -> None:
"""Invalidate stored VLM answers after a re-extraction rewrote pages/.
Called from the extraction choke point after a successful
(non-cache-hit) extraction, BEFORE the extraction-success marker is
committed. Exceptions propagate — the hook owns its error policy.
The current cloud hook is purely informational (Prompt Studio has no
read-side answer cache; output rows are overwritten per run, the
same semantics text-mode re-extraction has always had) and never
raises. A future hook that performs real invalidation must either
handle its own failures or let them fail the re-extraction loudly:
because the failure lands before the marker commits, a retry re-runs
extraction and invalidation instead of cache-hitting past a
stale-answer state.
"""
if not VLM_IMAGE_ANSWER_AVAILABLE:
return
_hooks.invalidate_vlm_answers_on_reextraction(
document_id=document_id,
profile_manager=profile_manager,
extract_file_path=extract_file_path,
)