The Chunkr AI API follows a task-based pattern where you create a task and monitor its progress through polling. The poll() method handles this by automatically checking the task’s status at regular intervals until it transitions out of the Starting or Processing states.

After polling completes, it’s important to verify the final task status, which will be one of:

  • Succeeded: Task completed successfully
  • Failed: Task encountered an error
  • Cancelled: Task was manually cancelled

Synchronous Usage

When you have a TaskResponse object, you can poll it. Look at creating and getting a task for more information on how to get a TaskResponse object.

from chunkr_ai import Chunkr

# Initialize client
chunkr = Chunkr()

try:
    # Given that you already have a task object, you can poll it
    task.poll()  
    print(task.output.chunks)
finally:
    # Clean up when done
    chunkr.close()

Asynchronous Usage

For async applications, use await:

from chunkr_ai import Chunkr
import asyncio

async def process_document():
    # Initialize client
    chunkr = Chunkr()

    try:
        # Given that you already have a task object
        await task.poll()  
        print(task.output.chunks)
    finally:
        # Clean up when done
        chunkr.close()

Error Handling

By default, failed tasks i.e. task.status == "Failed" will not raise exceptions. You can configure this behavior using the raise_on_failure parameter when initializing the client:

from chunkr_ai import Chunkr

# Initialize client with automatic error raising
chunkr = Chunkr(raise_on_failure=True)