litellm/litellm/integrations/supabase.py
mateo-berri b604e2b20c refactor(lint): apply every safe ruff autofix and zero 28 strict-rule budgets
About 35,000 fixes ruff marks safe across 32 rules (UP006/UP045/UP007
modern annotations, UP032 f-strings, SIM114/SIM118, RET501, and
friends), removal of the 1,296 typing imports the rewrite orphaned, and
hand fixes for what the fixers could not see: five star-import
freeloaders of typing names, two F823 late-import annotations, the
/get/config/list introspection crash on types.UnionType, redundant
function-local RoleMappings imports in ui_sso.py that shadowed the
module-level name once the annotation lost its quotes, and one FURB168
tautology.

B009/B010/PIE804/RUF019 are excluded on purpose: their safe fixes
rewrite getattr/setattr/**-splat/key-in-dict escape hatches into forms
basedpyright then rejects (283 new errors measured), so their budgets
stay at base values.

ruff-strict-budget.json drops by 39,579 this commit (39,968 across the
branch) with 28 rules at an actual 0 and 9 more sharply down.
type-discipline-budget.json ratchets LIT002/LIT006/LIT009 down; LIT001
moves to the now-honest total: the checker matches the spelling `set`
but not the alias `Set`, so the 160 typing.Set annotations rewritten to
set[...] were always mutable-set annotations and only now count.
2026-08-01 15:43:29 -07:00

104 lines
4 KiB
Python

#### What this does ####
# On success + failure, log events to Supabase
import os
import subprocess
import sys
import traceback
import litellm
class Supabase:
# Class variables or attributes
supabase_table_name = "request_logs"
def __init__(self):
# Instance variables
self.supabase_url = os.getenv("SUPABASE_URL")
self.supabase_key = os.getenv("SUPABASE_KEY")
try:
import supabase
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "supabase"])
import supabase
if self.supabase_url is None or self.supabase_key is None:
raise ValueError(
"LiteLLM Error, trying to use Supabase but url or key not passed. Create a table and set `litellm.supabase_url=<your-url>` and `litellm.supabase_key=<your-key>`"
)
self.supabase_client = supabase.create_client( # type: ignore
self.supabase_url, self.supabase_key
)
def input_log_event(self, model, messages, end_user, litellm_call_id, print_verbose):
try:
print_verbose(f"Supabase Logging - Enters input logging function for model {model}")
supabase_data_obj = {
"model": model,
"messages": messages,
"end_user": end_user,
"status": "initiated",
"litellm_call_id": litellm_call_id,
}
data, count = self.supabase_client.table(self.supabase_table_name).insert(supabase_data_obj).execute()
print_verbose(f"data: {data}")
except Exception:
print_verbose(f"Supabase Logging Error - {traceback.format_exc()}")
def log_event(
self,
model,
messages,
end_user,
response_obj,
start_time,
end_time,
litellm_call_id,
print_verbose,
):
try:
print_verbose(f"Supabase Logging - Enters logging function for model {model}, response_obj: {response_obj}")
total_cost = litellm.completion_cost(completion_response=response_obj)
response_time = (end_time - start_time).total_seconds()
if "choices" in response_obj:
supabase_data_obj = {
"response_time": response_time,
"model": response_obj["model"],
"total_cost": total_cost,
"messages": messages,
"response": response_obj["choices"][0]["message"]["content"],
"end_user": end_user,
"litellm_call_id": litellm_call_id,
"status": "success",
}
print_verbose(f"Supabase Logging - final data object: {supabase_data_obj}")
data, count = (
self.supabase_client.table(self.supabase_table_name)
.upsert(supabase_data_obj, on_conflict="litellm_call_id")
.execute()
)
elif "error" in response_obj:
if "Unable to map your input to a model." in response_obj["error"]:
total_cost = 0
supabase_data_obj = {
"response_time": response_time,
"model": response_obj["model"],
"total_cost": total_cost,
"messages": messages,
"error": response_obj["error"],
"end_user": end_user,
"litellm_call_id": litellm_call_id,
"status": "failure",
}
print_verbose(f"Supabase Logging - final data object: {supabase_data_obj}")
data, count = (
self.supabase_client.table(self.supabase_table_name)
.upsert(supabase_data_obj, on_conflict="litellm_call_id")
.execute()
)
except Exception:
print_verbose(f"Supabase Logging Error - {traceback.format_exc()}")