Skip to main content
The retrieveChunks / retrieve_chunks method allows you to retrieve relevant document chunks from your ingested documents using semantic search. It returns the most relevant content for your query, making it ideal for building custom applications with your preferred LLM.

Method Overview

Sync Method

client.sources.retrieve_chunks()

Async Method

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

Method Signature

Parameters

Response Object

The method returns a SourceRetrieveChunksResponse object with the following properties:

Chunk Object

Each chunk in the chunks list contains:

Code Examples

Basic Retrieval

Retrieval from Specific Documents (using file_ids)

Retrieval from Specific Documents (using file_names - deprecated)

Async Retrieval

Error Handling

Using Retrieved Chunks with Your LLM

This method is designed to give you full control over how retrieved chunks are used as context. Here’s how to integrate it with your preferred LLM.

With OpenAI

With Anthropic Claude

With Google Gemini

Advanced Examples

Document Search Interface

Build a document search that shows relevant excerpts:
Search multiple queries efficiently with async:

RAG with Source Citations

Build a RAG system that properly cites sources:

Multi-Step Reasoning

Build a system that retrieves, reasons, and retrieves again if needed:

Use Cases

Custom RAG Applications

Use this method to build custom LLM features with full control over retrieval + generation:
  1. Retrieve — Get relevant chunks using semantic search
  2. Build context — Format chunks for your LLM prompt
  3. Generate — Create answers using any LLM (OpenAI, Anthropic, Google, etc.)
Build document search interfaces that show relevant excerpts:
  1. Query for relevant content
  2. Display chunks with file names and page numbers
  3. Allow users to navigate to source documents

Knowledge Base Q&A

Create custom Q&A systems with full control over:
  • Prompt engineering
  • Response formatting
  • Source citations
  • Multi-step reasoning

Comparison with Chat SDK

Use retrieveChunks() / retrieve_chunks() when you need full control over retrieval + generation. Use ask() for quick Q&A with built-in answer generation.

Best Practices

Query Optimization

  1. Be specific — Clear, specific queries return more relevant chunks
  2. Use natural language — Write queries as you would ask a person
  3. Include key terms — Include important keywords from the domain

Filtering and Scoring

  1. Use file_ids — Restrict to specific documents when you know the source
  2. Filter by score — Consider filtering chunks with low relevance scores
  3. Limit results — Process only the top N most relevant chunks

Context Building

  1. Include metadata — Add file names and page numbers to your context
  2. Order by relevance — Put highest-scoring chunks first
  3. Limit context size — Don’t exceed your LLM’s context window

Error Handling

  1. Handle empty results — Check if chunks were returned
  2. Implement retries — Use the SDK’s built-in retry mechanism
  3. Set timeouts — Configure appropriate timeouts for your use case

Error Reference

Troubleshooting

Causes: Query too specific, no matching content, or files not processedSolutions:
  • Try a broader, more general query
  • Verify files have been uploaded and processed
  • Remove file_names filter to search all documents
  • Check document processing status with client.sources.list()
Causes: Query doesn’t match document content wellSolutions:
  • Rephrase the query using terms from your documents
  • Be more specific about what you’re looking for
  • Check if documents contain the information you need
Causes: Incorrect file name or file not in projectSolutions:
  • Verify exact file names (case-sensitive)
  • Use client.sources.list() to see available files
  • Ensure files have been successfully processed
Causes: Large document corpus or complex queriesSolutions:
  • Use file_names or file_ids to limit search scope
  • Increase timeout value
  • Process queries in batches if needed

Next Steps

After mastering document retrieval:

Chat with Documents

Use built-in LLM for quick Q&A without custom integration

Extract Structured Data

Extract structured data from documents using JSON Schema

Upload Documents

Add more documents to your knowledge base