Skip to content

fix(proxy-extras): decode text settings and catalog values read on SQL_ASCII databases - #52287

Closed
sukhdeepjohar wants to merge 1 commit into
BerriAI:mainfrom
sukhdeepjohar:fix-52247-sql-ascii-settings
Closed

sukhdeepjohar wants to merge 1 commit into
BerriAI:mainfrom
sukhdeepjohar:fix-52247-sql-ascii-settings

Conversation

@sukhdeepjohar

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

[insert handwritten description here]

Details

TLDR

Problem this solves:

  • On a SQL_ASCII Postgres, the request-log index build crashes at startup
  • psycopg returns text as bytes there, so int("b'8192'") fails
  • Three request-log indexes are never built, so spend-log lookups stay unindexed

How it solves it:

  • One _text() helper decodes bytes before use in migration_data_checks.py
  • Covers block size, the restored session settings, column types and migration names
  • UTF8 databases get exactly the same values as before

User Flow

Before: an admin whose Postgres database was created with encoding SQL_ASCII never gets the request-log indexes

  1. They start the proxy with DATABASE_URL pointing at that database, and the migrations apply
  2. The log shows Exception in thread litellm-request-log-indexes ending in ValueError: invalid literal for int() with base 10: "b'8192'"
  3. The proxy serves traffic, but LiteLLM_SpendLogs_api_key_startTime_idx, LiteLLM_SpendLogs_litellm_call_id_idx and LiteLLM_DailyUserSpend_api_key_date_idx do not exist

After: the same database gets all three indexes at startup

  1. They start the proxy with DATABASE_URL pointing at that database, and the migrations apply
  2. The log shows no request-log index error
  3. All three indexes exist and are valid, the same as on a UTF8 database

Relevant issues

Fixes #52247

Affected release

Regression in v1.109.0, from #48493, which added the pre-index data checks that read SHOW block_size.

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

Both runs use the published ghcr.io/berriai/litellm:main-latest image (sha256:f38bf0888c091db9079396e17fc8f028d6b75cd26c3e8cb45c5fbdd1f3b66f87, litellm 1.111.0.dev4) started the way the image starts, against postgres:16-alpine. The litellm_proxy_extras package, where the bug lives, comes from source and goes first on PYTHONPATH, so the code that runs is the merge base or this PR's tip. No provider calls are involved, since the failure happens at startup before any request.

Shared setup

docker network create net52247
docker run -d --name pg --network net52247 -e POSTGRES_PASSWORD=pw -e POSTGRES_DB=litellm \
  -e POSTGRES_INITDB_ARGS="--encoding=SQL_ASCII --locale=C" postgres:16-alpine
git clone https://github.com/BerriAI/litellm.git src && git -C src checkout <hash>
docker run -d --name proxy --network net52247 -v "$PWD/config.yaml:/config.yaml:ro" \
  -v "$PWD/src/litellm/proxy-extras:/extras:ro" -e PYTHONPATH=/extras \
  -e DATABASE_URL=postgresql://postgres:pw@pg:5432/litellm \
  ghcr.io/berriai/litellm:main-latest --config /config.yaml --port 4000
# once /health/readiness answers
docker logs proxy 2>&1 | grep -A3 "Exception in thread litellm-request-log-indexes"
docker exec pg psql -U postgres -d litellm -Atc "select relname from pg_class where relname in \
  ('LiteLLM_SpendLogs_api_key_startTime_idx','LiteLLM_SpendLogs_litellm_call_id_idx','LiteLLM_DailyUserSpend_api_key_date_idx')"

config.yaml

model_list:
  - model_name: fake
    litellm_params:
      model: openai/fake
      api_base: http://127.0.0.1:9/v1
      api_key: sk-fake-not-secret
general_settings:
  master_key: sk-52247-master-not-secret

Env vars set: DATABASE_URL, PYTHONPATH, POSTGRES_PASSWORD, POSTGRES_DB, POSTGRES_INITDB_ARGS.

Before (0cc072c)

  1. SHOW server_encoding on the database returns SQL_ASCII
  2. The proxy starts and /key/generate works, but the log shows
Exception in thread litellm-request-log-indexes:
  File "/extras/litellm_proxy_extras/migration_data_checks.py", line 348, in find_blockers
ValueError: invalid literal for int() with base 10: "b'8192'"
  1. The index query returns no rows. LiteLLM_SpendLogs_api_key_startTime_idx, LiteLLM_SpendLogs_litellm_call_id_idx and LiteLLM_DailyUserSpend_api_key_date_idx are all missing

After (c152707)

  1. SHOW server_encoding on the database returns SQL_ASCII
  2. The proxy starts and /key/generate works, and the log has no request-log index exception
  3. The index query returns all three names, each with indisvalid = true

The same After run on a UTF8 database (POSTGRES_INITDB_ARGS="--encoding=UTF8") also builds all three, as it did before this PR.

Tests, at the tip, in a clean Python 3.13 container with psycopg[binary] and pytest

tests/unit/litellm_proxy_extras/test_migration_data_checks.py
tests/unit/litellm_proxy_extras/test_request_log_indexes.py
tests/unit/litellm_proxy_extras/test_migration_dry_run.py

On the merge base with only the new test, test_a_sql_ascii_database_gets_the_same_findings_and_settings_back_as_a_utf8_one[sql-ascii-database] fails with ValueError: invalid literal for int() with base 10: "b'8192'" (1 failed, 49 passed). With the fix, 50 passed. ruff check, ruff format --check, ruff_strict_gate.py, type_discipline_gate.py and test_quality_gate.py pass at the tip.

Type

🐛 Bug Fix
✅ Test

Caveats (if any)

Low

  • Other litellm_proxy_extras reads may still see bytes on SQL_ASCII
    • request_log_indexes.py child partition names, partitioned tables only
    • utils.py failed-migration logs, which then print empty
  • _text() replaces undecodable bytes instead of raising
    • settings, type names and migration names are ASCII, so no value changes today
  • openapi_docs_gate.py and type_check_gate.py were left to CI here

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

…L_ASCII databases

On a SQL_ASCII database psycopg returns text values as bytes, so the request-log index
build read SHOW block_size as "b'8192'" and crashed before building its indexes. Decode
every text value find_blockers reads through one helper.
@github-actions

Copy link
Copy Markdown
Contributor

#52247 was fixed by #53965 on main, so this pull request is closed. Reopen it if something was missed.

@github-actions github-actions Bot closed this Nov 24, 2026
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

1 participant