Commit Graph

87 Commits

Author SHA1 Message Date
yuneng-jiang 002d64b321 fix(tests): increase MAX_CALLS and reduce sleep in flaky e2e budget test
The test_chat_completion_low_budget test was flaky because async spend
tracking couldn't reliably catch up within 50 calls with 0.5s sleeps.
Increased to 200 calls with 0.1s sleeps (same total time budget) to
give more opportunities for budget enforcement to trigger.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 00:04:31 -07:00
Cursor Agent cc3f9cd65b fix(ci): stabilize CI tests - conditional import, mock fixes, timing adjustments
Fix 1.1: Make ResponseApplyPatchToolCall import conditional with try/except
  for compatibility with openai==1.100.1 (CI environment)
Fix 1.2: Move Router creation inside mock context in vector store tests
  so mocks are applied before Router captures function references
Fix 1.3: Update test_model_group_info_e2e to check for 'anthropic/*'
  wildcard group instead of specific model names not in proxy config
Fix 2.1: Increase redis cache test sleep from 1s to 5s
Fix 2.2: Increase spend accuracy test sleep from 25s to 45s
Fix 2.3: Add 0.5s sleep between budget test calls
Fix 2.4: Increase vertex AI spend test sleep from 20s to 40s

Co-authored-by: yuneng-jiang <yuneng-jiang@users.noreply.github.com>
2026-03-13 00:01:25 +00:00
Ishaan Jaffer b7e48f1d9e test fix 2026-01-31 19:08:07 -08:00
Ishaan Jaffer 466e6bdcf1 fix(test): make test_proxy_failure_metrics resilient to missing proxy-level metrics
- Check for both litellm_proxy_failed_requests_metric_total and the deprecated litellm_llm_api_failed_requests_metric_total
- The proxy-level failure hook may not always be called depending on where the exception occurs
- Simplify total_requests check to only verify key fields

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-01-31 18:46:27 -08:00
Ishaan Jaffer 92c8e00520 test_proxy_success_metrics 2026-01-31 18:36:22 -08:00
Ishaan Jaffer faff9d1dc5 test_proxy_failure_metrics 2026-01-31 18:10:17 -08:00
shin-bot-litellm 0c006794f1 litellm_fix_mapped_tests_core: fix test isolation and mock injection issues (#20209)
* litellm_fix_mapped_tests_core: fix test isolation and mock injection issues

## Problem
Four tests in litellm_mapped_tests_core were failing:
1. test_register_model_with_scientific_notation - KeyError due to test isolation issues
2. test_search_uses_registry_credentials - Mock not being called due to incorrect patch path
3. test_send_email_missing_api_key - Real API calls despite mocking
4. test_stream_transformation_error_sync - Mock not effective, real API called

## Solution

### test_register_model_with_scientific_notation
- Use unique model name to avoid conflicts with other tests
- Clear LRU caches before test to prevent stale data
- Clean up model_cost entry after test

### test_search_uses_registry_credentials
- Use patch.object() on the actual base_llm_http_handler instance
- String-based patching for instance methods can fail; direct object patching is more reliable

### test_send_email_missing_api_key
- Directly inject mock HTTP client into logger instance
- This bypasses any caching issues that could cause the fixture mock to be ineffective

### test_stream_transformation_error_sync
- Patch litellm.completion directly instead of the handler module's litellm reference
- This ensures the mock is effective regardless of import order

## Regression
These tests were affected by LRU caching added in #19606 and HTTP client caching.

* fix(test): use patch.object for container API tests to fix mock injection

## Problem
test_retrieve_container_basic tests were failing because mocks weren't
being applied correctly. The tests used string-based patching:
  patch('litellm.containers.main.base_llm_http_handler')

But base_llm_http_handler is imported at module level, so the mock wasn't
intercepting the actual handler calls, resulting in real HTTP requests
to OpenAI API.

## Solution
Use patch.object() to directly mock methods on the imported handler
instance. Import base_llm_http_handler in the test file and patch like:
  patch.object(base_llm_http_handler, 'container_retrieve_handler', ...)

This ensures the mock is applied to the actual object being used,
regardless of import order or caching.

* fix(test): add missing Prometheus metric labels to test_proxy_failure_metrics

Add client_ip, user_agent, model_id labels to expected metric patterns.
These labels were added in PRs #19717 and #19678 but test wasn't updated.

* fix(test_resend_email): use direct mock injection for all email tests

Extend the mock injection pattern used in test_send_email_missing_api_key
to all other tests in the file:
- test_send_email_success
- test_send_email_multiple_recipients

Instead of relying on fixture-based patching and respx mocks which can
fail due to import order and caching issues, directly inject the mock
HTTP client into the logger instance. This ensures mocks are always used
regardless of test execution order.

* fix(test): use patch.object for image_edit and vector_store tests

- test_image_edit_merges_headers_and_extra_headers: import base_llm_http_handler
  and use patch.object instead of string path patching
- test_search_uses_registry_credentials: import module and patch via
  module.base_llm_http_handler to ensure we patch the right instance

---------

Co-authored-by: Ishaan Jaff <ishaanjaffer0324@gmail.com>
2026-01-31 17:53:54 -08:00
Ishaan Jaffer bb3c2a92a0 fix(test): update test_prometheus with masked user_id and missing labels
- Update expected user_id from 'default_user_id' to '*******_user_id' (PII masking)
- Add missing client_ip, user_agent, model_id labels (from PRs #19717, #19678)
- Update label order to match Prometheus alphabetical sorting

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-01-31 17:51:27 -08:00
Ishaan Jaffer 3a3576dfb4 fix: update test_prometheus to expect masked user_id in metrics
The user_id field 'default_user_id' is being masked to '*******_user_id'
in prometheus metrics for privacy. Updated test expectations to match
the actual behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-01-31 17:26:37 -08:00
yuneng-jiang d01c48ec5e User metrics for promethus 2026-01-07 15:05:09 -08:00
Ishaan Jaffer 6112160a16 Revert "[Fix] Security - Remove example API keys with high entropy (#18255)"
This reverts commit 24edbccf5c.
2025-12-20 20:48:11 +05:30
Alexsander Hamir 24edbccf5c [Fix] Security - Remove example API keys with high entropy (#18255) 2025-12-19 10:09:50 -08:00
yuneng-jiang 085d07db46 Fixing tests 2025-12-18 16:12:06 -08:00
Ishaan Jaff 5ea0854eda [Feat] Guardrails Load Balancing - Allow Platform admins to load balance between guardrails (#18181)
* add _aguardrail_helper for LB

* add _aguardrail_helper on router.py

* test_proxy_logging_pre_call_hook_load_balancing

* add _execute_guardrail_with_load_balancing

* add LB TEsting

* docs guard lb

* fix linting

* fix lint
2025-12-19 00:08:03 +05:30
Krish Dholakia b9f2cc1c98 Model Armor - Logging guardrail response on llm responses (#16977)
* Litellm dev 11 22 2025 p1 (#16975)

* fix(model_armor.py): return response after applying changes

* fix: initial commit adding guardrail span logging to otel on post-call runs

sends it as a separate span right now, need to include in the same llm request/response span

* fix(opentelemetry.py): include guardrail in received request log + set input/ouput fields on parent otel span instead of nesting it

allows request/response to be seen easily on observability tools

* fix(model_armor.py): working model armor logging on post call events

* fix: fix exception message

* fix(opentelemetry.py): add backwards compatibility for litellm_request

allow users building on the spec change to use previous spec
2025-11-22 15:44:28 -08:00
Ishaan Jaffer 14543324af test_team_budget_metrics 2025-11-01 09:21:17 -07:00
Ishaan Jaffer 2d836dfb6d test_basic_moderations_on_proxy_with_model 2025-10-27 13:49:47 -07:00
Alexsander Hamir eaa04cd8ce fix: use fastuuid helper (#14903)
* fix: use fastuuid helper across the codebase

First batch of changes, simple drop in replacement.

* second batch of changes

* fixed: script mistake on helper file
2025-09-25 15:47:01 -07:00
Krrish Dholakia 4d87199266 fix(prometheus.py): fix spend metrics 2025-09-18 19:12:07 -07:00
Krrish Dholakia aa7839e4cb fix: fix test 2025-09-18 19:02:52 -07:00
Mubashir Osmani a7a6381926 fix: flaky passthrough tests (#14692)
* fix: flaky passthrough tests

* Revert "fix: flaky passthrough tests"

This reverts commit ffe692e017600a8853ab7c31f95485958ab74c5f.

* fix: serialize prisma objects
2025-09-18 15:35:14 -07:00
Ishaan Jaffer e733b619db fix: test_user_email_in_all_required_metrics 2025-09-18 11:23:13 -07:00
Ishaan Jaffer 8296bfb866 fix: test metrics 2025-09-18 10:25:22 -07:00
Mubashir Osmani 8b804303ed fix: ci/cd tests + lint errors (#14646)
* fix: lint errors + tests

* fixed ci tests

* fixed tests

---------

Co-authored-by: Ishaan Jaff <ishaanjaffer0324@gmail.com>
2025-09-17 17:06:43 -07:00
boopesh07 36299dbc73 Added user_email labels to the prometheus monitoring. 2025-09-12 15:38:23 -07:00
Ishaan Jaff 2982c2a932 fix test_key_budget_metrics 2025-09-06 16:18:50 -07:00
Ishaan Jaff adeffda681 test_bedrock_guardrail_triggered 2025-07-09 17:05:06 -07:00
Ishaan Jaff e3094c2249 set flaky tests as flaky 2025-06-14 13:51:52 -07:00
Ishaan Jaff 14321a2708 [Feat] Prometheus - Track route on proxy_* metrics (#10992)
* fix: trace route on prometheus metrics

* fix: show route on prometheus metrics for total fails

* test: trace route on metrics

* fix: tests for route in prom metrics

* test: fix test metrics

* test: fix test_proxy_failure_metrics
2025-05-20 22:55:55 -07:00
Krish Dholakia d282babd3e Validate migrating keys to teams + Fix mistral image url on async translation (#10966)
* feat(key_management_endpoints.py): add validation checks for migrating key to team

Ensures requests with migrated key can actually succeed

Prevent migrated keys from failing in prod due to team missing required permissions

* fix(mistral/): fix image url handling for mistral on async call

* fix(key_management_endpoints.py): improve check for running team validation on key update
2025-05-19 21:01:53 -07:00
Prathamesh Saraf ac7b1efe5c Refactor budget assertions in tests to improve clarity and accuracy. Updated remaining hours check to ensure positive values and adjusted budget reset time validation for better range checks. (#10500) 2025-05-02 09:02:02 -07:00
Krish Dholakia 290e2528cd Schedule budget resets at expectable times (#10331) (#10333)
* Schedule budget resets at expectable times (#10331)

* Enhance budget reset functionality with timezone support and standardized reset times

- Added `get_next_standardized_reset_time` function to calculate budget reset times based on specified durations and timezones.
- Introduced `timezone_utils.py` to manage timezone retrieval and budget reset time calculations.
- Updated budget reset logic in `reset_budget_job.py`, `internal_user_endpoints.py`, `key_management_endpoints.py`, and `team_endpoints.py` to utilize the new timezone-aware reset time calculations.
- Added unit tests for the new reset time functionality in `test_duration_parser.py`.
- Updated `.gitignore` to include `test.py` and made minor formatting adjustments in `docker-compose.yml` for consistency.

* Fixed linting

* Fix for mypy

* Fixed testcase for reset

* fix(duration_parser.py): move off zoneinfo - doesn't work with python 3.8

* test: update test

* refactor: improve budget reset time calculation and update related tests for accuracy

* clean up imports in team_endpoints.py

* test: update budget remaining hours assertions to reflect new reset time logic

* build(model_prices_and_context_window.json): update model

---------

Co-authored-by: Prathamesh Saraf <pratamesh1867@gmail.com>
2025-04-29 20:59:44 -07:00
Ishaan Jaff 4e81b2cab4 [Team Member permissions] - Fixes (#9945)
* only load member permissions for non-admins

* run member permission checks on update + regenerate endpoints

* run check for /key/generate

* working test_default_member_permissions

* passing test with permissions on update delete endpoints

* test_create_permissions

* _team_key_generation_check

* fix TeamBase

* fix team endpoints

* fix api docs check
2025-04-12 11:17:51 -07:00
Ishaan Jaff f402e9bbd1 _get_exception_class_name 2025-04-04 21:23:21 -07:00
Ishaan Jaff 271b8b95bc test spend accuracy 2025-03-31 19:35:07 -07:00
Ishaan Jaff a753fc9d9f test_long_term_spend_accuracy_with_bursts 2025-03-31 19:17:13 -07:00
Ishaan Jaff 95e674d1e9 test spend accuracy 2025-03-31 19:01:17 -07:00
Ishaan Jaff ba550e2147 test local spend accuracy 2025-03-28 19:52:39 -07:00
Ishaan Jaff 91a7fb0a23 test string checked for model access control 2025-03-10 20:04:18 -07:00
Ishaan Jaff aa5ac6ba3d can_team_access_model 2025-03-10 20:03:19 -07:00
Ishaan Jaff 8811d4cd11 generate_key 2025-03-10 19:03:50 -07:00
Ishaan Jaff 55ae194912 test_user_email_metrics 2025-02-25 10:47:09 -08:00
Krish Dholakia 57e5ec07cc Improved wildcard route handling on /models and /model_group/info (#8473)
* fix(model_checks.py): update returning known model from wildcard to filter based on given model prefix

ensures wildcard route - `vertex_ai/gemini-*` just returns known vertex_ai/gemini- models

* test(test_proxy_utils.py): add unit testing for new 'get_known_models_from_wildcard' helper

* test(test_models.py): add e2e testing for `/model_group/info` endpoint

* feat(prometheus.py): support tracking total requests by user_email on prometheus

adds initial support for tracking total requests by user_email

* test(test_prometheus.py): add testing to ensure user email is always tracked

* test: update testing for new prometheus metric

* test(test_prometheus_unit_tests.py): add user email to total proxy metric

* test: update tests

* test: fix spend tests

* test: fix test

* fix(pagerduty.py): fix linting error
2025-02-11 19:37:43 -08:00
Ishaan Jaff d19614b8c0 (QA / testing) - Add e2e tests for key model access auth checks (#8000)
* fix _model_matches_any_wildcard_pattern_in_list

* test key model access checks

* add key_model_access_denied to ProxyErrorTypes

* update auth checks

* test_model_access_update

* test_team_model_access_patterns

* fix _team_model_access_check

* fix config used for otel testing

* test fix test_call_with_invalid_model

* fix model acces check tests

* test_team_access_groups

* test _model_matches_any_wildcard_pattern_in_list
2025-01-25 17:15:11 -08:00
Ishaan Jaff a7b3c664d1 (Feat) set guardrails per team (#7993)
* _add_guardrails_from_key_or_team_metadata

* e2e test test_guardrails_with_team_controls

* add try/except on team new

* test_guardrails_with_team_controls

* test_guardrails_with_api_key_controls
2025-01-25 10:41:11 -08:00
Ishaan Jaff bf46ae7346 (Testing) e2e testing for team budget enforcement checks (#7988)
* test_team_and_key_budget_enforcement

* test_team_budget_update

* test_gemini_pro_json_schema_httpx_content_policy_error
2025-01-24 18:18:12 -08:00
Ishaan Jaff 2017596913 Revert "test_team_and_key_budget_enforcement"
This reverts commit 9d44f51847.
2025-01-24 15:32:41 -08:00
Ishaan Jaff 9d44f51847 test_team_and_key_budget_enforcement 2025-01-24 15:31:48 -08:00
Ishaan Jaff 1719dc23c7 (Feat) - emit litellm_team_budget_reset_at_metric and litellm_api_key_budget_remaining_hours_metric on prometheus (#7946)
* set litellm_team_budget_reset_at_metric

* add _get_team_info_from_db_lru_cached

* _set_team_budget_metrics

* e2e test_team_budget_metrics

* update doc string

* add _get_remaining_hours_for_budget_reset

* fix team endpoints

* _get_remaining_hours_for_budget_reset

* _set_key_budget_metrics  on startup

* test_key_budget_metrics

* prom fixes for emitting key / team metrics

* fix _set_api_key_budget_metrics_after_api_request

* test_increment_remaining_budget_metrics

* unit test test_increment_remaining_budget_metrics

* test_initialize_remaining_budget_metrics
2025-01-23 18:12:47 -08:00
Ishaan Jaff 4caf4c0277 (Feat - prometheus) - emit litellm_overhead_latency_metric (#7913)
* add track_llm_api_timing

* add track_llm_api_timing

* test_litellm_overhead

* use ResponseMetadata class for setting hidden params and response overhead

* instrument http handler

* fix track_llm_api_timing

* track_llm_api_timing

* emit response overhead on hidden params

* fix resp metadata

* fix make_sync_openai_embedding_request

* test_aaaaatext_completion_endpoint fixes

* _get_value_from_hidden_params

* set_hidden_params

* test_litellm_overhead

* test_litellm_overhead

* test_litellm_overhead

* fix import

* test_litellm_overhead_stream

* add LiteLLMLoggingObject

* use diff folder for testing

* use diff folder for overhead testing

* test litellm overhead

* use typing

* clear typing

* test_litellm_overhead

* fix async_streaming

* update_response_metadata

* move test file

* emit litellm_overhead_latency_metric on prometheus

* add prometheus callback

* litellm_overhead_latency_metric_bucket

* fix apply hidden params

* fix StandardLoggingHiddenParams
2025-01-21 20:36:30 -08:00