Commit Graph
5648 Commits
Author SHA1 Message Date
Cesar GarciaandGitHub 0ed261b34e fix(gemini): fix negative text_tokens when using cache with images (#18768)
* fix(gemini): prevent negative text_tokens with explicit caching (#18750)

## Problem
When using Gemini with explicit caching (especially with images),
text_tokens would become negative (e.g., -3327) due to incorrectly
subtracting total cached_tokens from modality-specific text_tokens.

## Root Cause
The old code did:
```python
text_tokens = text_tokens - cached_tokens  # 737 - 4064 = -3327
```

This was wrong because:
- cached_tokens includes ALL modalities (text + image + audio + video)
- text_tokens only contains text
- Subtracting total from specific caused negative values

## Solution
Parse cacheTokensDetails to get per-modality cached token breakdown:
```python
if "cacheTokensDetails" in usage_metadata:
    cached_text_tokens = parse from cacheTokensDetails["TEXT"]
    text_tokens = text_tokens - cached_text_tokens  # Correct!
```

Now we subtract cached tokens per modality, preventing negatives.

## Changes
- Parse cacheTokensDetails field from Gemini response
- Calculate non-cached tokens per modality (text, image, audio)
- Remove incorrect global cached_tokens subtraction
- Add tests for explicit caching and implicit/no caching scenarios

## Testing
- Added test_gemini_cache_tokens_details_no_negative_values
- Added test_gemini_without_cache_tokens_details
- All existing Gemini caching tests pass

Fixes #18750

* feat: add cache_read_input_tokens to Usage object

Addresses reviewer feedback to include cached tokens at the top level
of the Usage object. This aligns with how Anthropic provider handles
cached tokens and ensures they are visible in the final usage response.

* fix: add cacheTokensDetails field to UsageMetadata TypedDict

Fixes mypy error where cacheTokensDetails was being accessed but not defined
in the UsageMetadata TypedDict type definition.
2026-01-12 17:04:33 +05:30
932f06104d fix: include IMAGE token count in cost calculation for Gemini models (#18876)
* fix: include IMAGE token count as separate usage count and pricing

* fix: remove duplicate TypedDict key and variable definitions

- Remove duplicate input_cost_per_image_token in ModelInfoBase TypedDict
- Remove duplicate image_tokens variable declaration in _calculate_usage()

Fixes MyPy errors:
- types/utils.py:146: Duplicate TypedDict key
- vertex_and_google_ai_studio_gemini.py:1541: Name already defined

---------

Co-authored-by: Thomas Rehn <271119+tremlin@users.noreply.github.com>
2026-01-12 17:03:42 +05:30
Harshit JainandGitHub 5ce3a56ac3 add better err handling for antropic (#18955) 2026-01-12 17:03:03 +05:30
Ryan MalloyandGitHub c2366194d4 fix(anthropic): prevent dropping thinking when any message has thinking_blocks (#18929)
* fix(anthropic): prevent dropping thinking when any message has thinking_blocks

When Claude returns multiple assistant messages in a conversation, some may
have thinking_blocks while others may not (Claude's behavior varies). The
previous logic only checked the LAST assistant message with tool_calls,
dropping the thinking param if it had no thinking_blocks.

This caused errors when earlier messages still contained thinking_blocks:
"When thinking is disabled, an assistant message cannot contain thinking"

The fix adds a new check: only drop thinking if NO assistant messages
have thinking_blocks. If any message has thinking_blocks, we keep
thinking enabled.

Fixes #18926

* chore: re-trigger CI
2026-01-12 16:29:44 +05:30
Igal BoxermanandGitHub 6bb63525db fix(guardrails): fix SerializationIterator error and pass tools to guardrail (#18932)
* fix(generic-guardrail-api): fix SerializationIterator error on multimodal requests

When sending multimodal messages (with images) through the Generic Guardrail API,
the `model_dump()` call fails with "Object of type SerializationIterator is not
JSON serializable" error.

Root cause: The `ChatCompletionAssistantMessage` type defines `content` as an
`Iterable` (not just `List`), and Pydantic's `model_dump()` creates a
`SerializationIterator` for iterables which is not JSON serializable.

Fix: Use `model_dump(mode="json")` which properly converts all iterables to
lists and ensures all complex objects are JSON serializable.

* fix(guardrails): pass tools (function definitions) to guardrail inputs

The unified guardrail handler was not passing the `tools` parameter
(function definitions) from the request to the guardrail inputs.
This meant guardrails could not inspect or validate tool definitions.

Added extraction of `data.get("tools")` and inclusion in the
GenericGuardrailAPIInputs passed to `apply_guardrail()`.

* test(guardrails): add tests for tools passed to guardrail

Added tests verifying that tools (function definitions) are correctly
passed to guardrails in the unified guardrail handler:
- test_tools_passed_to_guardrail
- test_multiple_tools_passed_to_guardrail
- test_no_tools_in_request
- test_tools_and_tool_calls_both_passed
2026-01-12 16:27:54 +05:30
1169be44b5 fix(google_genai): forward extra_headers in generateContent adapter (#18935)
When using the generateContent endpoint with non-Google providers like
github_copilot, the extra_headers from model config were not being
forwarded to the underlying litellm.completion/acompletion calls.

This caused providers requiring custom headers (e.g., Editor-Version
for GitHub Copilot authentication) to reject requests with errors like
"missing Editor-Version header for IDE auth".

Changes:
- Forward extra_headers in _prepare_completion_kwargs() handler
- Pass extra_headers explicitly to adapter in generate_content()
- Pass extra_headers explicitly to adapter in agenerate_content_stream()
- Pass extra_headers explicitly to adapter in generate_content_stream()
- Add tests for extra_headers forwarding behavior
- Update existing test to expect extra_headers in passed fields

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-12 16:23:58 +05:30
Sameer Kankute 3d38ec4756 Add all replicate models in model cost map 2026-01-12 15:19:49 +05:30
Sameer Kankute 68fdcaea4a Fix: [Bug]: Gemini Image Generation: imageConfig parameters (imageSize, aspectRatio) are completely ignored #18656 2026-01-12 13:38:37 +05:30
Sameer Kankute 508e4da40e Fix: respect max_completion_tokens in thinking feat 2026-01-12 12:44:46 +05:30
Sameer KankuteandGitHub c96a7e80dd Merge pull request #18898 from yogeshwaran10/litellm_fix_gemini_token_usage_details
fix: missing completion_tokens_details in gemini 3 flash when reasoning_effort is not used (#18896)
2026-01-12 12:33:40 +05:30
Sameer Kankute 3c1ffda117 Add: missing anthropic tool results 2026-01-12 11:44:07 +05:30
yogeshwaran10 0e960df8f4 refactor(tests): move gemini token usage tests to test_vertex_and_google_ai_studio_gemini.py 2026-01-12 11:20:13 +05:30
Sameer Kankute dabb459d2b Fix : model id encoding for bedrock passthrough 2026-01-12 10:57:17 +05:30
Sameer Kankute aa97a34a83 Fix tests/test_litellm/llms/bedrock/test_base_aws_llm.py 2026-01-12 09:05:28 +05:30
Sameer Kankute a9e57cf272 [Bug]: Add Custom CA certificates to boto3 clients 2026-01-12 09:05:09 +05:30
Yuta Saito 76f79610b3 fix: forward MCP extra headers case-insensitively 2026-01-12 10:32:51 +09:00
YutaSaitoandGitHub c171954629 Merge pull request #18912 from BerriAI/litellm_fix_pangea_guardrail
[fix] respect pangea guardrail default_on during initialization
2026-01-12 05:43:54 +09:00
Ishaan Jaffer 7aee1fcb81 OCR test fixes 2026-01-11 08:00:31 -08:00
Yuta Saito cef087f88b fix: respect pangea guardrail default_on during initialization 2026-01-11 08:33:31 +09:00
Ishaan Jaffer f1d516f0ae test fixes 2026-01-10 15:15:41 -08:00
Ishaan Jaffer 1ebe979eee test_manus_responses_api_with_file_upload 2026-01-10 15:12:00 -08:00
yuneng-jiang 2e6fe80f90 optional query params for org list 2026-01-10 14:44:28 -08:00
Ishaan Jaffer 6a9041e67d Revert "aws fix base"
This reverts commit 225f411abc.
2026-01-10 14:08:11 -08:00
Ishaan Jaffer 69aaad5e69 Revert "added extraction of top level metadata for custom lables in prometheus callbacks (#18087)"
This reverts commit 14a4a9c031.
2026-01-10 14:00:54 -08:00
Ishaan Jaffer 6b7f114847 test_lists_with_sensitive_keys_are_masked 2026-01-10 13:55:11 -08:00
Ishaan Jaffer 35c636ba97 test_health_check_not_called_when_disabled 2026-01-10 13:55:11 -08:00
Ishaan Jaffer ff8e9aeb5c Revert "Add support for Vertex AI API keys"
This reverts commit ad501048f3.
2026-01-10 13:39:49 -08:00
Ishaan JaffandGitHub c0cf8bc27d [Feat] Manus FILES API - Add File upload, get, delete, list (#18904)
* add MANUS get response

* init TwoStepFileUploadRequest

* init TwoStepFileUploadConfig

* add async_create_file to handle 2 step uploads

* init ManusFilesConfig

* add add get_provider_files_config MANUS

* fix validate_environment

* test_manus_files_api_e2e_all_methods

* aws fix base

* init files API MANUS

* test_manus_responses_api_with_file_upload

* mypy lint fixes

* fix BedrockFilesConfig

* manus docs

* docs manus

* mypy lint

* add add fix resposne api utils MANUS
2026-01-10 13:27:54 -08:00
Ishaan Jaffer 3c3ed3bcfb fix resposne api utils 2026-01-10 13:25:31 -08:00
Ishaan Jaffer e3fe02148d _mask_sequence 2026-01-10 13:20:43 -08:00
Ishaan JaffandGitHub f19cce950c add MANUS get response (#18900) 2026-01-10 12:21:45 -08:00
Ishaan JaffandGitHub ab50fea663 [Fix] turn_off_message_logging Does Not Redact Request Messages in proxy_server_request Field When Stored to Database (#18897)
* update _get_proxy_server_request_for_spend_logs_payload

* test_spend_logs_redacts_request_and_response_when_turn_off_message_logging_enabled
2026-01-10 11:28:21 -08:00
yogeshwaran10 d98c71f07e Fixes #18896 : Handle missing completion_tokens_details when reasoning_effort is not used 2026-01-11 00:45:42 +05:30
Sameer KankuteandGitHub cb03e5a6dd Merge pull request #18852 from BerriAI/litellm_add_ssl_verify_bedrock
[Bug]: Add Custom CA certificates to boto3 clients
2026-01-10 11:41:32 +05:30
Alexsander HamirandGitHub 578abd4465 Add memory leak detection tests with CI integration (#18881) 2026-01-09 17:36:10 -08:00
yuneng-jiang dfb298792c New endpoint for router fields + react query 2026-01-09 16:49:22 -08:00
yuneng-jiang 6022d37eef patching with feature flag 2026-01-09 15:07:53 -08:00
yuneng-jiang 572f3f6d70 Merge commit 'refs/pull/17420/head' of github.com:BerriAI/litellm into litellm_hakhundov_contribution 2026-01-09 14:32:31 -08:00
YutaSaitoandGitHub 07db8fe656 Merge pull request #18855 from BerriAI/litellm_fix_mcp-error-in-multiple-server
[fix] mcp error in multiple servers
2026-01-10 07:26:16 +09:00
yuneng-jiang c29d042df4 Case insensitive email login 2026-01-09 12:51:00 -08:00
yuneng-jiang 1d06cb90e2 Fixing test 2026-01-09 11:51:07 -08:00
Sameer KankuteandGitHub 0c7db97ad5 Merge pull request #18871 from BerriAI/litellm_fix_test_count_tokens_caching
Fix :test_count_tokens_caching
2026-01-10 01:20:55 +05:30
Harshit JainandGitHub 8a683d9a6a Add fix for bedrock_cache, metadata and max_model_budget (#18872) 2026-01-10 01:09:00 +05:30
Sameer Kankute 777ae4f530 Fix :test_count_tokens_caching 2026-01-10 00:57:11 +05:30
Shivam RawatandGitHub 43dd0e6ef5 remove model before casting it in the transformation (#18810) 2026-01-10 00:43:38 +05:30
Cesar GarciaandGitHub c19c97591e fix: align max_tokens with max_output_tokens for consistency (#18820)
* fix: align max_tokens with max_output_tokens for consistency

Fixed inconsistent max_tokens definitions in model_prices_and_context_window.json.
According to LiteLLM convention, max_tokens should equal max_output_tokens when available.

Models fixed:
- deepseek-chat: 131072 → 8192 (now equals max_output_tokens)
- dashscope/qwen-flash: 1000000 → 32768 (now equals max_output_tokens)
- databricks/databricks-gemma-3-12b: 128000 → 32000 (now equals max_output_tokens)

This ensures consistency across all providers where max_tokens represents
the maximum number of tokens that can be generated in the output.

* fix: align max_tokens with max_output_tokens for 244 models

- Fix 244 models where max_tokens != max_output_tokens
- Add test to validate max_tokens consistency and prevent regressions

According to model_prices_and_context_window.json spec:
- max_tokens is a LEGACY parameter
- Should always equal max_output_tokens when both are present

This ensures consistency across all model definitions.
2026-01-10 00:37:45 +05:30
Martin GauthierandGitHub dd087c8cca 🐛 fix: propagate headers in router embedding calls (#18844)
Fix router embedding methods to properly propagate proxy model
configuration headers to LLM API calls by calling
_update_kwargs_before_fallbacks() just like completion() does.

Previously, router.embedding() and router.aembedding() manually
set num_retries and metadata but didn't call
_update_kwargs_before_fallbacks(), which meant default_litellm_params
(including headers) were not propagated correctly.

Changes:
- Replace manual kwargs setup with _update_kwargs_before_fallbacks()
  in _embedding method (litellm/router.py:3318)
- Apply Black formatting to router.py for consistency
- Add comprehensive unit tests for header propagation
- Add integration tests for various router configurations

Tests verify:
- Headers from default_litellm_params are included in embedding calls
- Metadata (model_group) is properly set
- Consistency between completion() and embedding() behavior
- Support for deployment-specific headers, fallbacks, and retries
2026-01-09 23:57:18 +05:30
Harshit JainandGitHub 1203e84162 fix(otel): update semantic conventions to 1.38 (gen_ai attributes) (#18793) 2026-01-09 23:43:44 +05:30
Sameer KankuteandGitHub 8dac83e093 Merge pull request #18859 from BerriAI/litellm_azure_image_gen_fix
Fix: response_format leaking into extra_body
2026-01-09 23:09:27 +05:30
yuneng-jiang 1e314ce203 Merge remote-tracking branch 'origin' into litellm_user_promethus_metrics 2026-01-09 09:33:16 -08:00