litellm/tests/unit/integrations/gitlab/test_gitlab_prompt_manager.py
yuneng-jiang 7248fd8328
chore: backport CI fixes, #43206 and Rust changes to rc/1.104.0 (#43338)
* feat(model-catalog): add Rust registry validation (#43136)

* doc

* feat(model-catalog): add Rust registry validation

* test(model-catalog): ignore integration tests

* docs(model-catalog): fix validation note grammar

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
(cherry picked from commit c289d5d6fb)

* test(rust): reorganize core crate tests and split cache and OCR suites (#43177)

* test(rust): group cache tests under cache/ and fold test_ocr.py into ocr/

The two failure cases in test_ocr.py duplicated the upstream-500 and timeout rows
of PUBLIC_FAILURES, so only the file-input encoding case moves to ocr/test_requests.py

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(rust): split the response cache suite into one file per backend

test_response_cache.py grew to 2400 lines. Each backend now has its own file, shared
fixtures live in cache/conftest.py and shared helpers in support/cache.py. The helpers
alias the private native test handles once, dropping the per-call reportPrivateUsage hits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* split tokenizer test

* test(core): consolidate route integration tests under tests/ with rstest and wiremock

Moves the public-API OCR route tests out of src/ocr/route.rs and document.rs into
tests/ocr/, split per provider plus lifecycle, machine, and document tests, merging
the duplicated pairs. Messages, audio transcription, and chat completions share one
wiremock-based upstream and recording secret source in tests/support, and gain
table-driven cases for auth, routing, upstream errors, streaming, and declines.
Tests of litellm-llms items move to that crate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(messages): keep the stream relay test independent of the stream head contents

The stream head carries no headers on main, so the relay test asserts the
open-then-deliver order and the relayed body instead of header hand-off.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 10413796c6)

* feat(rust): hand upstream response headers to the native Messages stream (#43178)

* ci: drop the ocr_testing job now that tests/ocr_tests is gone

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(ocr): restore the live OCR matrix and the ocr_testing job

The public litellm.ocr / aocr / Router interface is unchanged by the Rust
migration, so the live provider matrix still applies. Drops the stale VCR skip
list for the deleted test_rust_bridge.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(messages): show streamed upstream headers never reach the native stream

The Python handler puts the upstream response headers on the stream's
_hidden_params before the first chunk so the proxy can forward them as
llm_provider-* headers. The native route drops them, and this test fails
on the Rust path while passing on Python.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* feat(messages): hand upstream response headers to the native stream before its first chunk

The Messages route fills MessagesStreamHead from the upstream response and
yields it on Open. The Python driver converts it through the protocol host
and hands it to Stream and SyncStream as their _hidden_params, so a
streamed native call carries additional_headers the same way the Python
handler does and the proxy can forward them as llm_provider-* headers.

The relay contract lives in the core crate test, the hand-off in the
host-python driver test, and the header projection in the route host test,
so the recording-server test that showed the gap is dropped.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* wip

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 081f73f021)

* feat(testkit): agent clients for Claude Code, Codex and opencode (#43181)

* feat(testkit): install and configure Claude Code, Codex and opencode against a gateway

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* refactor(testkit): derive targets from target-lexicon and split agents behind a trait

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* refactor(testkit): group sources into agent and install folders

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* feat(testkit): split agents into install, configure and drive with semver-aware launch

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* style(testkit): drop a needless borrow

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
(cherry picked from commit 9c10e0f985)

* ci: run migrated unit selections on every event in legacy GHA shards (#43182)

* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 694783ebbe)

* test: move tests/test_litellm root and small trees into tests/unit (#43186)

* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit f6882246d4)

* refactor(framer): replace Framer trait with tokio-util codecs (#43193)

* refactor(framer): replace Framer trait with tokio-util codecs

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor(framer): port SSE and AWS event stream framing to codecs

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(rust): drop clone on Copy capabilities in messages request test

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(rust): use field init shorthand in messages request test

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 3c93ea1697)

* test: move tests/test_litellm/llms into tests/unit/llms (#43191)

* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: keep the job's UNIT_FLAG out of the shard-script tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 5e6dc89ba1)

* test: move tests/test_litellm integrations and secret_managers into tests/unit (#43194)

* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: move tests/test_litellm integrations and secret_managers into tests/unit

Rename-only. Mirrors the old paths, including the directory conftests
and the prompt and JSON fixtures. Follow-up commits prune and wire them.

* test: prune and repoint the moved integrations tests

Deletes the 7 audited tests a stronger test in the same tree already
covers, imports the TLS sink helpers from their new conftest path, and
restores os.environ after each integrations test. Some presets write
OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the
legacy tree's test ordering that header leaked into the AgentOps tests.

* ci: run the moved integrations tests under their legacy flag

The integrations GHA shard and a new CircleCI job run the integrations
unit selection. secret_managers joins the misc selection.

* docs: point integrations and secret_managers references at tests/unit

* test: make the moved integrations directories packages

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: keep the job's UNIT_FLAG out of the shard-script tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit cf491d1df9)

* fix(tests): drop the repeated UNIT_FLAG key in test_unit_shard_missing_paths (#43212)

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
(cherry picked from commit 25de1ab2b0)

* fix(jwt): let x-litellm-team-id select DB membership teams when the token also carries a team claim (#43206)

* fix(jwt): let x-litellm-team-id select DB membership teams when the token also carries a team claim

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* docs(jwt): describe header team selection under fallback_to_db_teams

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: yassin <yassin@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 7b4fd47c6e)

* test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199)

* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: move tests/test_litellm integrations and secret_managers into tests/unit

Rename-only. Mirrors the old paths, including the directory conftests
and the prompt and JSON fixtures. Follow-up commits prune and wire them.

* test: prune and repoint the moved integrations tests

Deletes the 7 audited tests a stronger test in the same tree already
covers, imports the TLS sink helpers from their new conftest path, and
restores os.environ after each integrations test. Some presets write
OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the
legacy tree's test ordering that header leaked into the AgentOps tests.

* ci: run the moved integrations tests under their legacy flag

The integrations GHA shard and a new CircleCI job run the integrations
unit selection. secret_managers joins the misc selection.

* docs: point integrations and secret_managers references at tests/unit

* test: make the moved integrations directories packages

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit

Rename-only. Mirrors the old paths, including fixtures, the stubtest config
and the native-route wheel script. Two files that collide with existing unit
files are merged in a follow-up commit.

* test: merge, prune and repoint the moved core, routing, responses, caching and rust_bridge tests

Merges the two files that collided with existing unit files, folding the
legacy extra case into test_is_chat_completion_cached_dict, and deletes the
9 audited tests a stronger test in the same file already covers.

Keeps what needs the network in tests/test_litellm: test_tokenizers pulls a
tokenizer from the Hugging Face hub, and the gpt2 and r50k_base tokenizer
cases download their BPE files. The unit core_utils conftest points
TIKTOKEN_CACHE_DIR at litellm's bundled encodings so the rest never depend on
import order to stay offline, and FakeSecretVault moves to a shared module
so both trees can build it.

* ci: run the moved core, routing, responses, caching and rust_bridge tests under their flags

core_utils gets a core-utils flag and CircleCI job, and its GHA shard keeps
the legacy path for the retained network tests. router_utils and
router_strategy join enterprise-routing, responses joins
responses-caching-types (minus responses/mcp, which mcp-integration owns),
caching joins caching-local and rust_bridge joins misc. The redis-compat,
test-rust, stubtest and merge-smoke paths follow the move.

* docs: point the Rust crate references at tests/unit

* test: make the moved core, routing and rust_bridge directories packages

* test: keep the no-loop DualCache batch_get_cache regression test

It runs the sync path outside any event loop, which the inside-loop test
cannot, so a change that picks the Redis client by loop state would only
show up there.

* test: keep the job's UNIT_FLAG out of the shard-script tests

* fix(url_utils): block 192.0.0.0/24 on every Python patch release

* test: move the new budget limiter tests into tests/unit/router_strategy

* test: move the new sentry scrubbing tests into tests/unit/litellm_core_utils

* test: move the new zerobus tests into tests/unit/integrations

* test: make tests/unit/integrations/zerobus a package

* test: load litellm's own tiktoken cache setup once instead of resetting it per test

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit a11a93f44a)

* refactor(rust): move credential inheritance and the SDK limits out of the legacy callback crate into a driver preflight (#43259)

The legacy callback crate carried two rewrites that have nothing to do with the
Logging contract: litellm_credential_name inheritance and the max_budget and
num_retries_per_request checks. Any later callback host would need them
unchanged, which is the smell the crate's AGENTS.md now names. They are now a
Preflight the driver in litellm-host-python runs on the keyword view begin
returned, before the host projects from it, supplied by python-bridge and passed
through run_legacy_call. The call order is unchanged (setup, deployment hook,
credentials, limits) and a rejection still fails the call as a host failure, so
the failure callbacks run as before. The preflight rewrites the adapter's own
copy in place, so no extra dict copy and no new lifecycle method

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit e9491d31b5)

* refactor(http): hand out an owned Client and route all providers through the pool (#43245)

* refactor(messages): take the provider client from the injected HTTP pool

The messages route kept its own process-wide reqwest client, so it ignored
ssl_verify, CA bundles, client certs, proxies and every other setting that
litellm-http resolves. The machine now takes the HttpClientPool and the
call's HttpClientConfig, as OCR does, and the bridge passes its shared pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(http): hand out an owned Client and move chat, audio and OIDC onto the pool

HttpClientPool now returns litellm_http::Client, a newtype only crates/http
can build, so every provider client carries the resolved TLS, proxy and
timeout settings. Chat completions and audio transcription drop their
process-wide reqwest clients and take the pool and call config like
messages; their 600s ceiling moves to the request. OidcResolver takes its
client instead of building one, and the bridge hands it the pooled one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(secrets): build Google, Azure and CyberArk manager clients from the pool

The native secret managers built bare reqwest clients, so they ignored the
host's TLS and proxy settings. load_native_manager now takes the pool and
the host config and hands each manager a pooled client.

CyberArk's CYBERARK_SSL_VERIFY and CYBERARK_CLIENT_CERT/KEY become an
override on the host config instead of a hand-built client. To express a
certificate and key in separate files, HttpClientConfig::client_certificate
is now a ClientIdentity that is either one PEM or a split pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(clippy): only crates/http may build a reqwest client

Fence reqwest::Client, ClientBuilder and the TLS builder methods with
disallowed-types and disallowed-methods so new code takes a
litellm_http::Client from the pool. crates/http is exempt as the one place
clients are built, and testkit as a dev-only installer. Tests move to
litellm_http::Client::plain_for_test or a pooled client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(secrets-cyberark): keep verifying certificates when the host disables it

Python hands CyberArk its own ssl_verify, which wins over the global
setting, so CYBERARK_SSL_VERIFY unset or true still verifies even when the
host sets ssl_verify false. The pooled client copied the host's Disabled
and would send the API key unverified; fall back to the built-in roots
instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(python-bridge): treat a missing litellm package as no host HTTP settings

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 1a58162630)

* refactor(rust): promote anthropic messages out of experimental_pass_through (#43269)

Co-authored-by: Yujong Lee <yujong@berri.ai>
(cherry picked from commit 2ef3250ec3)

* test: stop CI tests from downloading tokenizer files and images (#43257)

* test: load the embedding base image from a committed 100x100 PNG instead of downloading it

* test: move the volcengine embedding test into tests/unit

* test: check gpt2 and r50k_base tokenizer parity against committed tiktoken reference files

* test: check hub tokenizer selection against an in-memory Hugging Face hub

* test: serve image URLs from respx in the gemini tool-result and format-param tests

* ci: drop the emptied legacy core-utils test path

* test: cover the cohere and anthropic tokenizer paths in the hub tokenizer test

* test: fetch every format-param image through respx and check its bytes reach the request

* test: drop the gpt2 and r50k_base parity tests, which no litellm path uses

* test: drop comments that restate assertions in the format-param test

(cherry picked from commit 2530255624)

* fix(rust): preserve nested optional import failures (#43265)

* fix(rust): preserve nested optional import failures

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(rust): restore Python modules after settings tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit e1a9378093)

* fix(callbacks-legacy-python): traverse and release the retained headers dict (#43274)

* fix(callbacks-legacy-python): traverse and release the retained headers dict

LegacyLogging keeps the headers dict it hands to pre_call and post_call, but
its traverse never reported that edge to the collector and close never dropped
it. A cycle a callback builds through that dict could not be collected, and a
closed call kept the dict alive until the driver dropped the whole adapter.
Visit and clear headers like body, with regression tests for both

* refactor(callbacks-legacy-python): move the test support module into its own file

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
(cherry picked from commit 4eb340abe0)

* test(integration): port langfuse callbacks-in-db coverage to the local harness (#43282)

* test(integration): port langfuse callbacks-in-db coverage to the local harness

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(integration): assert the langfuse db rows after the exported span

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit e11c3f5815)

* test: finish the non-proxy half of tests/test_litellm (#43281)

* test: move key-gated tests/test_litellm SDK tests into tests/llm_translation and drop empty folders

* test: make token counter and health check unit tests run offline

* ci: point unit shards, rust path filter, Makefile and docs at tests/unit

* docs: fix stale test_litellm run paths in moved llm_translation tests

* fix: correct databricks e2e sys.path depth and contributing example path

---------

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 99655b6f86)

* refactor(rust): prepare inference and auth foundations for the gateway (#43287)

* refactor(rust): prepare inference and auth foundations

* fix(rust): keep textract operations parsing from kebab-case model names

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 7ae721bf79)

* feat(rust): add config router and gateway crates (#43289)

Co-authored-by: Yujong Lee <yujong@berri.ai>
(cherry picked from commit affb547525)

* test(integration): run the Langfuse DB-callback test on its own scratch database (#43288)

* test(integration): run the Langfuse DB-callback test on its own scratch database

The test from #43282 wrote success_callback=langfuse and the LANGFUSE_* env
into the shared integration LiteLLM_Config. The suite's long-running gateway
reloads that table and only ever adds callbacks, so it kept exporting to the
test's closed Langfuse fake for the rest of the shard even after the rows were
restored. The owned proxy now gets a scratch database, which also removes the
snapshot/restore code. scratch_database moves into _support/database.py so
test_cache_and_quota and this test share one copy, and the stock-config guard
now checks the callback settings instead of the raw YAML text.

* test(integration): include failure_callback in the stock-config Langfuse guard

(cherry picked from commit dd63637322)

* refactor(rust): expand logging and test coverage across gateway and Anthropic messages (#43295)

* refactor(rust): prepare inference and auth foundations

* fix(rust): keep textract operations parsing from kebab-case model names

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* done

* refactor(types): derive Anthropic beta string conversions with Strum

* fix(anthropic): report missing max_tokens as a missing field

* refactor(rust): type Anthropic messages headers and auth after the Python layout

Delete anthropic/messages/headers.rs. Its OAuth handling, credential ladder
and beta merging move to anthropic/common_utils.rs where Python keeps them
(optionally_handle_anthropic_oauth, get_auth_header, _merge_beta_headers),
and the feature beta injection becomes update_headers_with_anthropic_beta on
the messages config, as in Python. The BaseAnthropicMessagesConfig impl is
unchanged apart from the bodies of validate_environment and request_headers

Beta values are now the AnthropicBeta enum and BetaSet, which sort, dedupe
and comma-join by construction. Request params gain typed speed, tools and
context_management through Recognized, so the beta logic matches on enums
instead of string-comparing JSON. OauthToken parses the sk-ant-oat token once
and the chat config shares that detection instead of its own copy

Case-insensitive header helpers move next to has_header in litellm-http.
One deliberate divergence: a Bearer-prefixed OAuth key configured through
api_key or ANTHROPIC_API_KEY is sent with a single Bearer scheme, where
Python would emit "Bearer Bearer"

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* done

* fix(rust): repair test compilation and clippy failures

resolve auth before building the outbound request in prepare tests, give the host hook tests their own error type, and drop the disallowed reqwest client and err().expect() from core tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit 90873c46de)

* test(proxy_behavior): scope the management proxy fixture to its package so its spend monitor cannot race the spend tests (#43302)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 115668f43e)

* fix(ci): stop stale CI reds, keep unit tests off the host env, retry CyberArk policy conflicts (#43294)

* fix(ci): stop five stale or flaky CI reds and retry CyberArk policy-load conflicts

The Langfuse redaction unit test exports to a local OTLP capture instead of
polling Langfuse Cloud through a recorded lookup. The passthrough worker-kill
test only requires spend rows for requests the surviving worker served. The
spend-routes sweep treats the intentional /spend/capture_rate 503 as expected.
CyberArk retries a 409 policy load in Python, Rust and the e2e Conjur helper
instead of reading it as "variable exists". The integration egress guard now
matches the script's own cgroup, so it no longer blocks the CircleCI agent,
which runs as the same user.

* fix(ci): keep the policy-load backoff typed as float

* fix(ci): retry CyberArk policy loads without blocking the event loop and tighten the worker-kill and Langfuse tests

* fix(secrets): load CyberArk policy one request at a time per manager

* test(secrets): pin that non-conflict CyberArk policy failures are not retried

* test(unit): run tests/unit with only an allowlisted host environment

CircleCI's unit job inherits every project env var, so real provider keys,
REDIS_HOST, DATABASE_URL and AWS or Azure credentials reached tests that
assume none are set. Locally, litellm's import-time load_dotenv did the same
from any .env up the tree. The unit conftest now drops every variable outside
a small allowlist and disables dotenv before litellm is imported.

* test(e2e): name a failed search and the stuck batch status instead of misattributing them

The websearch session test read an empty web_search_tool_result_error block as a
successful search, so a failing search tool surfaced as a session billing bug.
The batch cancellation timeout now reports the last status the proxy returned.

* fix(ci): scrub the host environment per unit test instead of for the whole pytest process

GHA shards run tests/unit next to other suites in one process, so the import-time
scrub deleted MCP_TEST_PEER_PYTHON before tests/mcp_tests read it and the MCP
upstream fell back to the SDK2 interpreter. The two websearch tests that called
OpenAI and Perplexity live are removed: tests/unit no longer sees their keys.

* fix(ci): scrub only the host variables present before litellm is imported

The per-test scrub also deleted TIKTOKEN_CACHE_DIR, which litellm sets at import to
its bundled encodings, so tokenizer paths tried to download them and hit the
socket guard. The prisma setup test now passes its own database URL instead of
reading one another test leaked into the process environment.

* fix(ci): stop the order-dependent unit reds and settle logging tasks on their own queue

LoggingWorker marked a task done on whichever queue was current when the callback
finished, so a callback that outlived an event-loop change raised "task_done()
called too many times" or undercounted the new loop's queue. It now settles the
queue the task came from.

The rest are test isolation fixes for failures that only appeared when another
file ran first on the same xdist worker: a replaced user_api_key_cache, breaker
metrics unregistered by prometheus tests, semantic_router's health-check filter on
uvicorn.access, logging tasks carried over from bedrock tests, a Router-written
model_cost entry, and a stray post captured by the langflow test. The token
counter check now asserts bounded chunking instead of wall-clock time.

* test(e2e/ui): wait for the logout redirect before visiting a protected page

Logout revokes the session server-side before clearing cookies and navigating, so an immediate page.goto either ran with the cookie still set or was aborted by the logout redirect (net::ERR_ABORTED).

* test(unit): restore the prometheus metrics config per test and settle logs carried from earlier tests in the a2a cost tests

* test(router): pin the router clock in the usage counter tests so a minute rollover cannot empty the read

* test(e2e/ui): wait for logout to clear the token cookie instead of for a login redirect

* test(integration/mcp): answer the model-info probe another test's proxy sends to the model double

(cherry picked from commit 14f4c34c61)

* test(e2e): assert only litellm-owned batch behavior and move the blank S3 env pin to an integration test (#43321)

(cherry picked from commit e53e67ede5)

* ci: drop main and litellm_* branch filters from the CircleCI litellm-main workflows (#43272)

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit c822c7fffa)

* test(e2e): accept the otel cost write as a linked root trace (#42931)

* test(e2e): accept the otel cost write as a linked root trace

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(e2e): window and fail-closed the linked otel trace read-back

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(e2e): poll the otel read-back without recursion

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: yuneng <yuneng@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 3ebf6a1fd8)

* refactor(rust): exclude gateway and config crates from rc backport

* refactor(rust): exclude router crate from rc backport

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
Co-authored-by: yassin <yassin@berri.ai>
2026-09-26 14:26:59 -07:00

865 lines
28 KiB
Python

import re
from unittest.mock import MagicMock, patch
import pytest
from litellm.integrations.gitlab.gitlab_client import GitLabClient
from litellm.integrations.gitlab.gitlab_prompt_manager import (
GitLabPromptCache,
GitLabPromptManager,
GitLabPromptTemplate,
GitLabTemplateManager,
decode_prompt_id,
encode_prompt_id,
)
# -----------------------
# GitLabPromptTemplate
# -----------------------
def test_gitlab_prompt_template_creation():
"""Test GitLabPromptTemplate creation and metadata extraction."""
metadata = {
"model": "gpt-4",
"temperature": 0.7,
"input": {"schema": {"text": "string"}},
"output": {"format": "json"},
}
template = GitLabPromptTemplate(
template_id="test_template",
content="Hello {{name}}!",
metadata=metadata,
)
assert template.template_id == "test_template"
assert template.content == "Hello {{name}}!"
assert template.model == "gpt-4"
assert template.optional_params["temperature"] == 0.7
assert template.input_schema == {"text": "string"}
# -----------------------
# GitLabClient init & validation
# -----------------------
def test_gitlab_client_initialization_token_vs_oauth():
"""Test GitLabClient initialization with token and oauth auth methods."""
# token (default)
config_token = {
"project": "group/sub/repo",
"access_token": "glpat-XYZ",
"branch": "main",
}
client = GitLabClient(config_token)
assert client.project == "group/sub/repo"
assert client.access_token == "glpat-XYZ"
assert client.branch == "main"
assert client.auth_method == "token"
# token header is used
assert client.headers.get("Private-Token") == "glpat-XYZ"
assert "Authorization" not in client.headers
# oauth
config_oauth = {
"project": 123456, # numeric project id supported
"access_token": "oauth-bearer",
"auth_method": "oauth",
}
client_oauth = GitLabClient(config_oauth)
assert client_oauth.auth_method == "oauth"
assert client_oauth.headers.get("Authorization") == "Bearer oauth-bearer"
assert "Private-Token" not in client_oauth.headers
def test_gitlab_client_missing_required_fields():
"""Test GitLabClient initialization with missing required fields."""
with pytest.raises(ValueError, match="project and access_token are required"):
GitLabClient({"project": "group/x/repo"})
with pytest.raises(ValueError, match="project and access_token are required"):
GitLabClient({"access_token": "tok"})
# -----------------------
# GitLabClient: get_file_content
# -----------------------
@patch("litellm.integrations.gitlab.gitlab_client.HTTPHandler.get")
def test_gitlab_client_get_file_content_raw_success(mock_get):
"""Successful file content retrieval via RAW endpoint."""
mock_response = MagicMock()
mock_response.text = "file content"
mock_response.content = b"file content"
mock_response.headers = {"content-type": "text/plain"}
mock_response.status_code = 200
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
client = GitLabClient({"project": "g/s/r", "access_token": "tok"})
content = client.get_file_content("prompts/test.prompt")
assert content == "file content"
mock_get.assert_called_once()
@patch("litellm.integrations.gitlab.gitlab_client.HTTPHandler.get")
def test_gitlab_client_get_file_content_raw_404_fallback_json_base64(mock_get):
"""When RAW returns 404, fallback to JSON endpoint and decode base64 content."""
import base64
# First RAW 404
resp_raw = MagicMock()
resp_raw.status_code = 404
resp_raw.raise_for_status.side_effect = Exception()
mock_get.side_effect = [resp_raw]
# Then JSON OK
resp_json = MagicMock()
encoded = base64.b64encode(b"json-content").decode("utf-8")
resp_json.json.return_value = {"content": encoded, "encoding": "base64"}
resp_json.status_code = 200
resp_json.raise_for_status.return_value = None
# We need mock_get to return JSON response second time; easiest: reset side_effect to list of returns
def side_effect(url, headers):
if "/raw?" in url:
return resp_raw
else:
return resp_json
mock_get.side_effect = side_effect
client = GitLabClient({"project": "g/s/r", "access_token": "tok"})
content = client.get_file_content("prompts/test.prompt")
assert content == "json-content"
@patch("litellm.integrations.gitlab.gitlab_client.HTTPHandler.get")
def test_gitlab_client_get_file_content_not_found(mock_get):
"""File not found returns None."""
# Simulate RAW 404 and JSON 404
resp_404 = MagicMock()
resp_404.status_code = 404
resp_404.raise_for_status.side_effect = Exception()
def side_effect(url, headers):
return resp_404
mock_get.side_effect = side_effect
client = GitLabClient({"project": "g/s/r", "access_token": "tok"})
content = client.get_file_content("missing.prompt")
assert content is None
@patch("litellm.integrations.gitlab.gitlab_client.HTTPHandler.get")
def test_gitlab_client_get_file_content_access_denied(mock_get):
"""403 raises a helpful message."""
import httpx
resp = MagicMock()
resp.status_code = 403
# raise_for_status inside client only called on non-404 success path;
# simulate exception path by making the request itself raise an httpx error wrapper
err = httpx.HTTPStatusError("403", request=MagicMock(), response=resp)
mock_get.side_effect = err
client = GitLabClient({"project": "g/s/r", "access_token": "tok"})
with pytest.raises(Exception, match=re.escape("Access denied to file 'test.prompt'")):
client.get_file_content("test.prompt")
@patch("litellm.integrations.gitlab.gitlab_client.HTTPHandler.get")
def test_gitlab_client_get_file_content_auth_failed(mock_get):
"""401 raises auth error."""
import httpx
resp = MagicMock()
resp.status_code = 401
err = httpx.HTTPStatusError("401", request=MagicMock(), response=resp)
mock_get.side_effect = err
client = GitLabClient({"project": "g/s/r", "access_token": "tok"})
with pytest.raises(Exception, match="Authentication failed"):
client.get_file_content("test.prompt")
# -----------------------
# GitLabClient: list_files
# -----------------------
@patch("litellm.integrations.gitlab.gitlab_client.HTTPHandler.get")
def test_gitlab_client_list_files_success(mock_get):
"""List .prompt files via repository tree API."""
mock_response = MagicMock()
mock_response.json.return_value = [
{"type": "blob", "path": "prompts/test1.prompt"},
{"type": "blob", "path": "prompts/test2.prompt"},
{"type": "blob", "path": "prompts/other.txt"},
{"type": "tree", "path": "prompts/subdir"},
]
mock_response.status_code = 200
mock_response.raise_for_status.return_value = None
mock_get.return_value = mock_response
client = GitLabClient({"project": "g/s/r", "access_token": "tok"})
files = client.list_files("prompts", ".prompt", recursive=True)
assert files == ["prompts/test1.prompt", "prompts/test2.prompt"]
# -----------------------
# GitLabTemplateManager: parsing & rendering
# -----------------------
def test_gitlab_prompt_manager_parse_prompt_file():
"""Parse .prompt with YAML frontmatter."""
prompt_content = """---
model: gpt-4
temperature: 0.7
max_tokens: 150
input:
schema:
user_message: string
system_context?: string
---
{% if system_context %}System: {{system_context}}
{% endif %}User: {{user_message}}"""
manager = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
template = manager.prompt_manager._parse_prompt_file(prompt_content, "test_prompt")
assert template.template_id == "test_prompt"
assert template.model == "gpt-4"
assert template.temperature == 0.7
assert template.max_tokens == 150
assert template.input_schema == {
"user_message": "string",
"system_context?": "string",
}
assert "{% if system_context %}" in template.content
def test_gitlab_prompt_manager_parse_prompt_file_no_frontmatter():
"""Parse .prompt without YAML frontmatter."""
prompt_content = "Simple prompt: {{message}}"
manager = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
template = manager.prompt_manager._parse_prompt_file(
prompt_content, "simple_prompt"
)
assert template.template_id == "simple_prompt"
assert template.content == "Simple prompt: {{message}}"
assert template.metadata == {}
def test_gitlab_prompt_manager_render_template_and_errors():
"""Render a stored template; error if missing."""
manager = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
tpl = GitLabPromptTemplate(
template_id="t1",
content="Hello {{name}}! Welcome to {{place}}.",
metadata={"model": "gpt-4"},
)
manager.prompt_manager.prompts["t1"] = tpl
rendered = manager.prompt_manager.render_template(
"t1", {"name": "World", "place": "Earth"}
)
assert rendered == "Hello World! Welcome to Earth."
with pytest.raises(ValueError, match="Template 'nope' not found"):
manager.prompt_manager.render_template("nope", {})
# -----------------------
# GitLabPromptManager: integration & behavior
# -----------------------
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
def test_gitlab_prompt_manager_integration(mock_client_class):
"""Load prompt on init and render."""
mock_client = MagicMock()
mock_client.get_file_content.return_value = """---
model: gpt-4
temperature: 0.7
---
Hello {{name}}!"""
mock_client_class.return_value = mock_client
mgr = GitLabPromptManager(
{"project": "g/s/r", "access_token": "tok"}, prompt_id="test_prompt"
)
assert "test_prompt" in mgr.prompt_manager.prompts
template = mgr.prompt_manager.prompts["test_prompt"]
assert template.model == "gpt-4"
assert template.temperature == 0.7
rendered = mgr.prompt_manager.render_template("test_prompt", {"name": "World"})
assert rendered == "Hello World!"
def test_gitlab_prompt_manager_parse_prompt_to_messages():
"""Parse prompt content into chat messages."""
mgr = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
# single user msg
simple = "Hello there!"
msgs = mgr._parse_prompt_to_messages(simple)
assert msgs == [{"role": "user", "content": "Hello there!"}]
# multi-role
multi = """System: You are helpful.
User: Hi?
Assistant: Hello!"""
msgs = mgr._parse_prompt_to_messages(multi)
assert len(msgs) == 3
assert msgs[0]["role"] == "system" and msgs[0]["content"] == "You are helpful."
assert msgs[1]["role"] == "user" and msgs[1]["content"] == "Hi?"
assert msgs[2]["role"] == "assistant" and msgs[2]["content"] == "Hello!"
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
def test_gitlab_prompt_manager_pre_call_hook_basic(mock_client_class):
"""Pre-call hook parses messages and injects params."""
mock_client = MagicMock()
mock_client.get_file_content.return_value = """---
model: gpt-4
temperature: 0.7
---
System: You are helpful.
User: {{q}}"""
mock_client_class.return_value = mock_client
mgr = GitLabPromptManager(
{"project": "g/s/r", "access_token": "tok"}, prompt_id="p1"
)
original = [{"role": "user", "content": "ignored"}]
msgs, params = mgr.pre_call_hook(
user_id="u",
messages=original,
litellm_params={},
prompt_id="p1",
prompt_variables={"q": "What is AI?"},
)
assert len(msgs) == 2
assert msgs[0]["role"] == "system"
assert msgs[1]["role"] == "user" and msgs[1]["content"] == "What is AI?"
assert params["model"] == "gpt-4" and params["temperature"] == 0.7
def test_gitlab_prompt_manager_pre_call_hook_no_prompt_id():
"""If no prompt_id provided, messages/params unchanged."""
mgr = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
original = [{"role": "user", "content": "Hello"}]
msgs, params = mgr.pre_call_hook(
user_id="u", messages=original, litellm_params={}, prompt_id=None
)
assert msgs == original and params == {}
def test_gitlab_prompt_manager_get_available_prompts():
"""Return keys of stored templates."""
mgr = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
mgr.prompt_manager.prompts.update(
{
"p1": GitLabPromptTemplate("p1", "c1", {}),
"p2": GitLabPromptTemplate("p2", "c2", {}),
}
)
assert set(mgr.get_available_prompts()) == {"p1", "p2"}
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
def test_gitlab_prompt_manager_reload_prompts(mock_client_class):
"""Ensure reload resets and re-inits manager."""
mock_client = MagicMock()
mock_client.get_file_content.return_value = """---
model: gpt-4
---
Hello {{x}}"""
mock_client_class.return_value = mock_client
mgr = GitLabPromptManager(
{"project": "g/s/r", "access_token": "tok"}, prompt_id="t0"
)
assert "t0" in mgr.prompt_manager.prompts
# force reset
with patch.object(mgr, "_prompt_manager", None):
mgr.reload_prompts()
_ = mgr.prompt_manager
# No assertion beyond not raising and property access works
# -----------------------
# YAML fallback parsing
# -----------------------
def test_gitlab_prompt_manager_yaml_parsing_fallback_and_types():
mgr = GitLabPromptManager({"project": "g/s/r", "access_token": "tok"})
yaml_content = """model: gpt-4
temperature: 0.7
max_tokens: 150
enabled: true
disabled: false
count: 42
rate: 0.5"""
parsed = mgr.prompt_manager._parse_yaml_basic(yaml_content)
assert parsed["model"] == "gpt-4"
assert parsed["temperature"] == 0.7
assert parsed["max_tokens"] == 150
assert parsed["enabled"] is True
assert parsed["disabled"] is False
assert parsed["count"] == 42
assert parsed["rate"] == 0.5
# -----------------------
# prompts_path handling + prompt_version (ref) precedence
# -----------------------
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
def test_gitlab_prompt_manager_prompts_path_resolution_and_version(mock_client_class):
"""prompts_path + explicit prompt_version should produce correct repo path and ref."""
mock_client = MagicMock()
mock_client.get_file_content.return_value = "User: {{q}}"
mock_client_class.return_value = mock_client
cfg = {
"project": "g/s/r",
"access_token": "tok",
"prompts_path": "prompts/chat",
}
mgr = GitLabPromptManager(cfg)
_msgs, _params = mgr.pre_call_hook(
user_id="u",
messages=[],
litellm_params={},
prompt_id="folder/sub/my_prompt",
prompt_variables={"q": "ok"},
prompt_version="commit-sha-999",
)
mock_client.get_file_content.assert_any_call(
"prompts/chat/folder/sub/my_prompt.prompt", ref="commit-sha-999"
)
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
def test_gitlab_prompt_manager_version_precedence(mock_client_class):
"""
prompt_version > git_ref kwarg > manager _ref_override.
"""
mock_client = MagicMock()
mock_client.get_file_content.return_value = "User: {{q}}"
mock_client_class.return_value = mock_client
mgr = GitLabPromptManager(
{"project": "g/s/r", "access_token": "tok"}, ref="manager-default"
)
# prompt_version wins over git_ref kwarg
_msgs, _params = mgr.pre_call_hook(
user_id="u",
messages=[],
litellm_params={},
prompt_id="pA",
prompt_variables={"q": "hello"},
prompt_version="sha-111",
git_ref="feature/branch-xyz",
)
mock_client.get_file_content.assert_any_call("pA.prompt", ref="sha-111")
# If no prompt_version, use git_ref kwarg
_msgs, _params = mgr.pre_call_hook(
user_id="u",
messages=[],
litellm_params={},
prompt_id="pB",
prompt_variables={"q": "hello"},
git_ref="hotfix/ref-2",
)
mock_client.get_file_content.assert_any_call("pB.prompt", ref="hotfix/ref-2")
# If neither provided, fall back to manager override
_msgs, _params = mgr.pre_call_hook(
user_id="u",
messages=[],
litellm_params={},
prompt_id="pC",
prompt_variables={"q": "hello"},
)
mock_client.get_file_content.assert_any_call("pC.prompt", ref="manager-default")
# ---------------------------------------------------------------------
# ID Encoding/Decoding helpers
# ---------------------------------------------------------------------
def test_encode_decode_prompt_id_roundtrip():
raw = "invoice/extract"
encoded = encode_prompt_id(raw)
assert encoded == "gitlab::invoice::extract"
assert decode_prompt_id(encoded) == raw
def test_encode_prompt_id_already_encoded():
encoded = "gitlab::test::path"
assert encode_prompt_id(encoded) == encoded
# ---------------------------------------------------------------------
# GitLabTemplateManager behavior
# ---------------------------------------------------------------------
@pytest.fixture
def mock_gitlab_client():
client = MagicMock()
client.get_file_content.return_value = """---
model: bedrock/anthropic.claude-3-sonnet
temperature: 0.3
max_tokens: 100
---
system: You are a helpful bot.
user: Hello {{ name }}
"""
client.list_files.return_value = [
"prompts/chat/hello.prompt",
"prompts/chat/nested/sub.prompt",
]
return client
@pytest.fixture
def manager(mock_gitlab_client):
cfg = {
"project": "group/repo",
"access_token": "token",
"prompts_path": "prompts/chat",
}
return GitLabTemplateManager(gitlab_config=cfg, gitlab_client=mock_gitlab_client)
def test_list_templates_returns_encoded_ids(manager):
ids = manager.list_templates()
assert all(id.startswith("gitlab::") for id in ids)
assert "gitlab::hello" in ids
assert "gitlab::nested::sub" in ids
def test_load_prompt_from_gitlab_parses_metadata(manager, mock_gitlab_client):
manager._load_prompt_from_gitlab("gitlab::hello")
assert "gitlab::hello" in manager.prompts
tmpl = manager.prompts["gitlab::hello"]
assert isinstance(tmpl, GitLabPromptTemplate)
assert tmpl.metadata["model"].startswith("bedrock/")
assert "You are a helpful bot." in tmpl.content
def test_render_template_renders_jinja(manager, mock_gitlab_client):
manager._load_prompt_from_gitlab("gitlab::hello")
output = manager.render_template("gitlab::hello", {"name": "Prishu"})
assert "Hello Prishu" in output
def test_get_template_returns_none_if_not_loaded(manager):
assert manager.get_template("gitlab::missing") is None
def test_repo_path_conversion(manager):
raw = "gitlab::nested::sub"
repo_path = manager._id_to_repo_path(raw)
assert repo_path.endswith("nested/sub.prompt")
# Ensure decode/encode reversibility
decoded = manager._repo_path_to_id(repo_path)
assert decoded == raw
# ---------------------------------------------------------------------
# GitLabPromptManager high-level integration
# ---------------------------------------------------------------------
@pytest.fixture
def prompt_manager(mock_gitlab_client):
cfg = {
"project": "group/repo",
"access_token": "tkn",
"prompts_path": "prompts/chat",
}
return GitLabPromptManager(gitlab_config=cfg, gitlab_client=mock_gitlab_client)
def test_get_prompt_template_renders_content(prompt_manager):
encoded_id = "gitlab::hello"
content, meta = prompt_manager.get_prompt_template(encoded_id, {"name": "World"})
assert "Hello World" in content
assert "model" in meta
def test_pre_call_hook_parses_roles(prompt_manager):
prompt_id = "gitlab::hello"
messages, params = prompt_manager.pre_call_hook(
user_id="user123",
messages=[],
prompt_id=prompt_id,
prompt_variables={"name": "Tester"},
)
assert isinstance(messages, list)
roles = [m["role"] for m in messages]
assert "system" in roles and "user" in roles
assert "model" in params
def test_get_available_prompts_returns_sorted(prompt_manager):
ids = prompt_manager.get_available_prompts()
assert any(id.startswith("gitlab::") for id in ids)
assert ids == sorted(ids)
# ---------------------------------------------------------------------
# GitLabPromptCache behavior
# ---------------------------------------------------------------------
@pytest.fixture
def prompt_cache(mock_gitlab_client):
cfg = {
"project": "group/repo",
"access_token": "tkn",
"prompts_path": "prompts/chat",
}
return GitLabPromptCache(cfg, gitlab_client=mock_gitlab_client)
def test_cache_load_all_builds_internal_maps(prompt_cache):
result = prompt_cache.load_all()
assert isinstance(result, dict)
# check encoded key presence
assert any(k.startswith("gitlab::") for k in result)
assert prompt_cache.list_files()
assert prompt_cache.list_ids()
def test_cache_get_by_id_handles_encoded_and_decoded(prompt_cache):
prompt_cache.load_all()
encoded = "gitlab::hello"
decoded = decode_prompt_id(encoded)
assert prompt_cache.get_by_id(encoded)
assert prompt_cache.get_by_id(decoded)
def test_cache_reload_resets_and_reloads(prompt_cache):
prompt_cache.load_all()
before = set(prompt_cache.list_ids())
prompt_cache.reload()
after = set(prompt_cache.list_ids())
assert before == after
# -----------------------
# Test fakes / fixtures
# -----------------------
class FakeTemplateManager:
"""
Minimal stand-in for GitLabTemplateManager that GitLabPromptCache expects.
"""
def __init__(self, prompts_path="prompts"):
# simulate a configured prompts folder (affects _id_to_repo_path)
self.prompts_path = prompts_path.strip("/")
self.prompts = {} # id -> GitLabPromptTemplate
# Seeds used by list_templates()
self._discoverable_ids = []
# Methods used by GitLabPromptCache.load_all
def list_templates(self, *, recursive: bool = True):
return list(self._discoverable_ids)
def _load_prompt_from_gitlab(self, pid, ref=None):
# Pretend we fetched and parsed a file; add a basic template if not present
if pid not in self.prompts:
self.prompts[pid] = GitLabPromptTemplate(
template_id=pid,
content=f"User: Hello from {pid}",
metadata={"model": "gpt-4", "temperature": 0.1},
)
def get_template(self, pid):
return self.prompts.get(pid)
def _id_to_repo_path(self, pid):
base = f"{self.prompts_path}/" if self.prompts_path else ""
return f"{base}{pid}.prompt"
class FakePromptManagerWrapper:
"""
Minimal wrapper to mimic GitLabPromptManager(prompt_manager=<GitLabTemplateManager>).
GitLabPromptCache.__init__ expects GitLabPromptManager(...).prompt_manager.
"""
def __init__(self, fake_tm):
self.prompt_manager = fake_tm
@pytest.fixture()
def fake_managers():
"""
Provide a fresh FakeTemplateManager plus a wrapper for each test.
"""
tm = FakeTemplateManager(prompts_path="prompts/chat")
wrapper = FakePromptManagerWrapper(tm)
return tm, wrapper
# -----------------------
# Tests
# -----------------------
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabPromptManager")
def test_cache_load_all_encodes_ids_and_populates_maps(mock_pm_cls, fake_managers):
tm, wrapper = fake_managers
# Simulate two files discovered under prompts_path
tm._discoverable_ids = ["a", "sub/b"]
# When GitLabPromptCache constructs GitLabPromptManager(...), return our wrapper
mock_pm_cls.return_value = wrapper
cache = GitLabPromptCache({"project": "g/s/r", "access_token": "tkn"})
result = cache.load_all()
# Encoded keys are present
assert set(result.keys()) == {encode_prompt_id("a"), encode_prompt_id("sub/b")}
# Files map built with full repo paths
expect_a_path = tm._id_to_repo_path("a")
expect_b_path = tm._id_to_repo_path("sub/b")
assert cache.list_files() == [expect_a_path, expect_b_path]
# IDs list is the encoded IDs
assert set(cache.list_ids()) == {encode_prompt_id("a"), encode_prompt_id("sub/b")}
# Stored entries have normalized json shape
a_entry = cache.get_by_id("gitlab::a")
assert a_entry["id"] == "a" # id is the raw (decoded) id in the entry body
assert a_entry["path"] == expect_a_path
assert a_entry["metadata"]["model"] == "gpt-4"
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabPromptManager")
def test_cache_get_by_id_accepts_encoded_and_decoded(mock_pm_cls, fake_managers):
tm, wrapper = fake_managers
tm._discoverable_ids = ["x/y"]
mock_pm_cls.return_value = wrapper
cache = GitLabPromptCache({"project": "g/s/r", "access_token": "tkn"})
cache.load_all()
# Encoded lookup
encoded = encode_prompt_id("x/y")
decoded = "x/y"
by_encoded = cache.get_by_id(encoded)
by_decoded = cache.get_by_id(decoded)
assert by_encoded is not None
assert by_decoded is not None
assert by_encoded == by_decoded # normalization works
# sanity on shape
assert by_encoded["id"] == "x/y"
assert by_encoded["path"].endswith("prompts/chat/x/y.prompt")
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabPromptManager")
def test_cache_reload_clears_then_reloads(mock_pm_cls, fake_managers):
tm, wrapper = fake_managers
tm._discoverable_ids = ["p1"]
mock_pm_cls.return_value = wrapper
cache = GitLabPromptCache({"project": "g/s/r", "access_token": "tkn"})
first = cache.load_all()
assert encode_prompt_id("p1") in first
# Change discovered ids and ensure reload reflects the change
tm._discoverable_ids = ["p2"]
reloaded = cache.reload()
assert encode_prompt_id("p1") not in reloaded
assert encode_prompt_id("p2") in reloaded
# internal maps should reflect only new state
assert cache.list_ids() == [encode_prompt_id("p2")]
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabPromptManager")
def test_cache_skips_when_template_missing_even_after_reload_attempt(
mock_pm_cls, fake_managers
):
"""
If get_template(pid) returns None even after a retry load, the entry is skipped.
"""
class MissingTemplateManager(FakeTemplateManager):
def get_template(self, pid):
# Always return None to trigger the continue path
return None
def _load_prompt_from_gitlab(self, pid, ref=None):
# Pretend to load, but still don't populate prompts so get_template stays None
pass
tm = MissingTemplateManager(prompts_path="prompts")
wrapper = FakePromptManagerWrapper(tm)
mock_pm_cls.return_value = wrapper
cache = GitLabPromptCache({"project": "g/s/r", "access_token": "tkn"})
tm._discoverable_ids = ["will/vanish"]
out = cache.load_all()
assert out == {}
assert cache.list_files() == []
assert cache.list_ids() == []
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabPromptManager")
def test_cache_get_by_file_returns_exact_entry(mock_pm_cls, fake_managers):
tm, wrapper = fake_managers
tm._discoverable_ids = ["alpha", "nested/beta"]
mock_pm_cls.return_value = wrapper
cache = GitLabPromptCache({"project": "g/s/r", "access_token": "tkn"})
cache.load_all()
alpha_path = tm._id_to_repo_path("alpha")
beta_path = tm._id_to_repo_path("nested/beta")
alpha = cache.get_by_file(alpha_path)
beta = cache.get_by_file(beta_path)
assert alpha and alpha["id"] == "alpha"
assert beta and beta["id"] == "nested/beta"