Fixed Size Chunking
Split text into character-limited chunks with optional overlap.
FixedSizeChunking creates chunks up to chunk_size characters and avoids splitting a word when a boundary is available.
Create a Python file
from agno.agent import Agent
from agno.knowledge.chunking.fixed import FixedSizeChunking
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.pdf_reader import PDFReader
from agno.vectordb.pgvector import PgVector
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
knowledge = Knowledge(
vector_db=PgVector(table_name="recipes_fixed_size_chunking", db_url=db_url),
)
knowledge.insert(
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
reader=PDFReader(
name="Fixed Size Chunking Reader",
split_on_pages=False,
chunking_strategy=FixedSizeChunking(),
),
)
agent = Agent(
knowledge=knowledge,
search_knowledge=True,
)
agent.print_response("How do I make Thai curry?", markdown=True)Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno sqlalchemy psycopg pgvector pypdf openaiExport your OpenAI API key
Set OpenAI Key
Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.
export OPENAI_API_KEY=sk-***Run PgVector
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql \
-v pgvolume:/var/lib/postgresql \
-p 5532:5432 \
--name pgvector \
agnohq/pgvector:18Run the script
python fixed_size_chunking.pyThe example sets split_on_pages=False so PDFReader combines the pages before applying FixedSizeChunking. Keep the default value of True to chunk each page independently.
Fixed Size Chunking Params
| Parameter | Type | Default | Description |
|---|---|---|---|
chunk_size | int | 5000 | Maximum characters per chunk. Use a positive value. |
overlap | int | 0 | Characters repeated between adjacent chunks. Use 0 <= overlap < chunk_size. |