Provides machine learning and computer vision capabilities for document processing. Enables AI-powered document description and content extraction.
from nutrient_sdk import VisionConstruction
Vision cannot be instantiated directly. Obtain instances through static factory methods or via other SDK classes.
Class Methods
classify_text
@classmethoddef classify_text(cls, request: ClassificationRequest, text: str) -> strClassifies caller-supplied text against the candidate labels carried by request (zero-shot) with no document. The text is scored directly by the text branch and the image branch is skipped, so nothing is rendered and no file is opened — use this when you already have the text (an email body, a database field, your own extraction pipeline) rather than a document on disk.
Parameters:
| Name | Type | Description |
|---|---|---|
request | ClassificationRequest | The classification request carrying at least two candidate labels. |
text | str | The text to classify. |
Returns: str - JSON with the predicted label and the ranked candidate probabilities.
decide
@classmethoddef decide(cls, state_json: str, questions_json: str) -> strCalls the nutrient-document-decision model’s native decision primitive directly: full caller control over both the shared state and the questions to answer against it. No document, schema, or template list is involved — this is the raw decision-model API for callers who want calibrated yes/no, multiple-choice, or ordinal-scale answers over their own structured state. The same concepts as llama.cpp’s own /v1/systemone endpoint carry over (a shared state, and per-question type/instructions/options), but the concrete wire shape differs: questionsJson here is a JSON array with each question’s id as a field, not an object keyed by id. usage.promptTokens is exact (summed from the native decode); there is no exact pre-call tokenizer, so budget roughly 1 token per 3-4 characters of rendered state/ instructions text (closer to 1 per 2.5-3 for JSON-heavy state) for cost estimates before calling.
Parameters:
| Name | Type | Description |
|---|---|---|
state_json | str | The shared state every question is evaluated against: a JSON object, array, or plain string. Images are embedded inline as {"type":"image","source":"data:MIME-TYPE;base64,BASE64-DATA"} objects anywhere in the structure. |
questions_json | str | JSON array of questions. Each question has an id, a type (“noul”, “choice”, or “score”), instructions, and options (an array of objects with key and description). “noul” takes 0 options (default true/false wording) or exactly 2 ([true-option, false-option]); “choice”/“score” take 2+ options, each requiring a non-empty “key” for “choice” (“score” options are numbered by position instead). |
Returns: str - JSON: {"answers": {questionId: {type, chosenIndex, chosenKey, confidence, probabilities}}, "usage": {"promptTokens": N, "outputTokens": M}}.
detect_languages_text
@classmethoddef detect_languages_text(cls, text: str) -> str@classmethoddef detect_languages_text(cls, text: str, settings: DocumentSettings) -> strDetects the language of caller-supplied text, fully offline, with no document. CLD3 scores the text directly — nothing is rendered and no file is opened. Use this when you already have the text (an email body, a database field, your own extraction pipeline) rather than a document on disk.
Parameters:
| Name | Type | Description |
|---|---|---|
text | str | The text to detect the language of. |
settings (optional) | DocumentSettings | Document settings; max_languages bounds how many languages are reported. |
Returns: str - JSON with the predicted language and text direction.
generate_schema
@classmethoddef generate_schema(cls, request: SchemaGenerationRequest) -> str@classmethoddef generate_schema(cls, request: SchemaGenerationRequest, settings: DocumentSettings) -> strGenerates a JSON Schema for a class of documents using a vision model, with default document settings. The schema is designed for downstream LLM structured-output extraction tasks: it is hard-clamped to the knobs in SchemaGenerationSettings and to the target structured-output dialect selected there, so the same call with the same settings always yields a schema the target accepts.
Parameters:
| Name | Type | Description |
|---|---|---|
request | SchemaGenerationRequest | The schema generation request: the document type the schema must represent (required), an optional natural-language requirement, and up to five example documents used as grounding samples. |
settings (optional) | DocumentSettings | Document settings carrying SchemaGenerationSettings (schema shape and target dialect) and the vision model connection. |
Returns: str - A JSON envelope object with a schema property (the generated JSON Schema) and a constraints array of cross-field rules in standard JsonLogic. The array is empty unless SchemaGenerationSettings.include_constraints is enabled.
generate_schema_to_file
@classmethoddef generate_schema_to_file(cls, request: SchemaGenerationRequest, output_path: str) -> None@classmethoddef generate_schema_to_file(cls, request: SchemaGenerationRequest, settings: DocumentSettings, output_path: str) -> NoneGenerates a JSON Schema for a class of documents using a vision model and writes it to a file, with default document settings.
Parameters:
| Name | Type | Description |
|---|---|---|
request | SchemaGenerationRequest | The schema generation request: the document type the schema must represent (required), an optional natural-language requirement, and up to five example documents used as grounding samples. |
output_path | str | Path to the output file receiving the JSON Schema. |
settings (optional) | DocumentSettings | Document settings carrying SchemaGenerationSettings (schema shape and target dialect) and the vision model connection. |
set
@classmethoddef set(cls, document: Document) -> VisionCreates a Vision instance for the specified document.
Parameters:
| Name | Type | Description |
|---|---|---|
document | Document | The document to analyze using vision capabilities. |
Returns: Vision - A Vision instance ready to perform analysis on the document. Raises:
NullOrEmptyParameterException: Thrown when document is null.
Methods
classify
def classify(self, request: ClassificationRequest) -> strClassifies the document against the candidate labels carried by request (zero-shot) and exports the ranked result. Branch weights, supplied text, and the other knobs are read from DocumentClassificationSettings on the document’s settings. The result is always JSON; output_format is not consulted because only the JSON exporter serializes the classification result.
Parameters:
| Name | Type | Description |
|---|---|---|
request | ClassificationRequest | The classification request carrying at least two candidate labels. |
Returns: str - The exported content as a string (JSON), including the predicted label and ranked candidate probabilities.
classify_to_file
def classify_to_file(self, request: ClassificationRequest, output_path: str) -> NoneClassifies the document against the candidate labels carried by request (zero-shot) and writes the exported result to a file. The result is always JSON; output_format is not consulted because only the JSON exporter serializes the classification result.
Parameters:
| Name | Type | Description |
|---|---|---|
request | ClassificationRequest | The classification request carrying at least two candidate labels. |
output_path | str | Path to the output file. |
describe
def describe(self) -> strGenerates an AI-powered description of the document content.
Returns: str - A string containing the document description.
detect_forms
def detect_forms(self) -> strDetects form fields on the document and exports the result. Output format is determined by output_format (JSON or IR Lite). Each detected field carries its type and bounding box. To also assign AI semantic labels (e.g. “First name”), set FormLabelingSettings.enable_ai_labeling on the document’s settings before calling — no separate method is needed.
Returns: str - The exported content as a string (JSON or IR Lite JSON depending on settings).
detect_forms_to_file
def detect_forms_to_file(self, output_path: str) -> NoneDetects form fields on the document and writes the exported result to a file. Output format is determined by output_format (JSON or IR Lite).
Parameters:
| Name | Type | Description |
|---|---|---|
output_path | str | Path to the output file. |
detect_languages
def detect_languages(self) -> strDetects the language and text direction of the document, fully offline, and exports the result. Runs the offline cascade (Tesseract OSD for script → script-model OCR → fastText for the specific language) on every page and reports one detection per page. Output format is determined by output_format.
Returns: str - JSON with the predicted language, text direction, and per-page detections.
detect_languages_to_file
def detect_languages_to_file(self, output_path: str) -> NoneDetects the language and text direction of the document, fully offline, and writes the exported result to a file. Output format is determined by output_format.
Parameters:
| Name | Type | Description |
|---|---|---|
output_path | str | Path to the output file. |
extract_content
def extract_content(self) -> strdef extract_content(self, settings: DocumentLayoutJsonExportSettings) -> strExtracts structured content from the document using machine vision processing. The pipeline used is determined by the engine setting and the output format by output_format.
Parameters:
| Name | Type | Description |
|---|---|---|
settings (optional) | DocumentLayoutJsonExportSettings | Settings controlling what to include in the JSON output. |
Returns: str - The exported content as a string (JSON, Markdown, or IR Lite JSON depending on settings).
extract_content_to_file
def extract_content_to_file(self, output_path: str) -> Nonedef extract_content_to_file(self, output_path: str, settings: DocumentLayoutJsonExportSettings) -> NoneExtracts structured content from the document and writes it to a file. The pipeline used is determined by the engine setting and the output format by output_format.
Parameters:
| Name | Type | Description |
|---|---|---|
output_path | str | Path to the output file. |
settings (optional) | DocumentLayoutJsonExportSettings | Settings controlling what to include in the JSON output. |
extract_structured
def extract_structured(self, request: StructuredExtractionRequest) -> strExtracts structured data from the document, shaped to the JSON Schema carried by the request’s {"schema": ...} envelope. The document is first read by the extraction pipeline selected by engine, then an AI model fills the schema from the recognized content. Provider, model, endpoint, and confidence reporting are driven by AiProcessingSettings on the document’s settings.
Parameters:
| Name | Type | Description |
|---|---|---|
request | StructuredExtractionRequest | The extraction request carrying the schema envelope (required) and optional instructions. |
Returns: str - A JSON string with two top-level nodes: extraction (the schema-shaped extracted fields) and metadata (per-field source locations and grounding labels).
extract_structured_to_file
def extract_structured_to_file(self, request: StructuredExtractionRequest, output_path: str) -> NoneExtracts structured data from the document, shaped to the JSON Schema carried by the request’s {"schema": ...} envelope, and writes the JSON result to a file. See extract_structured for the result shape.
Parameters:
| Name | Type | Description |
|---|---|---|
request | StructuredExtractionRequest | The extraction request carrying the schema envelope (required) and optional instructions. |
output_path | str | Path to the output file. |
split
def split(self) -> strSplits a merged document into its constituent sub-documents (page-stream segmentation) and exports the result. Every page is scored for whether it starts a new sub-document — fusing a per-page image signal with the page’s text — and the contiguous page ranges are returned. The threshold and the other knobs are read from DocumentSplitSettings on the document’s settings. The result is always JSON; output_format is not consulted because only the JSON exporter serializes the split result.
Returns: str - The exported content as a string (JSON), carrying the detected sub-document segments (1-based inclusive page ranges with their boundary confidence).
split_to_file
def split_to_file(self, output_path: str) -> NoneSplits a merged document into its constituent sub-documents (page-stream segmentation) and writes the exported result to a file. The result is always JSON; output_format is not consulted because only the JSON exporter serializes the split result.
Parameters:
| Name | Type | Description |
|---|---|---|
output_path | str | Path to the output file. |
warmup
def warmup(self) -> NonePreloads (warms up) all resources needed for vision processing. This downloads all model files based on the document’s VisionSettings before execution. Call this to avoid download delays during extract_content().