Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(logging): merge hidden_params into metadata for streaming completions

Milanf36a59d196e4
diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py
index fea139a64b4..56f7f305dca 100644
--- a/litellm/litellm_core_utils/litellm_logging.py
+++ b/litellm/litellm_core_utils/litellm_logging.py
@@ -1686,6 +1686,28 @@ class Logging(LiteLLMLoggingBaseClass):
)
return logging_result
+ def _merge_hidden_params_from_response_into_metadata(self, logging_result: Any) -> None:
+ """
+ Copy response._hidden_params into litellm_params.metadata['hidden_params'].
+
+ Non-streaming success uses _process_hidden_params_and_response_cost (skipped when
+ stream=True). Streaming assembles the full response later; without this merge,
+ OTEL/callbacks that read metadata.hidden_params miss cost-related fields.
+ """
+ if logging_result is None:
+ return
+ hidden_params = getattr(logging_result, "_hidden_params", None)
+ if not hidden_params:
+ return
+ if self.model_call_details.get("litellm_params") is None:
+ return
+ self.model_call_details["litellm_params"].setdefault("metadata", {})
+ if self.model_call_details["litellm_params"]["metadata"] is None:
+ self.model_call_details["litellm_params"]["metadata"] = {}
+ self.model_call_details["litellm_params"]["metadata"]["hidden_params"] = getattr(
+ logging_result, "_hidden_params", {}
+ )
+
def _process_hidden_params_and_response_cost(
self,
logging_result,
@@ -2010,6 +2032,9 @@ class Logging(LiteLLMLoggingBaseClass):
self.model_call_details[
"response_cost"
] = self._response_cost_calculator(result=complete_streaming_response)
+ self._merge_hidden_params_from_response_into_metadata(
+ complete_streaming_response
+ )
## STANDARDIZED LOGGING PAYLOAD
self.model_call_details[
"standard_logging_object"
@@ -2545,6 +2570,10 @@ class Logging(LiteLLMLoggingBaseClass):
)
self.model_call_details["response_cost"] = None
+ self._merge_hidden_params_from_response_into_metadata(
+ complete_streaming_response
+ )
+
## STANDARDIZED LOGGING PAYLOAD
self.model_call_details[
"standard_logging_object"
diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
index 0f950f6da77..e5fb0ebdf6e 100644
--- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
+++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
@@ -2248,3 +2248,54 @@ def test_failure_handler_runs_sync_callbacks_for_non_pass_through_requests(
)
dummy_logger.log_failure_event.assert_called_once()
+
+
+def test_merge_hidden_params_from_response_into_metadata_populates_metadata():
+ """Streaming completion path should mirror non-stream: metadata.hidden_params from response."""
+ from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+
+ logging_obj = LiteLLMLoggingObj(
+ model="gpt-4o-mini",
+ messages=[{"role": "user", "content": "hi"}],
+ stream=True,
+ call_type="acompletion",
+ start_time=time.time(),
+ litellm_call_id="merge-hp-test",
+ function_id="merge-hp-fn",
+ )
+ logging_obj.model_call_details = {
+ "litellm_params": {"metadata": {}},
+ }
+
+ class _Resp:
+ _hidden_params = {"response_cost": 0.001, "model_id": "mid-test"}
+
+ logging_obj._merge_hidden_params_from_response_into_metadata(_Resp())
+ meta = logging_obj.model_call_details["litellm_params"]["metadata"]
+ assert meta["hidden_params"]["response_cost"] == 0.001
+ assert meta["hidden_params"]["model_id"] == "mid-test"
+
+
+def test_merge_hidden_params_from_response_into_metadata_no_op_when_empty():
+ from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
+
+ logging_obj = LiteLLMLoggingObj(
+ model="gpt-4o-mini",
+ messages=[{"role": "user", "content": "hi"}],
+ stream=True,
+ call_type="acompletion",
+ start_time=time.time(),
+ litellm_call_id="merge-hp-empty",
+ function_id="merge-hp-empty-fn",
+ )
+ logging_obj.model_call_details = {
+ "litellm_params": {"metadata": {"existing": True}},
+ }
+
+ class _NoHp:
+ _hidden_params = {}
+
+ logging_obj._merge_hidden_params_from_response_into_metadata(_NoHp())
+ assert "hidden_params" not in logging_obj.model_call_details["litellm_params"][
+ "metadata"
+ ]