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
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/plainFields
| Field | Type | Default | Description |
|---|---|---|---|
enabled | boolean | true | false skips every check and allows with empty details |
sanitize_code | boolean | false | Replaces 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_bytes | integer | 524288 | Largest decoded size of one document. A larger one blocks the request. Lint rejects 0. |
allowed_mime_types | list of strings | [] | Exact MIME types to accept. Case matters. An empty list accepts every type. |
Phase and verdicts
| Item | Value |
|---|---|
| Stages | pre-request (default), pre-response |
| Request families | Chat Completions, Completions, Responses, Messages |
| Verdicts | allow, block |
| Reason codes | document-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.
{
"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 --jsonThe 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:
| Key | Use | Default |
|---|---|---|
name | Name in the findings | inline-document |
mime_type or content_type | Type for the allowlist and for extraction | text/plain |
text | Inline text | |
content_base64 or data | Base64 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
- The fenced code blocks of all request messages become the first text fragments.
- The control reads the document objects in the order of the keys above.
- A document larger than
max_document_bytesblocks withdocument-analyzer.document_too_large:<name>. - A type not in a non-empty
allowed_mime_typesblocks withdocument-analyzer.mime_type_not_allowed:<mime_type>. - The control reads the text. The method depends on the type. See Text extraction.
- With
sanitize_code, the six built-in code patterns are replaced. - A finding records
source,mime_type,extracted_chars,code_findings(the number of fenced code blocks), andextraction_method. - Non-empty text becomes a text fragment.
- If nothing blocked, the request is allowed with
document-analyzer.ok. Ifsanitize_codechanged a document, the reason code isdocument-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
| Type | Text | extraction_method |
|---|---|---|
A type that contains wordprocessingml.document | The text of word/document.xml without tags | docx |
A type that contains pdf | Printable ASCII runs of at least four characters | pdf |
text/*, or a type that contains json, xml, yaml, csv, javascript, markdown, x-sh, or sql | The content as UTF-8 | text |
image/*, with the OCR model installed | The English text in the image, from OCR | ocr |
image/*, without the OCR model | No text | unread_image |
| Another type with valid UTF-8 content and no NUL byte | The content as UTF-8 | text |
| Another binary type | No text | unread_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.
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
400with error codecontent_policy_violation. The first failing document stops the analysis, and the details contain only the findings before it. documents,attachments,input_documents, andfilesare not part of the OpenAI or Anthropic formats. The gateway forwards them. Onlysanitize_codechanges 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_codereplaces code patterns, but it does not block the request. To block on code patterns, addcode-sanitizer.- When
sanitize_codechanges a PDF or DOCX document, the gateway sends atext/plaindocument 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_codedoes not read images. It forwards image documents unchanged.- The Completions request family does not get the sanitized documents.
sanitize_coderewrites 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
- Content extractor: documents linked by URL.
- Code sanitizer: block the same code patterns in message text.
- verdictan models: install the optional OCR model.
- Investigate a blocked request: read the
blocked_reasonof a blocked document.