Skip to main content
This cookbook demonstrates how to retrieve document chunks from Morphik and send them to OpenAI for completion generation, using both presigned URLs and base64-encoded images.
Prerequisites
  • Install the Morphik SDK: pip install morphik
  • Install OpenAI SDK: pip install openai
  • Provide credentials via MORPHIK_URI and OPENAI_API_KEY
  • Documents ingested with multimodal support (use_colpali=True)

1. Ingest Documents with Multimodal Support

First, ingest your documents with multimodal retrieval enabled:

2. Retrieve Chunks as Presigned URLs

Get chunks as URLs that can be sent directly to vision models:

3. Send URLs to OpenAI

Send the presigned URLs to OpenAI’s vision model:

4. Retrieve Chunks as Base64 Images

For cases where you need base64-encoded images:

5. Send Base64 Images to OpenAI

Important Notes

Multimodal Ingestion and Retrieval

When you ingest with use_colpali=True, you must retrieve with use_colpali=True:

Content Type Handling

Chunks from PDFs ingested with multimodal support will have:
  • chunk.content_type = "application/pdf" (original document type)
  • chunk.content = PIL Image object (actual content)
When encoding to base64, always use "image/png" as the MIME type:

Output Format Comparison

Use Cases

This pattern is ideal for:
  • Document Q&A over visual documents (PDFs, scans, diagrams)
  • Report generation from technical documentation with charts and tables
  • Visual data analysis combining text and image understanding
  • Multi-document synthesis aggregating information across documents
  • Chart and diagram interpretation using vision-capable models
  • Technical specification review analyzing mixed text-visual content

Best Practices

1. Choose the Right Output Format

Use URLs for production workloads with large images:
Use base64 for small images or offline processing:

2. Handle Chunk Padding

Use padding to include adjacent chunks/pages for better context:

3. Filter with Metadata

Combine retrieval with metadata filtering for precise results:

Running the Example