Skip to content

fix(proxy): start on Postgres databases with SQL_ASCII encoding - #53965

Merged
kenneth-berri merged 2 commits into
mainfrom
litellm_sql_ascii_startup_decode
Nov 24, 2026
Merged

kenneth-berri merged 2 commits into
mainfrom
litellm_sql_ascii_startup_decode

Conversation

@kenneth-berri

@kenneth-berri kenneth-berri commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Handwritten 1-3 sentence description

The proxy now starts and restarts on a Postgres database whose encoding is SQL_ASCII, and builds its request-log indexes there. Before, the first boot logged an error and skipped the indexes, and every later boot crashed.

Details

Link to Devin session: https://app.devin.ai/sessions/83d42fd3dca2490c941686a5f537a783
Open in Devin Desktop: https://app.devin.ai/desktop/session/83d42fd3dca2490c941686a5f537a783?variant=devin
Requested by: @kenneth-berri

TLDR

Problem this solves:

  • SQL_ASCII databases return text as bytes to psycopg
  • First boot fails the request-log index build
  • Every later boot exits while reading migration names

How it solves it:

  • Startup database connections ask Postgres for UTF-8 text
  • Settings, catalog values and migration names arrive as strings

User Flow

Before: an operator whose Postgres database was created with SQL_ASCII encoding can start the proxy once and never again

  1. They start the proxy against the SQL_ASCII database and it serves, but the log shows ValueError: invalid literal for int() with base 10: "b'8192'"
  2. The request-log indexes are never built
  3. They restart the proxy and it exits with TypeError: cannot use a string pattern on a bytes-like object

After: the same operator starts and restarts the proxy with no error

  1. They start the proxy against the SQL_ASCII database and the log shows each request-log index being built
  2. They restart the proxy and it serves, and GET http://localhost:4247/health/liveliness returns 200

Relevant issues

Fixes #52247

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Shared setup, run the same way for both legs against a local Postgres 16. Each leg uses a fresh database and boots the proxy twice. The proxy ran with one worker and without Redis or --reload, because the bug is in startup migrations and index builds and does not involve shared worker state. DATABASE_URL and ANTHROPIC_API_KEY are the only env vars.

psql "$BASE/postgres" -c "CREATE DATABASE litellm_ascii ENCODING 'SQL_ASCII' LC_COLLATE 'C' LC_CTYPE 'C' TEMPLATE template0"
DATABASE_URL="$BASE/litellm_ascii" python litellm/proxy/proxy_cli.py --config config.yaml --port 4247 --num_workers 1
curl -s -o /dev/null -w "liveliness HTTP %{http_code}\n" localhost:4247/health/liveliness
model_list:
  - model_name: anthropic-haiku-4-5
    litellm_params:
      model: anthropic/claude-haiku-4-5
      api_key: os.environ/ANTHROPIC_API_KEY
general_settings:
  master_key: sk-repro-52247-master-key

Before (6088120)

  1. First boot serves but the index build fails
liveliness HTTP 200
ValueError: invalid literal for int() with base 10: "b'8192'"
  1. Second boot exits with code 1
liveliness HTTP 000
TypeError: cannot use a string pattern on a bytes-like object

After (ccc316f)

  1. First boot serves and builds the indexes
liveliness HTTP 200
Building index LiteLLM_SpendLogs_api_key_startTime_idx on LiteLLM_SpendLogs concurrently
Building index LiteLLM_SpendLogs_litellm_call_id_idx on LiteLLM_SpendLogs concurrently
Building index LiteLLM_DailyUserSpend_api_key_date_idx on LiteLLM_DailyUserSpend concurrently
Building index LiteLLM_EndUserTable_restricted_idx on LiteLLM_EndUserTable concurrently
  1. Second boot serves with no error, and the indexes are valid
liveliness HTTP 200
"LiteLLM_SpendLogs_api_key_startTime_idx"|t
"LiteLLM_DailyUserSpend_api_key_date_idx"|t
"LiteLLM_SpendLogs_litellm_call_id_idx"|t
"LiteLLM_EndUserTable_restricted_idx"|t

The new integration test test_a_sql_ascii_database_migrates_on_every_boot_and_gets_its_request_log_indexes fails on 6088120 and passes on ccc316f for both migration resolvers.

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Assumed migration and catalog names in SQL_ASCII databases are valid UTF-8
  • A non-UTF-8 name there would raise a decode error

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Note

Low Risk
Narrow change to how psycopg negotiates client encoding on existing startup paths; risk is limited to non-UTF-8 identifiers in SQL_ASCII databases.

Overview
Fixes proxy startup when the Postgres database uses SQL_ASCII encoding by passing client_encoding="utf8" on every proxy-extras psycopg.connect used for migrations, migration locking, request-log index builds, and related catalog/ledger queries. That forces migration names, settings, and catalog values to arrive as normal strings instead of byte literals that previously broke index builds on first boot and crashed restarts.

The injectable LensCheckConnect protocol and unit-test psycopg.connect mocks are updated to accept client_encoding. An integration test provisions a SQL_ASCII database and asserts migrations and request-log indexes succeed across repeated boots on both resolvers.

Reviewed by Cursor Bugbot for commit 3faa5e1. Bugbot is set up for automated code reviews on this repo. Configure here.

@kenneth-berri
kenneth-berri requested a review from a team November 23, 2026 23:49
@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@veria-ai

veria-ai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge, with the previous finding fixed and no new issues found.

Summary

Startup database connections now request UTF-8 text so SQL_ASCII databases return strings for migration names and index metadata.

  • Proxy startup reads database text from SQL_ASCII databases as strings.

Reviews (2) · Last reviewed commit: "test: wrap fake connect signatures to th..." · Reviewed by Greptile

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 3faa5e1. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proxy start crashes with ValueError in find_blockers when the database encoding is SQL_ASCII

2 participants