-
Notifications
You must be signed in to change notification settings - Fork 698
Description
Summary
The current Python SDK only provides synchronous methods, which creates significant challenges when integrating PageIndex into production async web frameworks like FastAPI, Starlette, or any asyncio-based application.
Problem
When using the sync SDK in an async context, we're forced to use asyncio.to_thread():
from pageindex import PageIndexClient
@app.post("/chat")
async def chat(query: str):
client = PageIndexClient(api_key=API_KEY)
response = await asyncio.to_thread(
client.chat_completions,
messages=[{"role": "user", "content": query}],
doc_id=["doc-1"]
)
return responseThis approach has several issues:
- Thread pool exhaustion - Python's default thread pool is limited (~40 threads)
- Increased latency - Thread context switching adds overhead
- Resource inefficiency - Threads blocked waiting for I/O
- Scalability limits - Cannot handle high concurrent request volumes
Use Case
We're building a Vietnamese administrative procedures (TTHC) chatbot using FastAPI and LangGraph (async-first). PageIndex's reasoning-based retrieval is excellent for our complex legal documents, but the sync-only SDK is a blocker for production deployment.
Proposed Solution
Add an async client alongside the existing sync client:
from pageindex import AsyncPageIndexClient
async def main():
client = AsyncPageIndexClient(api_key=API_KEY)
response = await client.chat_completions(
messages=[{"role": "user", "content": query}],
doc_id=["doc-1"]
)
tree = await client.get_tree(doc_id)
status = await client.get_document_status(doc_id)Implementation suggestion: Use httpx.AsyncClient instead of requests for HTTP calls.
Alternatives Considered
- Thread pool approach - Works but doesn't scale
- Build custom async wrapper - Maintenance burden
- Use different RAG solution - Would lose PageIndex's reasoning capabilities
Environment
- Python SDK version: 0.2.4
- Python version: 3.12
- Framework: FastAPI + LangGraph
Additional Context
Many modern Python API SDKs provide both sync and async clients (OpenAI, Anthropic, Stripe). This pattern is well-established and would make PageIndex more accessible to the async Python ecosystem.
Happy to contribute a PR if the team provides guidance on the preferred approach.