Skip to main content

Schema Design

At the core of LlamaExtract is the schema, which defines the structure of the data you want to extract from your documents.

Tips & Best Practices

  • Try to limit schema nesting to 3-4 levels.
  • Make fields optional when data might not always be present. Having required fields may force the model to hallucinate when these fields are not present in the documents.
  • When you want to extract a variable number of entities, use an array type. However, note that you cannot use an array type for the root node.
  • Use descriptive field names and detailed descriptions. Use descriptions to pass formatting instructions or few-shot examples.
  • Above all, start simple and iteratively build your schema to incorporate requirements.

Hitting "The response was too long to be processed" Error

This implies that the extraction response is hitting output token limits of the LLM. In such cases, it is worth rethinking the design of your schema to enable a more efficient/scalable extraction. e.g.

  • Instead of one field that extracts a complex object, you can use multiple fields to distribute the extraction logic.
  • You can also use multiple schemas to extract different subsets of fields from the same document and merge them later.

Another option (orthogonal to the above) is to break the document into smaller sections and extract from each section individually, when possible. LlamaExtract will in most cases be able to handle both document and schema chunking automatically, but there are cases where you may need to do this manually.

Defining Schemas (Python SDK)

Schemas can be defined using either Pydantic models or JSON Schema:

from pydantic import BaseModel, Field
from typing import List, Optional


class Experience(BaseModel):
company: str = Field(description="Company name")
title: str = Field(description="Job title")
start_date: Optional[str] = Field(description="Start date of employment")
end_date: Optional[str] = Field(description="End date of employment")


class Resume(BaseModel):
name: str = Field(description="Candidate name")
experience: List[Experience] = Field(description="Work history")

Using JSON Schema

schema = {
"type": "object",
"properties": {
"name": {"type": "string", "description": "Candidate name"},
"experience": {
"type": "array",
"description": "Work history",
"items": {
"type": "object",
"properties": {
"company": {
"type": "string",
"description": "Company name",
},
"title": {"type": "string", "description": "Job title"},
"start_date": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"description": "Start date of employment",
},
"end_date": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"description": "End date of employment",
},
},
},
},
},
}

agent = extractor.create_agent(name="resume-parser", data_schema=schema)