Skip to main content

Document analyzer

document-analyzer checks documents that a request carries. It blocks a document that is too large or has a type that you do not allow. It reads the text of each document and records counts and SHA-256 digests of that text. With sanitize_code: true, it removes code patterns from the documents that go to the provider. Otherwise, the documents go to the provider unchanged.

It reads custom request fields such as documents, not the standard OpenAI or Anthropic upload fields. Use it with an application and provider that understand those fields.

Configuration​

policy-config.yaml
pack:
  name: document-analyzer-example
  version: 1.0.0
  enabled: true
policies:
  chain:
    - document-analyzer
policy:
  document-analyzer:
    enabled: true
    sanitize_code: false
    max_document_bytes: 524288
    allowed_mime_types:
      - text/plain

Fields​

FieldTypeDefaultDescription
enabledbooleantruefalse skips every check and allows with empty details
sanitize_codebooleanfalseReplaces the built-in code patterns with [redacted code pattern] in the documents that the gateway sends to the provider. Applies to Chat Completions, Responses, and Messages.
max_document_bytesinteger524288Largest decoded size of one document. A larger one blocks the request. Lint rejects 0.
allowed_mime_typeslist of strings[]Exact MIME types to accept. Case matters. An empty list accepts every type.

Phase and verdicts​

ItemValue
Stagespre-request (default), pre-response
Request familiesChat Completions, Completions, Responses, Messages
Verdictsallow, block
Reason codesdocument-analyzer.ok, document-analyzer.code_sanitized, document-analyzer.document_too_large:<name>, document-analyzer.mime_type_not_allowed:<mime_type>

Use a document fixture​

Put the whole request, with its documents, in input.request of a JSON test. The runner ignores a documents field placed directly in input.

tests/document.json
{
  "name": "accepts-plain-text",
  "input": {
    "messages": [{"role": "user", "content": "Inspect the attached note."}],
    "request": {
      "model": "test-model",
      "messages": [{"role": "user", "content": "Inspect the attached note."}],
      "documents": [{"name": "note.txt", "mime_type": "text/plain", "text": "A short public note."}]
    }
  },
  "expected": {"verdict": "allow", "reason_code": "ok"}
}
verdictan policy lint --file policy-config.yaml
verdictan policy test --json

The test passes. The document-analyzer result has a finding for note.txt with extraction_method: text. The details have text_fragment_count: 1 and one SHA-256 digest in text_fragment_digests. The details do not contain the document text.

For a rejection test, copy the file and change mime_type to application/octet-stream. That case is blocked with document-analyzer.mime_type_not_allowed:application/octet-stream.

Supported request shape​

The control reads the arrays documents, attachments, input_documents, and files of the request. In each object:

KeyUseDefault
nameName in the findingsinline-document
mime_type or content_typeType for the allowlist and for extractiontext/plain
textInline text
content_base64 or dataBase64 content, used when text is missing

An object without text, content_base64, or data, or with Base64 that does not decode, is skipped.

How it works​

  1. The fenced code blocks of all request messages become the first text fragments.
  2. The control reads the document objects in the order of the keys above.
  3. A document larger than max_document_bytes blocks with document-analyzer.document_too_large:<name>.
  4. A type not in a non-empty allowed_mime_types blocks with document-analyzer.mime_type_not_allowed:<mime_type>.
  5. The control reads the text. The method depends on the type. See Text extraction.
  6. With sanitize_code, the six built-in code patterns are replaced.
  7. A finding records source, mime_type, extracted_chars, code_findings (the number of fenced code blocks), and extraction_method.
  8. Non-empty text becomes a text fragment.
  9. If nothing blocked, the request is allowed with document-analyzer.ok. If sanitize_code changed a document, the reason code is document-analyzer.code_sanitized.

The details contain text_fragment_count, text_fragment_digests, findings, blocked_reason, and sanitized_documents. Each digest has a sha256 value and a chars count. The details do not contain the extracted text or the sanitized text.

Text extraction​

TypeTextextraction_method
A type that contains wordprocessingml.documentThe text of word/document.xml without tagsdocx
A type that contains pdfPrintable ASCII runs of at least four characterspdf
text/*, or a type that contains json, xml, yaml, csv, javascript, markdown, x-sh, or sqlThe content as UTF-8text
image/*, with the OCR model installedThe English text in the image, from OCRocr
image/*, without the OCR modelNo textunread_image
Another type with valid UTF-8 content and no NUL byteThe content as UTF-8text
Another binary typeNo textunread_binary

To read text in images, install the optional OCR model on the gateway host with verdictan models install ocr. The CLI does not bundle this model. The command downloads tesseract.js-core with English language data, and makes sure that the SHA-256 digest of each file is correct. The gateway never downloads the model. See verdictan models.

While the OCR model is not installed, the control does not read images. When OCR fails or exceeds its time limit, the image also stays unread. The control does not decode other binary types as text.

Examples​

Accept PDF, text, and two image types up to 1 MiB. The control reads text in the images only when the OCR model is installed.

policy-config.yaml
pack:
  name: upload-screening
  version: 1.0.0
  enabled: true
policies:
  chain:
    - audit-logger
    - document-analyzer
policy:
  document-analyzer:
    max_document_bytes: 1048576
    allowed_mime_types:
      - application/pdf
      - text/plain
      - image/png
      - image/jpeg
  audit-logger: {}

Limits and failure behavior​

  • A block returns HTTP 400 with error code content_policy_violation. The first failing document stops the analysis, and the details contain only the findings before it.
  • documents, attachments, input_documents, and files are not part of the OpenAI or Anthropic formats. The gateway forwards them. Only sanitize_code changes them.
  • Compressed PDF streams are not decoded, so text from such a PDF is incomplete.
  • The control does not fetch URLs or read multipart uploads. For URLs, use content-extractor.
  • sanitize_code replaces code patterns, but it does not block the request. To block on code patterns, add code-sanitizer.
  • When sanitize_code changes a PDF or DOCX document, the gateway sends a text/plain document with the sanitized text. The original binary content does not go to the provider. A text document keeps its shape, and Base64 content is encoded again.
  • sanitize_code does not read images. It forwards image documents unchanged.
  • The Completions request family does not get the sanitized documents. sanitize_code rewrites documents on Chat Completions, Responses, and Messages only.
  • Lint rejects unknown fields. A block with a wrong type that bypasses lint runs with enabled: true, sanitize_code: false, max_document_bytes: 524288, and no MIME allowlist.

Next steps​