Skip to content

Commit 43ae32c

Browse files
committed
Merge branch 'stable/1.106.x' into litellm_backport_lit145_batch_stable_1_106_x
2 parents 0bfe09d + 500e19d commit 43ae32c

71 files changed

Lines changed: 54265 additions & 57858 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎ci_cd/generate_model_prices_schema.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@
6262
}
6363

6464
OBJECT_KEYS: dict[str, JsonSchema] = {
65+
"search_free_requests": {
66+
"type": "object",
67+
"description": "Free Google Search grounding requests per period, shared across models in the same shared_group.",
68+
"required": ["count", "period", "shared_group"],
69+
"additionalProperties": False,
70+
"properties": {
71+
"count": {"type": "integer", "minimum": 0},
72+
"period": {"type": "string", "enum": ["day", "month"]},
73+
"shared_group": {"type": "string", "pattern": "\\S"},
74+
},
75+
},
6576
"off_peak_pricing": {
6677
"type": "object",
6778
"description": "Rates that replace the same-named base fields while the request falls inside the stated UTC windows.",
@@ -91,17 +102,6 @@
91102
},
92103
"additionalProperties": False,
93104
},
94-
"search_free_requests": {
95-
"type": "object",
96-
"description": "Free Google Search grounding requests per period, shared across models in the same shared_group.",
97-
"required": ["count", "period", "shared_group"],
98-
"additionalProperties": False,
99-
"properties": {
100-
"count": {"type": "integer", "minimum": 0},
101-
"period": {"type": "string", "enum": ["day", "month"]},
102-
"shared_group": {"type": "string", "pattern": "\\S"},
103-
},
104-
},
105105
"guardrail_cost_per_unit": {
106106
"type": "object",
107107
"description": "USD cost per billable guardrail unit, keyed by the provider's usage counter name (e.g. Bedrock's contentPolicyUnits).",
@@ -122,8 +122,9 @@
122122
"properties": {
123123
"model": NON_BLANK_STRING,
124124
"region": NON_BLANK_STRING,
125+
"litellm_provider": {"type": "string", "enum": ["bedrock", "bedrock_mantle"]},
125126
},
126-
"description": "Model that counts tokens for this one, and the AWS region its count API runs in.",
127+
"description": "Model that counts tokens for this one, its counting provider, and the AWS region its count API runs in.",
127128
},
128129
}
129130

‎enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py‎

Lines changed: 140 additions & 72 deletions
Large diffs are not rendered by default.

‎enterprise/litellm_enterprise/proxy/hooks/managed_files.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import httpx
2626
from fastapi import HTTPException
27-
from pydantic import ValidationError
27+
from pydantic import BaseModel, JsonValue, TypeAdapter, ValidationError
2828
from typing_extensions import ReadOnly, Unpack
2929

3030
import litellm
@@ -347,6 +347,31 @@ def _managed_object_table(prisma_client: _ManagedResourcePrismaClient) -> _Manag
347347
return prisma_client.db.litellm_managedobjecttable
348348

349349

350+
_JSON_VALUE_ADAPTER: Final[TypeAdapter[JsonValue]] = TypeAdapter(JsonValue)
351+
_POSTGRES_UNSTORABLE_CHARACTER: Final = "\x00"
352+
353+
354+
def _storable_text(text: str) -> str:
355+
return text.replace(_POSTGRES_UNSTORABLE_CHARACTER, "")
356+
357+
358+
def _storable_json_value(value: JsonValue) -> JsonValue:
359+
if isinstance(value, str):
360+
return _storable_text(value)
361+
if isinstance(value, list):
362+
return [_storable_json_value(item) for item in value]
363+
if isinstance(value, dict):
364+
return {_storable_text(key): _storable_json_value(item) for key, item in value.items()}
365+
return value
366+
367+
368+
def postgres_storable_json(file_object: BaseModel) -> str:
369+
dumped: Final = file_object.model_dump_json()
370+
if "\\u0000" not in dumped:
371+
return dumped
372+
return _JSON_VALUE_ADAPTER.dump_json(_storable_json_value(_JSON_VALUE_ADAPTER.validate_json(dumped))).decode()
373+
374+
350375
def _storage_metadata_of(file_object: OpenAIFileObject | None) -> Mapping[str, str]:
351376
hidden_params: Final = cast( # cast-ok: _hidden_params is an untyped attribute the upload path sets
352377
"Mapping[str, object]", getattr(file_object, "_hidden_params", None) or {}

‎litellm-rust/crates/llms/src/anthropic/messages/transformation.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ impl BaseMessagesConfig for AnthropicMessagesConfig {
110110
}
111111

112112
fn request_headers(&self, headers: Headers, request: &MessagesRequest) -> Headers {
113-
update_headers_with_anthropic_beta(headers, request)
113+
merge_beta_headers(
114+
update_headers_with_anthropic_beta(headers, request),
115+
safeguards_betas(&request.params),
116+
)
114117
}
115118
}
116119

@@ -198,6 +201,20 @@ fn uses_structured_output(params: &MessagesOptionalParams) -> bool {
198201
.is_some_and(|config| config.format.is_some())
199202
}
200203

204+
fn safeguards_betas(params: &MessagesOptionalParams) -> BetaSet {
205+
carries_safeguards(params)
206+
.then_some(AnthropicBeta::DangerousToolUse20260903)
207+
.into_iter()
208+
.collect()
209+
}
210+
211+
fn carries_safeguards(params: &MessagesOptionalParams) -> bool {
212+
params
213+
.extra
214+
.get("safeguards")
215+
.is_some_and(|value| !value.is_null())
216+
}
217+
201218
fn messages_carry_output_config(messages: &[Message]) -> bool {
202219
messages
203220
.iter()
@@ -1052,6 +1069,12 @@ mod tests {
10521069
json!({"messages": [{"role": "user", "content": "hi", "output_config": {"effort": "low"}}]}),
10531070
&[("anthropic-beta", "claude-code-20250219,context-management-2025-06-27,effort-2025-11-24,interleaved-thinking-2025-05-14,per-turn-control-2026-07-01")],
10541071
)]
1072+
#[case::safeguards_add_the_dangerous_tool_use_beta(
1073+
&[("anthropic-beta", "claude-code-20250219")],
1074+
json!({"safeguards": [{"type": "dangerous_tool_use"}]}),
1075+
&[("anthropic-beta", "claude-code-20250219,dangerous-tool-use-2026-09-03")],
1076+
)]
1077+
#[case::null_safeguards_add_no_beta(&[("x-api-key", "k")], json!({"safeguards": null}), &[("x-api-key", "k")])]
10551078
fn request_headers_merge_the_feature_betas(
10561079
#[case] input: &[(&str, &str)],
10571080
#[case] fields: Value,

‎litellm/anthropic_beta_headers_config.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@
229229
"computer-use-2025-11-24": "computer-use-2025-11-24",
230230
"context-1m-2025-08-07": "context-1m-2025-08-07",
231231
"context-management-2025-06-27": "context-management-2025-06-27",
232-
"dangerous-tool-use-2026-09-03": null,
232+
"dangerous-tool-use-2026-09-03": "dangerous-tool-use-2026-09-03",
233233
"effort-2025-11-24": "effort-2025-11-24",
234234
"fast-mode-2026-02-01": "fast-mode-2026-02-01",
235235
"files-api-2025-04-14": "files-api-2025-04-14",

‎litellm/anthropic_beta_headers_manager.py‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,28 @@
2222
- LITELLM_ANTHROPIC_BETA_HEADERS_URL: Custom URL for remote config (optional)
2323
"""
2424

25+
import asyncio
2526
import json
2627
import os
28+
import threading
29+
from collections.abc import Mapping
30+
from concurrent.futures import ThreadPoolExecutor
31+
from functools import cache
2732
from importlib.resources import files
28-
from typing import Final
33+
from typing import Final, TypeAlias
2934

3035
import httpx
36+
from pydantic import TypeAdapter
3137

3238
from litellm._logging import verbose_logger
3339

3440
# Cache for the loaded configuration
3541
_BETA_HEADERS_CONFIG: dict | None = None
42+
_BETA_HEADERS_CONFIG_LOCK: Final = threading.Lock()
43+
_BETA_HEADERS_CONFIG_LOADER: Final = ThreadPoolExecutor(max_workers=1, thread_name_prefix="litellm-beta-headers-config")
44+
45+
BetaHeadersConfig: TypeAlias = Mapping[str, str | Mapping[str, str | None]]
46+
_BETA_HEADERS_CONFIG_ADAPTER: Final = TypeAdapter(BetaHeadersConfig)
3647

3748

3849
class GetAnthropicBetaHeadersConfig:
@@ -174,13 +185,27 @@ def _load_beta_headers_config() -> dict:
174185
if _BETA_HEADERS_CONFIG is not None:
175186
return _BETA_HEADERS_CONFIG
176187

177-
# Get the URL from environment or use default
178-
from litellm import anthropic_beta_headers_url
188+
with _BETA_HEADERS_CONFIG_LOCK:
189+
if _BETA_HEADERS_CONFIG is not None:
190+
return _BETA_HEADERS_CONFIG
191+
192+
# Get the URL from environment or use default
193+
from litellm import anthropic_beta_headers_url
194+
195+
_BETA_HEADERS_CONFIG = get_beta_headers_config(url=anthropic_beta_headers_url)
196+
verbose_logger.debug("Loaded and cached beta headers config")
197+
198+
return _BETA_HEADERS_CONFIG
199+
200+
201+
async def aload_beta_headers_config() -> None:
202+
if _BETA_HEADERS_CONFIG is None:
203+
await asyncio.get_running_loop().run_in_executor(_BETA_HEADERS_CONFIG_LOADER, _load_beta_headers_config)
179204

180-
_BETA_HEADERS_CONFIG = get_beta_headers_config(url=anthropic_beta_headers_url)
181-
verbose_logger.debug("Loaded and cached beta headers config")
182205

183-
return _BETA_HEADERS_CONFIG
206+
@cache
207+
def _bundled_beta_headers_config() -> BetaHeadersConfig:
208+
return _BETA_HEADERS_CONFIG_ADAPTER.validate_python(GetAnthropicBetaHeadersConfig.load_local_beta_headers_config())
184209

185210

186211
def reload_beta_headers_config() -> dict:
@@ -288,6 +313,14 @@ def is_beta_header_supported(
288313
return beta_header in provider_mapping and provider_mapping[beta_header] is not None
289314

290315

316+
def is_beta_header_supported_by_bundled_config(beta_header: str, provider: str) -> bool:
317+
config: Final = _bundled_beta_headers_config()
318+
aliases: Final = config.get("provider_aliases", {})
319+
canonical_provider: Final = (aliases.get(provider) if isinstance(aliases, Mapping) else None) or provider
320+
provider_mapping: Final = config.get(canonical_provider, {})
321+
return isinstance(provider_mapping, Mapping) and provider_mapping.get(beta_header) is not None
322+
323+
291324
def get_provider_beta_header(
292325
anthropic_beta_header: str,
293326
provider: str,

‎litellm/batches/batch_utils.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,17 @@ def _batch_response_was_successful(
801801
message batch results lines report ``result.type == "succeeded"``; Bedrock
802802
batch output lines report ``modelOutput`` (and no ``error``).
803803
"""
804-
if custom_llm_provider == "anthropic":
805-
return _get_anthropic_result_from_batch_results_line(batch_job_output_file).get("type") == "succeeded"
806804
if custom_llm_provider == "bedrock":
807805
return batch_job_output_file.get("modelOutput") is not None and batch_job_output_file.get("error") is None
808-
_response: Final[dict[str, object]] = cast( # cast-ok: batch response bodies come from provider JSON
809-
dict[str, object], batch_job_output_file.get("response", None) or {}
806+
envelope_key: Final = "result" if custom_llm_provider == "anthropic" else "response"
807+
envelope: Final = batch_job_output_file.get(envelope_key)
808+
if not isinstance(envelope, Mapping):
809+
if envelope is not None:
810+
verbose_logger.warning("batch output %s must be an object, treating the line as unsuccessful", envelope_key)
811+
return False
812+
response: Final = cast(Mapping[object, object], envelope) # cast-ok: mapping shape validated above
813+
return (
814+
response.get("type") == "succeeded"
815+
if custom_llm_provider == "anthropic"
816+
else response.get("status_code") == 200
810817
)
811-
return _response.get("status_code", None) == 200

0 commit comments

Comments
 (0)