Skip to main content
The extract method allows you to extract structured information from your documents using standard JSON Schema and natural language instructions. This is ideal for document processing pipelines that need to convert unstructured documents into structured data.

Method Overview

Sync Method

client.sources.extract()

Async Method

await client.sources.extract() (using AsyncGraphor)

Method Signature

At least one of file_ids or file_names must be provided. file_ids is preferred.

Parameters

*At least one of file_ids or file_names must be provided. file_ids is preferred.

Thinking Level

The thinking_level parameter controls the model and thinking configuration used for extraction:

Output Schema

The output_schema parameter accepts a standard JSON Schema object. This defines the structure of the data you want to extract from your documents.
  • Basic types: string, number, integer, boolean
  • Object types: Nested objects with properties
  • Array types: Lists with items schema
  • Null unions: ["string", "null"] for optional fields
  • Required fields: Specify mandatory properties with required array
  • Descriptions: Help the model understand what to extract
  • oneOf, anyOf, allOf combinators
  • $ref references
  • Complex regex patterns
  • External schema references

Response Object

The method returns a SourceExtractResponse object with the following properties:

Code Examples

Basic Extraction

Basic Extraction (using file_names - deprecated)

Using Thinking Level

Control the model’s reasoning depth with thinking_level:

Extraction with Nested Objects and Arrays

Async Extraction

Multi-File Extraction

Error Handling

Schema Examples

Invoice Extraction

Contract Analysis

Resume Parsing

Product Catalog

Advanced Examples

Document Extraction Pipeline

Build a complete extraction pipeline for processing multiple documents:

Async Batch Extraction

Process many documents efficiently with async:

Extraction with Validation

Add validation to ensure extracted data meets your requirements:

Debugging with Raw JSON

Use the raw_json field to debug extraction issues:

Best Practices

Schema Design

  1. Use clear descriptions: Detailed property descriptions improve extraction accuracy
  2. Match types to data: Use number for amounts, string for dates, boolean for flags
  3. Keep nesting shallow: Avoid deeply nested structures (2-3 levels maximum)
  4. Define required fields: Use the required array to specify mandatory properties
  5. Use arrays for lists: Extract repeating items using arrays with item schemas

Instruction Writing

  1. Be specific: Include format preferences (e.g., “Use YYYY-MM-DD for dates”)
  2. Handle edge cases: Specify what to do for missing data (e.g., “Use null if not found”)
  3. Provide context: Explain what the document contains and what you need
  4. Avoid ambiguity: Use clear, unambiguous language

Performance

  1. Batch related files: Process related documents together for context
  2. Use appropriate timeouts: Extraction can take time for complex documents
  3. Implement retries: Handle transient errors with the SDK’s retry mechanism
  4. Cache results: Store extraction results to avoid reprocessing

Error Reference

Troubleshooting

Causes: File doesn’t exist, hasn’t been processed, or wrong file nameSolutions:
  • Verify the exact file name (case-sensitive)
  • Ensure the file has been uploaded and processed
  • Use client.sources.list() to check available files
Causes: Malformed JSON Schema or unsupported featuresSolutions:
  • Validate your schema against JSON Schema spec
  • Avoid unsupported features ($ref, oneOf, anyOf)
  • Ensure all type values are valid
  • Check that properties is an object, not an array
Causes: Large documents, complex schemas, or server loadSolutions:
  • Increase the timeout value
  • Simplify the schema (fewer fields, shallower nesting)
  • Process smaller document batches
Causes: Vague instructions, poor document quality, or inappropriate schemaSolutions:
  • Make instructions more specific
  • Reprocess the document with a better partition method
  • Add more context in property descriptions
  • Check the raw_json field for debugging
Causes: Document doesn’t contain all expected informationSolutions:
  • Make non-essential fields optional (remove from required)
  • Use null unions: "type": ["string", "null"]
  • Add instructions for handling missing data

Next Steps

After extracting data from your documents:

Upload Source

Upload new documents for extraction

Parse Source

Reprocess documents for better extraction quality

Chat with Documents

Ask questions about your documents

Data Extraction Guide

Learn schema design and extraction best practices