Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1168d3d
refactor: expose public hidden_params accessors
mateo-berri Oct 5, 2026
03752c1
fix: keep hidden params writes on duck-typed responses
mateo-berri Oct 5, 2026
b46587c
refactor: move hidden params helpers to core utils
mateo-berri Oct 5, 2026
2d4cb09
fix: preserve hidden params in duck-typed cost responses
mateo-berri Oct 6, 2026
1a990af
test: cover hidden params accessors across response types
mateo-berri Oct 6, 2026
aac16d4
fix: preserve hidden params for dynamic responses
mateo-berri Oct 6, 2026
2c2711e
Merge remote-tracking branch 'origin/main' into litellm_private_usage…
mateo-berri Oct 6, 2026
621e5ff
fix: expose logs through backend component
mateo-berri Oct 6, 2026
9598003
Revert "fix: expose logs through backend component"
mateo-berri Oct 6, 2026
0533184
test: cover dict hidden params helper path
mateo-berri Oct 6, 2026
9061b14
fix: preserve hidden params storage on item writes
mateo-berri Oct 6, 2026
f65c00b
test: cover hidden params accessors for OpenAI types
mateo-berri Oct 6, 2026
9040fcb
test: cover hidden params helper and response paths
mateo-berri Oct 6, 2026
366d278
chore: merge main into litellm_private_usage_hidden_params
mateo-berri Oct 7, 2026
2205511
chore: merge current main into litellm_private_usage_hidden_params
mateo-berri Oct 7, 2026
ebd5f5a
chore: merge current main into litellm_private_usage_hidden_params
mateo-berri Oct 7, 2026
b7b0234
chore: merge main into litellm_private_usage_hidden_params
mateo-berri Oct 7, 2026
22db33b
fix: complete hidden params accessor migration
mateo-berri Oct 7, 2026
4d68bfb
chore: merge latest main into litellm_private_usage_hidden_params
mateo-berri Oct 7, 2026
9ca0ea6
fix(litellm): tighten hidden parameter validation
mateo-berri Oct 7, 2026
d054933
fix(litellm): read HiddenParams model storage through get_hidden_params
mateo-berri Oct 7, 2026
2fca5dd
fix(litellm): transfer hidden params storage instead of the mapping view
mateo-berri Oct 7, 2026
7906331
fix(litellm): expose only set HiddenParams keys through the mapping view
mateo-berri Oct 7, 2026
e775d5f
perf(litellm): read HiddenParams view keys without dumping the model
mateo-berri Oct 7, 2026
34609e3
Merge remote-tracking branch 'origin/main' into litellm_private_usage…
mateo-berri Oct 7, 2026
c4c050f
test(litellm): assert HiddenParams extras through model_extra
mateo-berri Oct 7, 2026
913c0a9
fix(router): copy streaming hidden params into a plain dict
mateo-berri Oct 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions enterprise/litellm_enterprise/proxy/hooks/managed_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from litellm.constants import MAX_FILE_LIST_LIMIT
from litellm.files.types import FileRetrieveCallOptions, FileRetrieveProvider
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR
from litellm.litellm_core_utils.prompt_templates.common_utils import (
extract_file_metadata,
)
Expand Down Expand Up @@ -1315,7 +1316,10 @@ async def acreate_file(
model_mappings: Dict[str, str] = {}

for file_object in responses:
model_file_id_mapping = file_object._hidden_params.get("model_file_id_mapping")
file_hidden_params = cast( # cast-ok: preserve mapping operations on dynamic file metadata
dict[str, object], getattr(file_object, HIDDEN_PARAMS_ATTR)
)
model_file_id_mapping = file_hidden_params.get("model_file_id_mapping")
if model_file_id_mapping and isinstance(model_file_id_mapping, dict):
model_mappings.update(model_file_id_mapping)

Expand Down Expand Up @@ -1365,7 +1369,8 @@ async def return_unified_file_id(
_, file_type = extract_file_metadata(create_file_request["file"])

output_file_id = file_objects[0].id
model_id = file_objects[0]._hidden_params.get("model_id")
file_hidden_params: Final = cast(dict[str, object], getattr(file_objects[0], HIDDEN_PARAMS_ATTR))
model_id = file_hidden_params.get("model_id")

unified_file_id = SpecialEnums.LITELLM_MANAGED_FILE_COMPLETE_STR.value.format(
file_type,
Expand Down Expand Up @@ -1431,11 +1436,12 @@ async def async_post_call_success_hook(
if decoded_batch_id and is_litellm_executed_batch(decoded_batch_id):
return response
## Check if unified_file_id is in the response
unified_file_id = response._hidden_params.get("unified_file_id") # managed file id
unified_batch_id = response._hidden_params.get("unified_batch_id") # managed batch id
is_batch_create: Final = response._hidden_params.get(BATCH_CREATE_HIDDEN_PARAM) is True
model_id = cast(Optional[str], response._hidden_params.get("model_id"))
model_name = cast(Optional[str], response._hidden_params.get("model_name"))
response_hidden_params: Final = cast(dict[str, object], getattr(response, HIDDEN_PARAMS_ATTR))
unified_file_id = response_hidden_params.get("unified_file_id")
unified_batch_id = response_hidden_params.get("unified_batch_id")
is_batch_create: Final = response_hidden_params.get(BATCH_CREATE_HIDDEN_PARAM) is True
model_id = cast(Optional[str], response_hidden_params.get("model_id"))
model_name = cast(Optional[str], response_hidden_params.get("model_name"))

resolved_model_name = resolve_managed_output_file_model_name(
unified_input_file_id=unified_file_id if isinstance(unified_file_id, str) else response.input_file_id,
Expand Down Expand Up @@ -1547,12 +1553,12 @@ async def async_post_call_success_hook(

elif isinstance(response, LiteLLMFineTuningJob):
## Check if unified_file_id is in the response
unified_file_id = response._hidden_params.get("unified_file_id") # managed file id
unified_finetuning_job_id = response._hidden_params.get(
"unified_finetuning_job_id"
) # managed finetuning job id
model_id = cast(Optional[str], response._hidden_params.get("model_id"))
model_name = cast(Optional[str], response._hidden_params.get("model_name"))
finetuning_response_hidden_params: Final = cast( # cast-ok: preserve dynamic mapping behavior
dict[str, object], getattr(response, HIDDEN_PARAMS_ATTR)
)
unified_file_id = finetuning_response_hidden_params.get("unified_file_id")
unified_finetuning_job_id = finetuning_response_hidden_params.get("unified_finetuning_job_id")
model_id = cast(Optional[str], finetuning_response_hidden_params.get("model_id"))
original_response_id = response.id
if (unified_file_id or unified_finetuning_job_id) and model_id:
response.id = self.get_unified_generic_response_id(model_id=model_id, generic_response_id=response.id)
Expand Down
35 changes: 17 additions & 18 deletions litellm/caching/caching_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from litellm.caching import InMemoryCache
from litellm.caching.caching import S3Cache, response_cache_phase
from litellm.constants import CACHE_WRITE_SHUTDOWN_FLUSH_TIMEOUT_SECONDS
from litellm.litellm_core_utils.hidden_params import get_hidden_params
from litellm.litellm_core_utils.llm_response_utils.response_metadata import (
update_response_metadata,
)
Expand Down Expand Up @@ -212,6 +213,15 @@ def _request_cache_key(request_kwargs: Mapping[str, Any]) -> str | None:
return request_kwargs.get("cache_key", None)


def _set_cached_hidden_param(response: object, key: str, value: object) -> None:
if isinstance(response, TextCompletionResponse):
setattr(response.hidden_params, key, value)
return
hidden_params: Final = get_hidden_params(response)
if hidden_params is not None:
hidden_params[key] = value


class _CachedEmbeddingRecord(LiteLLMBaseModel):
model_config = ConfigDict(frozen=True)

Expand Down Expand Up @@ -371,8 +381,7 @@ async def async_get_cache(
or self.request_kwargs.get("cache_key")
or litellm.cache.get_cache_key(**self.request_kwargs)
)
if hasattr(cached_result, "_hidden_params"):
cached_result._hidden_params["cache_key"] = cache_key
_set_cached_hidden_param(cached_result, "cache_key", cache_key)
return CachingHandlerResponse(cached_result=cached_result)
elif (
call_type == CallTypes.aembedding.value
Expand Down Expand Up @@ -493,8 +502,7 @@ def sync_get_cache(
or self.request_kwargs.get("cache_key")
or litellm.cache.get_cache_key(**self.request_kwargs)
)
if hasattr(cached_result, "_hidden_params"):
cached_result._hidden_params["cache_key"] = cache_key
_set_cached_hidden_param(cached_result, "cache_key", cache_key)
return CachingHandlerResponse(cached_result=cached_result)
return CachingHandlerResponse(cached_result=cached_result)

Expand Down Expand Up @@ -574,7 +582,7 @@ def _process_async_embedding_cached_response(
model=model_name,
data=[None] * len(kwargs_input_as_list),
)
final_embedding_cached_response._hidden_params["cache_hit"] = True
final_embedding_cached_response.hidden_params["cache_hit"] = True

prompt_tokens = 0
aggregated_details: dict | None = None
Expand Down Expand Up @@ -749,6 +757,7 @@ def combine_cached_embedding_response_with_api_result(
if cached.usage is not None and embedding_response.usage is not None
else cached.usage
)
cached_hidden_params: Final = cached.hidden_params
merged: Final = EmbeddingResponse(
model=cached.model,
data=[
Expand All @@ -759,7 +768,7 @@ def combine_cached_embedding_response_with_api_result(
],
usage=merged_usage,
hidden_params={
**cached._hidden_params,
**cached_hidden_params,
"cache_hit": True,
},
_response_headers=cached._response_headers,
Expand Down Expand Up @@ -1029,12 +1038,7 @@ def _convert_cached_result_to_model_response(
)

response_obj: Final = ResponsesAPIResponse(**cached_result)
if (
hasattr(response_obj, "_hidden_params")
and response_obj._hidden_params is not None
and isinstance(response_obj._hidden_params, dict)
):
response_obj._hidden_params["cache_hit"] = True
_set_cached_hidden_param(response_obj, "cache_hit", True)

if _stream_replay_requested(kwargs):
cached_result = CachedResponsesAPIStreamingIterator(
Expand All @@ -1046,12 +1050,7 @@ def _convert_cached_result_to_model_response(
else:
cached_result = response_obj

if (
hasattr(cached_result, "_hidden_params")
and cached_result._hidden_params is not None
and isinstance(cached_result._hidden_params, dict)
):
cached_result._hidden_params["cache_hit"] = True
_set_cached_hidden_param(cached_result, "cache_hit", True)

#########################################################
# Add final timing metrics to the cached result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import litellm
from litellm import ModelResponse
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.hidden_params import get_hidden_params, get_or_create_hidden_params
from litellm.litellm_core_utils.prompt_templates.common_utils import (
responses_reasoning_items_from_thinking_blocks,
with_prompt_cache_breakpoint,
Expand Down Expand Up @@ -993,20 +994,25 @@ def transform_response(

# Preserve hidden params from the ResponsesAPIResponse, especially the headers
# which contain important provider information like x-request-id
raw_response_hidden_params: Final = getattr(raw_response, "_hidden_params", {})
raw_response_hidden_params: Final = get_hidden_params(raw_response) or {}
if raw_response_hidden_params:
if not hasattr(model_response, "_hidden_params") or model_response._hidden_params is None:
model_response._hidden_params = {}
model_response_hidden_params: Final = get_or_create_hidden_params(model_response)
# Merge the raw_response hidden params with model_response hidden params
# Preserve existing keys in model_response but add/override with raw_response params
for key, value in raw_response_hidden_params.items():
if key == "additional_headers" and key in model_response._hidden_params:
# Merge additional_headers to preserve both sets
existing_additional_headers = model_response._hidden_params.get("additional_headers", {})
merged_headers = {**value, **existing_additional_headers}
model_response._hidden_params[key] = merged_headers
if key == "additional_headers" and key in model_response_hidden_params:
existing_additional_headers = model_response_hidden_params.get("additional_headers", {})
merged_headers = {
**cast( # cast-ok: preserve mapping operations on dynamic response metadata
"dict[str, object]", value
),
**cast( # cast-ok: preserve mapping operations on dynamic response metadata
"dict[str, object]", existing_additional_headers
),
}
model_response_hidden_params[key] = merged_headers
else:
model_response._hidden_params[key] = value
model_response_hidden_params[key] = value

return model_response

Expand Down
9 changes: 7 additions & 2 deletions litellm/cost_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
DEFAULT_MAX_LRU_CACHE_SIZE,
DEFAULT_REPLICATE_GPU_PRICE_PER_SECOND,
)
from litellm.litellm_core_utils.hidden_params import HIDDEN_PARAMS_ATTR
from litellm.litellm_core_utils.llm_cost_calc.tool_call_cost_tracking import (
StandardBuiltInToolCostTracking,
)
Expand Down Expand Up @@ -2028,8 +2029,12 @@ def response_cost_calculator(
response_cost = 0.0
else:
if isinstance(response_object, BaseModel):
if hasattr(response_object, "_hidden_params"):
provider_response_cost: Final = get_response_cost_from_hidden_params(response_object._hidden_params)
if hasattr(response_object, HIDDEN_PARAMS_ATTR):
hidden_params: Final = cast( # cast-ok: cost metadata supports dict and Pydantic storage
dict[str, object] | BaseModel,
getattr(response_object, HIDDEN_PARAMS_ATTR),
)
provider_response_cost: Final = get_response_cost_from_hidden_params(hidden_params)
if provider_response_cost is not None:
return provider_response_cost

Expand Down
2 changes: 1 addition & 1 deletion litellm/decisions/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _parse_response(
response.raise_for_status()
payload: Final[object] = _DECISIONS_PAYLOAD_ADAPTER.validate_json(response.content)
result: Final = _DECISIONS_RESPONSE_ADAPTER.validate_python(prepared.config.unwrap_response(payload))
result._hidden_params.update(
result.hidden_params.update(
{
"model": f"{prepared.provider}/{prepared.upstream_model}",
"custom_llm_provider": prepared.provider,
Expand Down
2 changes: 1 addition & 1 deletion litellm/images/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def image_generation(
else:
model = "dall-e-2"
custom_llm_provider = "openai" # default to dall-e-2 on openai
model_response._hidden_params["model"] = model
model_response.hidden_params["model"] = model
openai_params: Final = [
"user",
"request_timeout",
Expand Down
10 changes: 8 additions & 2 deletions litellm/litellm_core_utils/core_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,18 @@ def redact_nested_match_and_regex_keys(
class _CarriesHiddenParams(Protocol):
_hidden_params: dict[str, object] # mutable-ok: the responses billed here keep hidden params in a plain dict

@property
def hidden_params(self) -> dict[str, object]: ... # mutable-ok: API requires mutation

@hidden_params.setter
def hidden_params(self, hidden_params: dict[str, object]) -> None: ... # mutable-ok: API requires mutation


def set_response_cost_in_hidden_params(response: _CarriesHiddenParams, cost: float | None) -> None:
"""Record a provider-reported cost where the cost calculator looks before the price map."""
if cost is None:
return
hidden_params: Final = response._hidden_params # pyright: ignore[reportPrivateUsage] # no public accessor
hidden_params: Final = response.hidden_params
additional_headers: Final[object] = hidden_params.get("additional_headers")
merged: Final[dict[str, object]] = { # mutable-ok: assigned into the plain-dict hidden params
**(additional_headers if isinstance(additional_headers, Mapping) else _NO_HEADERS),
Expand All @@ -794,7 +800,7 @@ def set_response_cost_in_hidden_params(response: _CarriesHiddenParams, cost: flo
def set_provider_response_headers_in_hidden_params(
response: _CarriesHiddenParams, headers: httpx.Headers | Mapping[str, str]
) -> None:
hidden_params: Final = response._hidden_params # pyright: ignore[reportPrivateUsage] # no public accessor
hidden_params: Final = response.hidden_params
existing_additional_headers: Final[object] = hidden_params.get("additional_headers")
raw_headers: Final[dict[str, str]] = dict(headers) # mutable-ok: stored as the plain-dict hidden param
additional_headers: Final[dict[str, object]] = { # mutable-ok: assigned into the plain-dict hidden params
Expand Down
59 changes: 59 additions & 0 deletions litellm/litellm_core_utils/hidden_params.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Final, Protocol, cast

_HIDDEN_PARAMS_ATTR: Final = "_hidden_params"
HIDDEN_PARAMS_ATTR: Final = _HIDDEN_PARAMS_ATTR


class _SupportsItemAssignment(Protocol):
def __setitem__(self, key: str, value: object) -> None: ...


def _get_hidden_params_storage(obj: object) -> object | None:
return obj.get(_HIDDEN_PARAMS_ATTR) if isinstance(obj, dict) else getattr(obj, _HIDDEN_PARAMS_ATTR, None)


def get_hidden_params(obj: object) -> dict[str, object] | None:
hidden_params: Final[object | None] = _get_hidden_params_storage(obj)
if isinstance(hidden_params, dict):
return cast( # cast-ok: runtime dict validation preserves dynamically typed legacy storage
dict[str, object], hidden_params
)
return None


def set_hidden_params(obj: object, hidden_params: dict[str, object]) -> None:
if isinstance(obj, dict):
obj[_HIDDEN_PARAMS_ATTR] = hidden_params
else:
setattr(obj, _HIDDEN_PARAMS_ATTR, hidden_params)


def set_hidden_param(obj: object, key: str, value: object) -> None:
hidden_params: Final[object | None] = _get_hidden_params_storage(obj)
if hidden_params is None:
set_hidden_params(obj, {key: value})
return
if isinstance(hidden_params, dict):
cast( # cast-ok: runtime dict validation preserves dynamically typed legacy storage
dict[str, object], hidden_params
)[key] = value
return
if hasattr(hidden_params, "__setitem__"):
cast( # cast-ok: hasattr validates the legacy storage assignment protocol
_SupportsItemAssignment, hidden_params
)[key] = value
return
raise TypeError(f"unsupported hidden params storage: {type(hidden_params).__name__}")


def get_or_create_hidden_params(obj: object) -> dict[str, object]:
hidden_params: Final[object | None] = _get_hidden_params_storage(obj)
if hidden_params is None:
created_hidden_params: Final[dict[str, object]] = {}
set_hidden_params(obj, created_hidden_params)
return created_hidden_params
if isinstance(hidden_params, dict):
return cast( # cast-ok: runtime dict validation preserves dynamically typed legacy storage
dict[str, object], hidden_params
)
raise TypeError(f"unsupported hidden params storage: {type(hidden_params).__name__}")
Loading
Loading