Skip to main content

Azure OpenAI

This guide shows how to connect PDF4LLM’s extraction output to Azure OpenAI — specifically, how to embed extracted text using text-embedding-3-small (or equivalent) and how to pass document content to a chat completion endpoint for summarisation, Q&A, and RAG. The guide assumes you have an active Azure OpenAI resource with at least one deployment for an embedding model and one for a chat model (e.g. gpt-4o).

Prerequisites

Install the Azure OpenAI .NET SDK alongside PDF4LLM:
You will need:

Client setup

Store credentials in environment variables or a secrets manager — never hardcode them in source files.
The most common use of PDF4LLM with Azure OpenAI is building a searchable index from document content: extract text, split into chunks, embed each chunk, and store embeddings for later retrieval.

Step 1 — Extract and chunk

Step 2 — Embed each chunk

Azure OpenAI rate limits vary by tier. For documents with many chunks, add a small delay between requests or use GenerateEmbeddingsAsync with a batch of inputs rather than one call per chunk.

Step 3 — Batch embedding for efficiency

The GenerateEmbeddingsAsync overload accepts a list of inputs, reducing round-trips:

Pattern 2 — Retrieval-augmented generation (RAG)

A full RAG pipeline has three stages: ingest (embed and store), retrieve (find relevant chunks for a query), and generate (pass retrieved chunks to the LLM). PDF4LLM handles the extraction step in the ingest stage.

Ingest

Generate — pass retrieved chunks to gpt-4o


Pattern 3 — Summarisation

For summarising a document or a set of pages, pass the extracted Markdown directly to a chat completion without embedding:
For long documents that exceed the model’s context window, summarise page-by-page and then summarise the summaries:

Pattern 4 — Multimodal: PDF pages with images

For documents where images carry meaningful information — technical diagrams, charts, infographics — embed images alongside text using gpt-4o’s vision capability:
Not all Azure OpenAI deployments support vision input. Confirm that your gpt-4o deployment has the vision capability enabled in Azure OpenAI Studio before using this pattern.

Pattern 5 — Form data extraction and LLM enrichment

Combine structured form field extraction with an LLM call to normalise, validate, or enrich the extracted values:

Token budgeting

Every pattern above passes text to an Azure OpenAI endpoint that has a token limit per request. Keep these constraints in mind: A token is approximately 4 characters for English text. A typical A4 page of dense text is 400–600 tokens. To stay safely within limits, estimate chunk token counts before sending:

Error handling

Azure OpenAI requests can fail due to rate limits, transient network errors, or content filtering. Wrap requests in retry logic:

Using Azure Managed Identity

For production deployments, prefer Managed Identity over API keys to avoid storing credentials:
Assign the Cognitive Services OpenAI User role to the managed identity in the Azure Portal, or via the Azure CLI:

Troubleshooting

RequestFailedException with status 401 The API key is incorrect, expired, or the endpoint URL does not match the key’s resource. Verify both in the Azure Portal under Keys and Endpoint. RequestFailedException with status 429 (Too Many Requests) You have exceeded the tokens-per-minute or requests-per-minute quota for your deployment. Apply the retry-with-backoff pattern above, reduce batch sizes, or request a quota increase in Azure OpenAI Studio. RequestFailedException with status 400 on embedding calls The input text exceeds the embedding model’s token limit (8 191 tokens). Reduce chunk sizes — the text being embedded is too long for a single embedding call. Content filter triggered (status 400 with a content filter error code) Azure OpenAI applies content filtering by default. If document content triggers the filter, the request fails with a content filter error rather than a rate limit error. Check ex.ErrorCode to distinguish. For legitimate documents triggering false positives, content filter configuration can be adjusted in Azure OpenAI Studio under Content Filters. Empty or low-quality embedding results Very short chunks (fewer than ~20 tokens) and very long chunks (over 512 tokens) both produce lower-quality embeddings. The short-chunk problem is common for page separators and headers extracted as standalone chunks — filter them with a minimum length check. The long-chunk problem requires splitting before embedding. Managed Identity auth fails locally DefaultAzureCredential works in managed environments but requires az login locally. Run az login in your terminal, or switch to AzureCliCredential explicitly for local development:

Next steps

PdfExtractor

Complete method signatures and parameters.

Extract JSON

Access bounding boxes and layout data for custom pipelines.

Image Extraction

Extracting images for multimodal model input.

OCR

Control OCR behaviour and language configuration.