Python SDK for sending AI chat turns and explicit support conversations to Letterbook
Project description
Letterbook Python SDK
Send AI chat turns or explicit support events to Letterbook so customer issues become conversations.
Turn-based detection
Each record_turn call captures one turn in the conversation. It does not create a ticket by itself. Letterbook queues the turns, waits for the conversation to go quiet, scans the transcript on the backend with an LLM, and creates a ticket only when it finds a relevant customer issue.
import letterbook
result = letterbook.record_turn(
user_id="user123",
event="user_message",
model="gpt_4",
input="The checkout page is broken and I was charged twice.",
output="I am sorry, I cannot fix billing issues from here.",
convo_id="conv789",
customer_email="customer@example.com",
debounce_seconds=300,
properties={
"system_prompt": "you are a helpful assistant",
"experiment": "experiment_a",
},
attachments=[
{
"type": "text",
"name": "Additional Info",
"value": "A very long document",
"role": "input",
}
],
)
print(result.queued, result.evaluation_after_seconds)
Set LETTERBOOK_API_KEY, or instantiate letterbook.Letterbook(api_key=...) for explicit configuration.
Set debounce_seconds to control how long Letterbook waits after the last message before evaluating the conversation.
If OpenTelemetry is configured in your app, the SDK automatically propagates the active trace context with each capture request. You can install letterbook-ai-chat[otel] if your app does not already include OpenTelemetry.
If customer_email is omitted, the SDK looks for properties["customer_email"], properties["email"], or an email-shaped user_id.
Email is only required if Letterbook promotes the capture to a ticket.
letterbook.capture_turn(...) and letterbook.capture(...) are deprecated compatibility aliases for letterbook.record_turn(...).
Push-based conversation creation
When your app already knows a tool call should create a support conversation, call create_conversation directly.
conversation = letterbook.create_conversation(
customer_email="customer@example.com",
customer_name="Customer",
subject="Checkout tool failed",
message="The checkout tool failed after charging the card twice.",
priority="high",
external_id="tool-call-123",
metadata={
"source": "ai_tool",
"session_id": "session_789",
},
)
print(conversation.id, conversation.url)
API-key REST helpers
The SDK also wraps the existing API-key endpoints:
letterbook.list_conversations(status="open", page_size=50)
letterbook.get_conversation("conv_123")
letterbook.add_conversation_note(
"conv_123",
author_email="agent@example.com",
content="Investigated from the external workflow.",
mentioned_user_emails=["teammate@example.com"],
)
letterbook.delete_user("user_123", confirm_email="agent@example.com")
Framework adapters
Framework adapters are optional and do not add hard dependencies to the base SDK. Install the framework package separately, then import adapters from letterbook.integrations.
Pass a stable convo_id such as your chat session, thread, or conversation id when using turn-based detection. Framework run/event ids are per model call and are not used as fallbacks because they split one transcript into separate evaluations.
LangChain:
from langchain_openai import ChatOpenAI
from letterbook.integrations import create_langchain_callback_handler
handler = create_langchain_callback_handler(
user_id="user123",
model="gpt_4",
convo_id="conv789",
customer_email="customer@example.com",
)
model = ChatOpenAI(callbacks=[handler])
LlamaIndex:
from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager
from letterbook.integrations import create_llamaindex_callback_handler
handler = create_llamaindex_callback_handler(
user_id="user123",
model="gpt_4",
convo_id="conv789",
customer_email="customer@example.com",
)
Settings.callback_manager = CallbackManager([handler])
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file letterbook_ai_chat-1.1.0.tar.gz.
File metadata
- Download URL: letterbook_ai_chat-1.1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07ada32f43735815f766175bba9b0b6ab3972a4db7a55e76169b87a03b935a3a
|
|
| MD5 |
b0dec591149bbd0f3972c31d97d0e5c9
|
|
| BLAKE2b-256 |
d08593c8c93984da3787c9404a904baf4cdd854c86d7b5b75ba2b5727e34c7d0
|
File details
Details for the file letterbook_ai_chat-1.1.0-py3-none-any.whl.
File metadata
- Download URL: letterbook_ai_chat-1.1.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a558a6322316819168547e0693b4424808e075d799cff4c65d3d276783e6ca98
|
|
| MD5 |
aa3f2e4ee7f247f8544e281a7069f30d
|
|
| BLAKE2b-256 |
fabd5ec953d3ea1a459075827bc8763c7144c2f54e97afd8d31044e9fe5d2c91
|