Skip to main content

Python client for Assistant UI sync server API

Project description

Assistant UI Sync Server API

A Python client library for interacting with Assistant UI sync server backends, providing the same API structure as the JavaScript/TypeScript useChatRuntime.

Installation

pip install assistant-ui-sync-server-api

Or using uv:

uv add assistant-ui-sync-server-api

Usage

Basic Example

import asyncio
from assistant_ui import AssistantClient

# Create a client
client = AssistantClient(base_url="https://api.example.com")

# Create messages
messages = [
    {
        "role": "user",
        "content": [{"type": "text", "text": "Hello, how are you?"}]
    }
]

# Send a chat request to a specific thread
async def main():
    thread = client.threads("thread-123")
    response = await thread.chat(
        messages=messages,
        system="You are a helpful assistant."
    )
    print(response.json())
    await client.close()

asyncio.run(main())

Using Context Managers

# Async context manager
async with AssistantClient(base_url="https://api.example.com") as client:
    thread = client.threads("thread-123")
    response = await thread.chat(messages=messages)

# Sync context manager
with AssistantClient(base_url="https://api.example.com") as client:
    thread = client.threads("thread-123")
    response = thread.chat_sync(messages=messages)

Authentication

# Static headers
client = AssistantClient(
    base_url="https://api.example.com",
    headers={"Authorization": "Bearer your-token"}
)

# Dynamic headers (async)
async def get_auth_headers():
    token = await fetch_token()
    return {"Authorization": f"Bearer {token}"}

client = AssistantClient(
    base_url="https://api.example.com",
    headers=get_auth_headers
)

Using Tools

tools = {
    "get_weather": {
        "description": "Get the current weather for a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"},
                "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
            },
            "required": ["location"]
        }
    }
}

thread = client.threads("thread-123")
response = await thread.chat(
    messages=messages,
    tools=tools
)

Canceling Operations

thread = client.threads("thread-123")

# Start a long-running chat
chat_task = asyncio.create_task(thread.chat(messages=messages))

# Cancel it
await thread.cancel()
chat_task.cancel()

Message Types

The package supports various message types matching the Assistant UI format:

from assistant_ui.types import Message

# Text message
text_message: Message = {
    "role": "user",
    "content": [{"type": "text", "text": "Hello!"}]
}

# Image message
image_message: Message = {
    "role": "user",
    "content": [
        {"type": "text", "text": "What's in this image?"},
        {"type": "image", "image": "https://example.com/image.jpg"}
    ]
}

# File message
file_message: Message = {
    "role": "user",
    "content": [
        {"type": "file", "data": "https://example.com/file.pdf", "mimeType": "application/pdf"}
    ]
}

# Assistant message with tool calls
assistant_message: Message = {
    "role": "assistant",
    "content": [
        {"type": "text", "text": "I'll help you with that."},
        {
            "type": "tool-call",
            "toolCallId": "call-123",
            "toolName": "get_weather",
            "args": {"location": "San Francisco"}
        }
    ]
}

# Tool result message
tool_message: Message = {
    "role": "tool",
    "content": [
        {
            "type": "tool-result",
            "toolCallId": "call-123",
            "toolName": "get_weather",
            "result": {"temperature": 72, "condition": "sunny"},
            "isError": False
        }
    ]
}

API Reference

AssistantClient

Main client for interacting with Assistant UI backends.

Constructor:

AssistantClient(
    base_url: str,
    headers: Optional[Dict[str, str] | Callable] = None,
    timeout: Optional[float] = None,
    **kwargs
)

Methods:

  • threads(thread_id: str) -> ThreadClient: Get a ThreadClient for a specific thread
  • close(): Close the async client
  • close_sync(): Close the sync client

ThreadClient

Client for interacting with a specific thread.

Methods:

  • chat(messages, system=None, tools=None, **kwargs): Send an async chat request
  • chat_sync(messages, system=None, tools=None, **kwargs): Send a sync chat request
  • cancel(): Cancel the current async operation
  • cancel_sync(): Cancel the current sync operation

Type Definitions

The package includes TypedDict definitions for all message types and configuration options, providing full type hints for better IDE support and type checking.

Development

This package is part of the Assistant UI monorepo. To contribute:

  1. Clone the main repository
  2. Navigate to python/assistant-ui
  3. Install dependencies with uv sync
  4. Run tests with uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

assistant_ui_sync_server_api-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

assistant_ui_sync_server_api-0.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file assistant_ui_sync_server_api-0.1.0.tar.gz.

File metadata

File hashes

Hashes for assistant_ui_sync_server_api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9cbb56112e783eb1d6f2d9387efb0bfbc49c2fc6b3c49eaca925a8da86d6d3c8
MD5 f511ada628e77500acf3a96b223a2b79
BLAKE2b-256 6db6a191cd2cc3ea0c8adb86f71ff2379f1c72fa4457143da6bc5a25d64e2757

See more details on using hashes here.

File details

Details for the file assistant_ui_sync_server_api-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for assistant_ui_sync_server_api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 343eb0dc7722222cb109236815ad8800600e291703dedd799d3dfcdac9a51b1d
MD5 75a02b2f53765c53178ec93d6b03d22b
BLAKE2b-256 c9cf4c6fbd955c60ccf75551b1068ee0183f15b2ae0ff622374069d8946eff6a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page