Chat History API¶
Persistent conversation storage.
langchain_cockroachdb.chat_message_histories.CockroachDBChatMessageHistory
¶
Bases: BaseChatMessageHistory
Chat message history stored in CockroachDB.
Pass a shared CockroachDBEngine to reuse one connection pool and one
retry policy across sessions. With only a connection string the history
opens and owns its own engine, which close() or aclose() releases.
Messages get ids generated here and are written with ON CONFLICT DO NOTHING, so retrying after an ambiguous result never duplicates them.
messages
property
¶
Get all messages for this session.
Note: BaseChatMessageHistory declares messages as a writable attribute, but we expose it as a read-only property (matching langchain-postgres).
__del__()
¶
Release an owned engine that was never closed, without blocking.
__init__(session_id, connection_string=None, engine=None, table_name='message_store', schema='public', *, retry_policy=None)
¶
Initialize chat message history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session_id
|
str
|
Unique session/thread identifier |
required |
connection_string
|
str | None
|
Database connection string, used when no engine is given |
None
|
engine
|
CockroachDBEngine | AsyncEngine | None
|
Shared CockroachDBEngine (recommended) or a SQLAlchemy AsyncEngine |
None
|
table_name
|
str
|
Table name for messages |
'message_store'
|
schema
|
str
|
Database schema |
'public'
|
retry_policy
|
RetryPolicy | None
|
How operations retry (default: the engine's policy) |
None
|
aadd_message(message)
async
¶
Add a message (async).
aadd_messages(messages)
async
¶
Add messages atomically, in order (async).
aclear()
async
¶
Clear messages (async).
aclose()
async
¶
Close the engine if this history created it.
acreate_table_if_not_exists()
async
¶
Create the message table and its session index if they do not exist.
add_message(message)
¶
Add a message to the session.
add_messages(messages)
¶
Add messages atomically, in order.
aget_messages()
async
¶
Get all messages for this session (async).
clear()
¶
Clear all messages for this session.
close()
¶
Close the engine if this history created it (sync).
create_table_if_not_exists()
¶
Create table (sync).
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
session_id |
str | Required | Session or thread identifier |
connection_string |
str | None | Database connection string (if engine not provided) |
engine |
CockroachDBEngine | None | Shared engine instance |
table_name |
str | "message_store" | Table name for messages |
retry_policy |
RetryPolicy | None | Retry policy (inherits from engine if not set) |
Key Methods¶
| Method | Description |
|---|---|
add_user_message() |
Add user message |
add_ai_message() |
Add AI response |
add_message() |
Add any message type |
add_messages() |
Add multiple messages atomically |
clear() |
Clear session history |
messages |
Get all messages |
acreate_table_if_not_exists() |
Create table if needed (async) |
create_table_if_not_exists() |
Create table if needed (sync) |
Examples¶
See Chat History Guide for comprehensive examples.