Skip to main content

Usage

from langchat.providers import Supabase

db = Supabase()                                              # reads from env
db = Supabase(url="https://xxxx.supabase.co", key="eyJ...")  # explicit

Parameters

url
str | None
default:"None"
Supabase project URL. Falls back to SUPABASE_URL.
key
str | None
default:"None"
Supabase API key. Falls back to SUPABASE_KEY or SUPABASE_SERVICE_ROLE_KEY.

Environment variables

SUPABASE_URL=https://xxxx.supabase.co
SUPABASE_KEY=eyJhbGc...
# or
SUPABASE_SERVICE_ROLE_KEY=eyJhbGc...   # for server-side use (bypasses RLS)
Use SUPABASE_SERVICE_ROLE_KEY for server deployments where you need to bypass Row Level Security. Use SUPABASE_KEY (anon key) for client-side or restricted access.

Tables created automatically

LangChat creates these tables on first run:

chat_history

ColumnTypeDescription
idbigintPrimary key (auto-increment)
user_idtextUser identifier
platformtextPlatform namespace
messagetextUser message
responsetextAI response
created_attimestamptzUTC timestamp

request_metrics

ColumnTypeDescription
idbigintPrimary key (auto-increment)
user_idtextUser identifier
request_timetextISO 8601 UTC timestamp
response_timefloatLatency in seconds
successbooleanWhether the request succeeded
error_message`textnull`Error details on failure

Querying your data

You can use the Supabase dashboard or the Python client directly to query these tables:
from supabase import create_client

client = create_client(url, key)

# Get all conversations for a user
history = (
    client
    .table("chat_history")
    .select("*")
    .eq("user_id", "alice")
    .order("created_at", desc=True)
    .execute()
)

# Get average response time
metrics = (
    client
    .table("request_metrics")
    .select("response_time")
    .eq("success", True)
    .execute()
)

Row Level Security (RLS)

If you enable RLS on Supabase tables, you must either:
  1. Use the service_role key (bypasses RLS entirely)
  2. Add appropriate RLS policies for the langchat operations
For most server-side deployments, using SUPABASE_SERVICE_ROLE_KEY is the simplest approach.