LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
import logging
from typing import Any
from adapter_processor_v2.adapter_processor import AdapterProcessor
from adapter_processor_v2.deprecated_adapters import (
get_deprecation_message,
is_adapter_deprecated,
)
from adapter_processor_v2.models import AdapterInstance
from rest_framework.serializers import ValidationError
from backend.serializers import AuditSerializer
from prompt_studio.prompt_profile_manager_v2.constants import ProfileManagerKeys
from prompt_studio.vlm_utils import get_profile_vision_warning
from .models import ProfileManager
logger = logging.getLogger(__name__)
# Adapter FK -> "conf" response key read by the UI.
ADAPTER_LABELS = (
(ProfileManagerKeys.LLM, "LLM"),
(ProfileManagerKeys.EMBEDDING_MODEL, "Embedding Model"),
(ProfileManagerKeys.VECTOR_STORE, "Vector Store"),
(ProfileManagerKeys.X2TEXT, "Text Extractor"),
)
class ProfileManagerSerializer(AuditSerializer):
class Meta:
model = ProfileManager
fields = "__all__"
# Dropped so a duplicate create surfaces the view's DuplicateData.
validators = []
def validate_prompt_studio_tool(self, value):
"""Refuse reparenting: the gate authorises against the stored parent."""
if self.instance and value != self.instance.prompt_studio_tool:
raise ValidationError("A profile cannot be moved to another project.")
return value
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
"""Reject a change to an adapter the requester cannot access or use.
An unchanged value passes, so a co-owner can still save a profile
that points at an adapter shared only with the owner, and a profile
already on a deprecated adapter stays editable in its other fields.
"""
request = self.context.get("request")
if not request:
return attrs
accessible = AdapterInstance.objects.for_user(request.user)
for field, _ in ADAPTER_LABELS:
adapter = attrs.get(field)
if not adapter or adapter == getattr(self.instance, field, None):
continue
if not accessible.filter(id=adapter.id).exists():
raise ValidationError({field: "No access to the selected adapter."})
if not adapter.is_available or is_adapter_deprecated(adapter.adapter_id):
raise ValidationError(
{field: get_deprecation_message(adapter.adapter_id)}
)
return attrs
def to_representation(self, instance): # type: ignore
"""Resolve the adapter FKs to the name, model and icon the UI renders.
Not filtered by adapter access - display data only, no credentials.
"""
rep: dict[str, Any] = super().to_representation(instance)
conf: dict[str, str] = {}
for field, label in ADAPTER_LABELS:
adapter = getattr(instance, field)
if not adapter:
continue
# Keep the id for adapters the viewer cannot access.
rep[f"{field}_id"] = str(rep[field])
rep[field] = adapter.adapter_name
conf[label] = AdapterProcessor.get_model_label(adapter)
if field == ProfileManagerKeys.LLM:
rep["icon"] = AdapterProcessor.get_icon(adapter)
if conf:
conf["Profile Name"] = instance.profile_name
rep["conf"] = conf
# Non-blocking image-mode/vision-LLM mismatch warning (cloud-only;
# always None in OSS — key omitted).
vision_warning = get_profile_vision_warning(instance)
if vision_warning:
rep["vision_warning"] = vision_warning
return rep