Skip to content

[Feature Request] Add async support to Python SDK for production use with FastAPI/async frameworksΒ #68

@minhluan1590

Description

@minhluan1590

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 response

This 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

  1. Thread pool approach - Works but doesn't scale
  2. Build custom async wrapper - Maintenance burden
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions