Skip to content

VisionLanguageModel: ChatOptions, incomplete-reply check, reply decoding and page rendering - #102

Open
luisquintanilla wants to merge 2 commits into
mainfrom
luisquintanilla/vlm-options-reply-decoding-page-rendering
Open

luisquintanilla wants to merge 2 commits into
mainfrom
luisquintanilla/vlm-options-reply-decoding-page-rendering

Conversation

@luisquintanilla

Copy link
Copy Markdown
Contributor

Implements #101. It adds four options to VisionLanguageModelDocumentExtractionClientOptions, so document OCR models work through any IChatClient with no new dependencies. Examples are LlamaIndex OCRia-1, olmOCR and Granite Docling, run behind Ollama, llama.cpp, an OpenAI-compatible server or Azure OpenAI.

The client stays sealed and keeps its existing types internal. The only new public types are two options groups and RenderedPage. CTK ships no renderer and no parser; the app supplies both.

API

public sealed class VisionLanguageModelDocumentExtractionClientOptions
{
    public string? Prompt { get; set; }                       // "" now sends only the document
    public bool UseStructuredOutput { get; set; }
    public ChatOptions? ChatOptions { get; set; }              // new: copied for every request
    public bool ThrowOnIncompleteReply { get; set; } = true;   // new
    public ReplyDecodingOptions? ReplyDecoding { get; set; }   // new: free-form only
    public PageRenderingOptions? PageRendering { get; set; }   // new
}

public sealed class ReplyDecodingOptions(DocumentExtractionClient decoder)
{
    public DocumentExtractionClient Decoder { get; }
    public string MediaType { get; set; } = "text/markdown";
}

public sealed class PageRenderingOptions(
    Func<Stream, string, CancellationToken, IAsyncEnumerable<RenderedPage>> renderer)
{
    public Func<Stream, string, CancellationToken, IAsyncEnumerable<RenderedPage>> Renderer { get; }
    public IList<string> MediaTypes { get; } = ["application/pdf"];
}

public sealed class RenderedPage(DataContent image, ExtractionDocumentSourcePage page)
{
    public DataContent Image { get; }
    public ExtractionDocumentSourcePage Page { get; }
}

Usage with an OCR model:

using var extractor = new VisionLanguageModelDocumentExtractionClient(chatClient, new()
{
    Prompt = "",                                                    // OCR models are trained without an instruction
    ChatOptions = new() { MaxOutputTokens = 8192, Temperature = 0 },
    PageRendering = new(RenderPdfAsync),                            // e.g. a ~20-line PDFtoImage renderer (README)
    ReplyDecoding = new(markdownDecoder),                           // any DocumentExtractionClient that reads text/markdown
});

Behavior

Option Behavior
Prompt = "" Sends only the image or PDF. Before this change an empty TextContent was sent too.
ChatOptions Cloned for each request. ExtractorName, when set, replaces ModelId. Structured mode replaces ResponseFormat.
ThrowOnIncompleteReply A FinishReason other than Stop (for example Length or ContentFilter) throws InvalidDataException, naming the page and, for Length, MaxOutputTokens. Structured mode always throws. A null finish reason and an empty reply that finished with Stop are accepted; see the deviations below.
ReplyDecoding Each non-blank free-form reply goes to the decoder as a UTF-8 stream of MediaType. Sections and elements the decoder returns without sources are attributed to the reply, and RawRepresentation is the ChatResponse. The decoder is borrowed. Combining it with UseStructuredOutput throws ArgumentException at construction.
PageRendering Inputs whose media type is in MediaTypes are rendered. Each page is one request, in order, and sections reference the renderer's Page (real page number and pixel size). The merged RawRepresentation is List<ChatResponse>. Other media types are sent unchanged, and rendered images must be image/*.

Breaking changes (package is experimental, CTDE0001)

Deviations from the #101 text, found while implementing

  1. An empty reply is not an error. An empty reply that finished normally is a blank page, and two existing tests assert this. That is common once pages are rendered, so only the finish reason decides, and blank replies are not passed to the decoder.
  2. Rendered pages are the source frames. [DocumentExtraction] Vision language model: document models as settings, plus reply-decoding and page-rendering middleware #101 only filled in unsourced sections. Here every section from a page references RenderedPage.Page, including in structured mode, where page indexes the model reports aren't used for provenance.
  3. The merged RawRepresentation is List<ChatResponse> rather than a list of per-page documents. It matches the single-call case and keeps finish reasons and usage reachable.
  4. RenderedPage is a sealed class, not a record, so its constructor can validate both arguments.

Out of scope, as a follow-up: #101 Q5, requiring cells in structured tables. The structured e2e run below shows why it matters.

Validation

Unit tests: VisionLanguageModel.UnitTests 189/189 pass: 19 new tests, plus 2 updated for the intended Prompt = "" and export-list changes. They cover:

DocumentExtraction.slnf builds with no warnings.

End to end: I packed this branch as 1.0.0-preview.local1 and ran it from a file-based app. The renderer is the README sample verbatim; the decoder is a community-style Markdig client. Inputs are a ParseBench page (financial_tables_infosys6…page1.pdf) and a 7-page scanned PDF.

Backend / model Options Result
Azure OpenAI gpt-6-luna rendering + Markdown decoding stop, 1 page at 1650×1275 px, 3 tables (4×4, 3×4, 6×12), 11 s
Azure OpenAI gpt-6-luna same, 7-page scanned PDF 7 requests, all stop; 7 sections each referencing its own page; 15 headings, 6 tables; 50 s
Azure OpenAI gpt-6-luna MaxOutputTokens = 200 InvalidDataException: '…page1.pdf' page 1: the model stopped with finish reason 'length', so the reply is incomplete. Increase ChatOptions.MaxOutputTokens.
Azure OpenAI gpt-6-luna rendering + structured stop, 3 tables all returned as literal tables with no cells (Q5)
Ollama llamaindex/ocria-1 (OllamaSharp) Prompt = "", Temperature = 0, rendering + Markdown decoding stop, 2,525 tokens, correct page provenance. OCRia-1 replies with HTML <table>s inside a code fence, so a Markdown decoder keeps them as text. An HTML decoder (ReplyDecoding.MediaType = "text/html") is the right choice for this model, which is why the parser stays pluggable.
Ollama qwen2.5vl:3b (OllamaSharp) MaxOutputTokens = 4096, rendering Repeated output hit the limit. Ollama's length now throws, where before truncated text was returned silently.

Docs

  • The provider README has a new "Transcribe with a document OCR model" section with a complete renderer, and updated defaults, incomplete-reply behavior and limitations.
  • The skill drops the manual FinishReason.Length gate and lists the new options.
  • The DocumentExtraction README's provider row is updated.

Luis Quintanilla and others added 2 commits November 30, 2026 22:39
…ecoding and page rendering

Adds options to VisionLanguageModelDocumentExtractionClientOptions so document
OCR models (OCRia-1, olmOCR, Granite Docling) work through any IChatClient:

- Prompt = "" sends only the document; ChatOptions are copied per request.
- ThrowOnIncompleteReply (default true): a non-Stop finish reason throws
  InvalidDataException; structured mode always throws.
- ReplyDecoding: hand free-form replies to an app-supplied
  DocumentExtractionClient (for example a Markdown or HTML parser).
- PageRendering + RenderedPage: send caller-rendered page images, one request
  per page, with the renderer's pages as source provenance.

Refs #101

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Refs #101

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant