Skip to content

Async vs Sync Usage in GraphBit Python API

GraphBit's Python bindings provide both synchronous (blocking) and asynchronous (non-blocking) interfaces for key operations, allowing you to choose the best fit for your application—whether it's a quick script, a web server, or a data pipeline.


Overview

  • Synchronous (Sync) functions block the current thread until the operation completes. They are simple to use in scripts and REPLs.
  • Asynchronous (Async) functions return immediately and run in the background, allowing your program to do other work or handle many tasks concurrently. Async is ideal for web servers, pipelines, and high-throughput applications.

Supported Sync and Async Functions

Module

Function Type
init Sync
version Sync
get_system_info Sync
health_check Sync
configure_runtime Sync
shutdown Sync

LLM Client

Function Type
complete Sync
get_stats Sync
complete_async Async
complete_stream Async
complete_batch Async
warmup Async

Embedding Client

Function Type
embed Sync
embed_many Sync
similarity Sync

Workflow

Function Type
add_node Sync
connect Sync
validate Sync

Workflow Executor

Function Type
configure Sync
get_stats Sync
reset_stats Sync
get_execution_mode Sync
execute Async
run_async Async

Workflow Result

Function Type
is_success Sync
is_failed Sync
state Sync
execution_time_ms Sync
get_variable Sync
get_all_variables Sync
variables Sync
get_node_output Sync
get_all_node_outputs Sync

Usage Examples

Synchronous Usage

import os

from graphbit import LlmConfig, LlmClient

config = LlmConfig.openai(os.getenv("OPENAI_API_KEY"))

client = LlmClient(config)
result = client.complete("Hello, world!")
print(result)

Asynchronous Usage

import os
import asyncio

from graphbit import LlmConfig, LlmClient

config = LlmConfig.openai(os.getenv("OPENAI_API_KEY"))

client = LlmClient(config)

async def main():
    result = await client.complete_async("Hello, async world!")
    print(result)

    # Streaming completion
    stream_result = await client.complete_stream("Stream this!")
    print(stream_result)

asyncio.run(main())

This documentation demonstrates sync and async functions supported by GraphBit’s Python API, enabling you to select the best approach for your use case, whether you need straightforward synchronous calls or high-performance asynchronous workflows.