Async vs Sync (Python only)
Learn when to use the sync and async Noukai clients.
Node.js only: The Node.js SDK is async-first. All methods return promises and use await or .then(). Skip this guide if you're using Node.
The Python SDK provides two clients: Noukai (sync) and AsyncNoukai (async). Both have the same API — choose based on your environment.
Sync client (Noukai)
Use the sync client for scripts, command-line tools, and Jupyter notebooks:
Advantages:
- Simple and familiar (no
async/await) - Works in Jupyter notebooks
- Good for one-off scripts and testing
- Automatic resource cleanup via context manager
Limitations:
- Cannot iterate (no
steps()orevents()) — useexecute()instead - Blocks the calling thread
- Not suitable for high-concurrency servers
Async client (AsyncNoukai)
Use the async client for FastAPI applications, aiohttp servers, and long-running async services:
Advantages:
- Full iteration support via
steps()andevents() - Non-blocking (can handle thousands of concurrent flows)
- Perfect for async frameworks like FastAPI, aiohttp, asyncio
- Automatic resource cleanup via async context manager
Limitations:
- Requires
async/awaitsyntax - Must be called from an async context
FastAPI integration
Use AsyncNoukai in FastAPI endpoints for non-blocking flow execution:
For streaming responses, iterate with events():
Jupyter notebooks
Use sync client (Noukai) in Jupyter for simplicity:
For async in Jupyter, use the ipython event loop:
Converting between sync and async
If you have sync code but need async iteration, wrap it with asyncio.run():
For the reverse (async code in sync), use libraries like nest_asyncio (Jupyter) or asgiref (rare):
Resource management
Both clients are context managers and should be used with with or async with to ensure proper cleanup:
Or manage manually:
Concurrency limits
For high-concurrency scenarios (e.g., FastAPI handling 1000s of concurrent requests), use AsyncNoukai:
Using sync client in a thread pool is an alternative but less efficient:
Choose the right client
| Use case | Client | Why |
|---|---|---|
| Scripts, CLI, Jupyter | Noukai | Simple, no async overhead |
| FastAPI endpoints | AsyncNoukai | Non-blocking, scales |
| Streaming responses | AsyncNoukai | Required for events() iteration |
| High concurrency | AsyncNoukai | Handles 1000s of concurrent flows |
| Legacy code | Noukai | No refactoring needed |
Next steps
- Tool calls: See Tool calls
- Error handling: See Errors & retries