SDKsPython

Sync & async

How to use the synchronous and asynchronous BimpeAI clients, manage their lifecycles, and iterate async pages and streams.

Every client exposes four resources: agents, workflows, conversations, and calls. BimpeAI is the synchronous client; AsyncBimpeAI is the asynchronous one. Both share the same surface — the only difference is that async methods return awaitables and async iterables.

Context managers

Both clients open an httpx client and own it for their lifetime. Use them as context managers so the connection pool is closed when you are done.

with BimpeAI(api_key="sk_...") as client:
    client.agents.list()

async with AsyncBimpeAI(api_key="sk_...") as client:
    await client.agents.list()

If you would rather manage the lifetime yourself, call client.close() on the sync client or await client.aclose() on the async one.

You can also hand in your own httpx client through the http_client argument, in which case the SDK uses it and leaves closing it to you.

import httpx

with httpx.Client(proxy="http://localhost:8080") as http:
    client = BimpeAI(api_key="sk_...", http_client=http)
    client.agents.list()

Async iteration

On the async client, iterating a list response and fetching the next page both use async for and await.

async with AsyncBimpeAI(api_key="sk_...") as client:
    async for agent in await client.agents.list(limit=50):
        print(agent.id, agent.name)

Walking page objects:

async with AsyncBimpeAI(api_key="sk_...") as client:
    page = await client.agents.list(limit=50)
    async for page in page.pages():
        print(page.meta.current_page if page.meta else None)
    next_page = await page.get_next_page()

Streaming messages follows the same pattern — async for over the stream yields StreamMessageEvent objects as they arrive.

async with AsyncBimpeAI(api_key="sk_...") as client:
    async for message in client.conversations.messages.stream(agent_id, conversation_id):
        print(message.role, message.message)

On this page