OCR

Extract text from images

Agents can read text from images – receipts, documents, signs, and screenshots.

Diagram

Text Extraction

👤 User

🤖 Agent

🖼️ Image

📝 Text

Quick Start

  1. Simple Usage
    typescript
    import { Agent } from 'praisonai';
    
    const agent = new Agent({
      instructions: 'Extract all text from images',
      llm: 'gpt-4o'  // Vision-capable model
    });
    
    await agent.chat([
      { role: 'user', content: [
        { type: 'text', text: 'What text is on this receipt?' },
        { type: 'image', url: 'https://example.com/receipt.jpg' }
      ]}
    ]);
    // "Store: Coffee Shop, Total: $4.50..."
  2. With Configuration
    typescript
    const agent = new Agent({
      instructions: 'Extract data as JSON',
      outputFormat: 'json'
    });
    
    await agent.chat([
      { role: 'user', content: [
        { type: 'text', text: 'Extract items and prices from this receipt' },
        { type: 'image', path: './receipt.jpg' }
      ]}
    ]);
    // { items: [...], total: 4.50, date: '...' }

User Interaction Flow

Diagram
VisionAgentUserVisionAgentUser“Read this receipt”[image attachment]Analyze imageDetected text“Items: Coffee $3.50…”

Configuration Levels

typescript
// Level 1: Bool - Enable with vision model
const agent = new Agent({
  llm: 'gpt-4o',
  vision: true
});

// Level 2: String - High detail for small text
const agent = new Agent({
  llm: 'gpt-4o',
  vision: 'high'
});

// Level 3: Dict - Full options
const agent = new Agent({
  vision: {
    detail: 'high',
    ocr: true,
    language: 'en'
  }
});

Common Uses

Use CaseExample
ReceiptsExtract items, totals, dates
Business cardsGet contact information
DocumentsDigitize scanned papers
ScreenshotsRead displayed text

Dedicated OCRAgent class

OCRAgent calls a real OCR backend through a required extractor function.

Diagram

🤖 OCRAgent

🔌 extractor

🛠️ backend

OCRResult

typescript
import { OCRAgent, type OCRExtractor } from 'praisonai';

const mistralExtractor: OCRExtractor = async ({ source, document, model, options, apiKey, baseUrl }) => {
  // Call your OCR backend (Mistral OCR, Tesseract, Cloud Vision, ...)
  // Return { text, pages: [{ index, markdown }], images?, metadata? }
  return { text: '...', pages: [{ index: 0, markdown: '...' }] };
};

const agent = new OCRAgent({
  name: 'Reader',
  extractor: mistralExtractor,
  model: 'mistral/mistral-ocr-latest',
});

const result = await agent.extract('receipt.jpg');
const textOnly = await agent.read('receipt.jpg');

OCRAgentConfig

FieldTypeDefaultDescription
extractorOCRExtractor—OCR backend. Required at call time — extract() throws without it.
namestring'OCRAgent'Agent name
instructionsstring—Optional instructions
llm / modelstring'mistral/mistral-ocr-latest'OCR model (model is an alias for llm)
baseUrlstring—Custom API endpoint URL
apiKeystring—API key for the provider
ocr`boolean \OCRConfig`—OCR settings; true/false use defaults
verbose`boolean \number`trueLog progress messages

OCRConfig

FieldTypeDefaultDescription
pagesnumber[][]Specific pages to extract (for PDFs)
imageLimitnumber0Maximum images per page
timeoutnumber600Timeout in seconds
apiBasestring''Custom API endpoint URL
apiKeystring''API key for the provider

OCRExtractRequest

The extractor receives this request:

FieldTypeDescription
sourcestringURL or path the caller asked to read
document{ type: string; ... }Provider document reference derived from source
modelstringModel to use for this extraction
options`Required

| Resolved OCR options | | baseUrl | string | Custom API endpoint URL, if configured | | apiKey | string` | API key, if configured |

OCRResult

The extractor returns this shape:

FieldTypeDescription
textstringCombined extracted text
pagesOCRPage[]Per-page results (index, markdown, images?)
imagesstring[]Optional base64 images
metadataRecord<string, any>Optional additional metadata

API Reference

OCRConfig

Complete configuration options

OCRAgent

Full class documentation


Best Practices

Use high detail for small text

Set detail: 'high' when reading receipts or documents.

Be specific about what to extract

“Extract the total and date” works better than “read this”.

Use good quality images

Clear, well-lit images produce better results.


Related