Skip to content

fix(proxy-extras): tolerate unloadable psycopg - #50527

Closed
chamanswami555 wants to merge 4 commits into
BerriAI:mainfrom
chamanswami555:fix-psycopg-import-guard
Closed

chamanswami555 wants to merge 4 commits into
BerriAI:mainfrom
chamanswami555:fix-psycopg-import-guard

Conversation

@chamanswami555

@chamanswami555 chamanswami555 commented Nov 7, 2026 •

Copy link
Copy Markdown

<make-me-human-readable>
the migration job died on nodes where the postgres driver cannot load, for example fips nodes that run the pure python driver on an image without libpq. with the default v2 resolver it died taking the migration lock, with the legacy resolver it died on the request-log index build. the lock now falls back to prisma's own advisory lock and the index build logs the skip, so the job finishes either way, the way the invalid index check already does when the driver is missing
</make-me-human-readable>

Details

TLDR

problem this solves:

  • the migration job crashed when psycopg could not load libpq
  • the v2 resolver's deploy lock imported psycopg unguarded, so the default path died before any migration ran
  • the request-log index build imported psycopg unguarded too, despite "never raises"
  • the sibling invalid index check already guards the same import

how it solves it:

  • one probe, psycopg_import_error, answers whether the driver loads and why not
  • without the driver prisma migrate deploy runs under prisma's own advisory lock, with the cause logged
  • recovery and baselining, which need the coordinator connection, report the cause as a clean error
  • the index build guards the import and logs the cause, the job treats it as a skip and exits 0
  • lock, function, manager and job tests fail the import like libpq does

User Flow

before: on a fips node the migration job dies on the first import psycopg it reaches

  1. they start the migration job, python /app/litellm/proxy/prisma_migration.py, with PSYCOPG_IMPL=python set because the bundled libcrypto aborts on fips
  2. with the default v2 resolver the job dies in prisma_migration_lock before any migration runs, with the legacy resolver the migrations apply and the job dies on the index build
  3. either way the log ends with ImportError: couldn't import requested psycopg 'python' implementation: libpq library not found, the container exits non-zero and the rollout stalls

after: the same job finishes and reports what it skipped

  1. they start the migration job the same way
  2. the log shows psycopg could not be imported (... libpq library not found); running prisma migrate deploy under Prisma's own advisory lock and the migrations apply
  3. the log shows psycopg could not be imported (... libpq library not found); skipping the request-log index build until a run where it loads, then Setup complete, and the job exits 0

Relevant issues

fixes #49680

related: #50902 adds libpq to the shipped images, which makes psycopg load there. this one keeps the job alive on any image where it does not, and names the cause. #50511 (closed) guarded only the index build and returned false, so the job still exited 1 unless its env var was set

Affected release

regression in v1.105.0 for the index build (arrived in #43948, first tagged v1.105.0-rc.1) and in v1.107.0 for the deploy lock (arrived in #46368, first tagged v1.107.0-dev.2)

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 on a throwaway postgres 17 listening on 127.0.0.1:5499 with a fresh database per run. psycopg 3 is installed, as in the image, but libpq is kept off the library path (no pg_config on PATH) and PSYCOPG_IMPL=python is set, so import psycopg raises ImportError: couldn't import requested psycopg 'python' implementation: libpq library not found, the same error as the issue. the job is the one the image runs, python litellm/proxy/prisma_migration.py, and echo $? prints its exit code. full logs are 700 lines of migrations each, only the relevant lines are shown. before is the merge base, after is the branch tip, each resolver once. the branch was brought up to date with main on 12 nov, after the package move (#51000), so the paths below are the new ones

Before (52d1c4c), default v2 resolver

  1. DATABASE_URL=postgresql://litellm@127.0.0.1:5499/litellm_e2e_v2_before_3 PSYCOPG_IMPL=python python litellm/proxy/prisma_migration.py; echo "exit code: $?"
  2. output, the job dies taking the deploy lock, no migration ran
2026-11-12 20:31:37,379 - litellm_proxy_extras - INFO - Using v2 migration resolver (--use_v2_migration_resolver)
Traceback (most recent call last):
  File "litellm/proxy/prisma_migration.py", line 40, in <module>
    sys.exit(main())
  ...
  File "litellm/proxy-extras/litellm_proxy_extras/utils.py", line 1007, in _setup_database_v2
  File "litellm/proxy-extras/litellm_proxy_extras/utils.py", line 1081, in _run_database_v2
  File "litellm/proxy-extras/litellm_proxy_extras/migration_lock.py", line 160, in prisma_migration_lock
  File "litellm/proxy-extras/litellm_proxy_extras/migration_lock.py", line 126, in migration_lock
    import psycopg
  ...
ImportError: couldn't import requested psycopg 'python' implementation: libpq library not found
before exit code: 1

After (6a4c9c4), default v2 resolver

  1. DATABASE_URL=postgresql://litellm@127.0.0.1:5499/litellm_e2e_v2_after_3 PSYCOPG_IMPL=python python litellm/proxy/prisma_migration.py; echo "exit code: $?"
  2. output, head and tail
2026-11-12 20:31:47,449 - litellm_proxy_extras - INFO - Using v2 migration resolver (--use_v2_migration_resolver)
2026-11-12 20:31:47,465 - litellm_proxy_extras - WARNING - psycopg could not be imported (couldn't import requested psycopg 'python' implementation: libpq library not found); running prisma migrate deploy under Prisma's own advisory lock instead of the pinned coordinator connection. The litellm[extra_proxy] extra provides psycopg, and PSYCOPG_IMPL=python also needs a system libpq.
2026-11-12 20:31:49,370 - litellm_proxy_extras - INFO - prisma migrate deploy stdout: Prisma schema loaded from schema.prisma
Datasource "client": PostgreSQL database "litellm_e2e_v2_after_3", schema "public" at "127.0.0.1:5499"
...
All migrations have been successfully applied.

2026-11-12 20:31:49,371 - litellm_proxy_extras - WARNING - psycopg is not installed; skipping the invalid index check. Install the litellm[extra_proxy] extra, which includes psycopg.
2026-11-12 20:31:49,371 - litellm_proxy_extras - WARNING - psycopg could not be imported (couldn't import requested psycopg 'python' implementation: libpq library not found); skipping the request-log index build until a run where it loads. The litellm[extra_proxy] extra provides psycopg, and PSYCOPG_IMPL=python also needs a system libpq.
LiteLLM: Setup complete. Skipping server startup as requested.
after exit code: 0

Before (52d1c4c), legacy resolver

  1. DATABASE_URL=postgresql://litellm@127.0.0.1:5499/litellm_e2e_before_6 PSYCOPG_IMPL=python USE_V2_MIGRATION_RESOLVER=false python litellm/proxy/prisma_migration.py; echo "exit code: $?"
  2. output, last lines after all migrations applied
  File ".venv/lib/python3.12/site-packages/psycopg/pq/__init__.py", line 114, in <module>
    import_from_libpq()
  File ".venv/lib/python3.12/site-packages/psycopg/pq/__init__.py", line 92, in import_from_libpq
    handle_error("python", e)
  File ".venv/lib/python3.12/site-packages/psycopg/pq/__init__.py", line 71, in handle_error
    raise ImportError(msg) from e
ImportError: couldn't import requested psycopg 'python' implementation: libpq library not found
before exit code: 1

After (6a4c9c4), legacy resolver

  1. DATABASE_URL=postgresql://litellm@127.0.0.1:5499/litellm_e2e_after_6 PSYCOPG_IMPL=python USE_V2_MIGRATION_RESOLVER=false python litellm/proxy/prisma_migration.py; echo "exit code: $?"
  2. output, last lines after all migrations applied
2026-11-12 20:32:38,255 - litellm_proxy_extras - INFO - Drift script is empty after filtering; nothing to apply
2026-11-12 20:32:38,255 - litellm_proxy_extras - INFO - ✅ Post-migration sanity check completed
2026-11-12 20:32:38,256 - litellm_proxy_extras - WARNING - psycopg is not installed; skipping the invalid index check. Install the litellm[extra_proxy] extra, which includes psycopg.
2026-11-12 20:32:38,256 - litellm_proxy_extras - WARNING - psycopg could not be imported (couldn't import requested psycopg 'python' implementation: libpq library not found); skipping the request-log index build until a run where it loads. The litellm[extra_proxy] extra provides psycopg, and PSYCOPG_IMPL=python also needs a system libpq.
LiteLLM: Setup complete. Skipping server startup as requested.
after exit code: 0

Type

🐛 Bug Fix

Caveats (if any)

Low

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

@chamanswami555

Copy link
Copy Markdown
Author

hi, the workflows on this pr are waiting for a maintainer's approval (first contribution), so unit, lint and the title check have not run yet. could someone approve the runs when convenient? the cla is signed, and the fix has a job-level test plus a live before and after run in the description. thanks.

@chamanswami555

Copy link
Copy Markdown
Author

@mateo-berri @yucheng-berri sorry for the ping: this fixes the migration job crash on fips nodes from #49680 (psycopg cannot load libpq with PSYCOPG_IMPL=python). the workflows are waiting for approval, could one of you take a look when you have a minute? thanks.

@veria-ai

veria-ai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Reviews

@chamanswami555

Copy link
Copy Markdown
Author

@ishaan-jaff @yassin-berriai could someone approve the workflow runs here? fips migration job crash from #49680, merges cleanly, veria found no issues

@github-actions

Copy link
Copy Markdown
Contributor

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

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.

[Bug]: prisma_migration --skip_server_startup crashes with ImportError when psycopg cannot load libpq (unguarded import in ensure_request_log_indexes)

1 participant