diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index c1c3d29c6fa..e718400fc8e 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -8,7 +8,7 @@ from types import MappingProxyType from typing import TYPE_CHECKING, Any, Final, Literal, Protocol, TypeVar, cast, runtime_checkable import httpx -from pydantic import TypeAdapter, ValidationError +from pydantic import BaseModel, TypeAdapter, ValidationError from typing_extensions import TypeIs # noqa: TID251 # narrows a duck-typed value to the Protocol its caller reads import litellm @@ -634,9 +634,7 @@ def process_response_headers( return {**additional_headers, **({"request-id": request_id} if request_id is not None else {})} -def _upstream_non_openai_keys( - response_type: "type[ModelResponseStream]", original_chunk: "ModelResponseStream" -) -> tuple[str, ...]: +def _upstream_non_openai_keys(response_type: type[BaseModel], original_chunk: BaseModel) -> tuple[str, ...]: # Access model_fields on the class, not the instance, to avoid Pydantic 2.11+ deprecation warnings expected_keys: Final = frozenset(response_type.model_fields) | frozenset({"usage"}) return tuple( @@ -646,13 +644,11 @@ def _upstream_non_openai_keys( ) -def _dump_keys(original_chunk: "ModelResponseStream", keys: tuple[str, ...]) -> Mapping[str, object]: - return original_chunk.model_dump(include=frozenset(keys)) +def _dump_keys(original_chunk: BaseModel, keys: tuple[str, ...]) -> Mapping[str, object]: + return original_chunk.model_dump(include=MappingProxyType(dict.fromkeys(keys, True))) -def upstream_non_openai_attributes( - response_type: "type[ModelResponseStream]", original_chunk: "ModelResponseStream" -) -> Mapping[str, object]: +def upstream_non_openai_attributes(response_type: type[BaseModel], original_chunk: BaseModel) -> Mapping[str, object]: keys: Final = _upstream_non_openai_keys(response_type, original_chunk) return MappingProxyType(_dump_keys(original_chunk, keys) if keys else {}) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index 8d67f914c7d..06e30f320dd 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -1703,9 +1703,10 @@ class CustomStreamWrapper: model_response.choices[0].delta = plain_delta if plain_delta is not None else Delta() plain_delta_created = plain_delta is not None # rebind-ok: the first chunk builds its delta else: - self._usage_chunk_upstream_attributes = upstream_non_openai_attributes( - ModelResponseStream, original_chunk - ) + if isinstance(original_chunk, BaseModel): + self._usage_chunk_upstream_attributes = upstream_non_openai_attributes( + ModelResponseStream, original_chunk + ) if self.send_stream_usage is True: model_response.choices = [] return model_response diff --git a/tests/unit/litellm_core_utils/test_streaming_handler.py b/tests/unit/litellm_core_utils/test_streaming_handler.py index 4c043e0a6d6..3dc8e7f79f8 100644 --- a/tests/unit/litellm_core_utils/test_streaming_handler.py +++ b/tests/unit/litellm_core_utils/test_streaming_handler.py @@ -907,7 +907,7 @@ async def test_final_usage_chunk_keeps_provider_fields_from_usage_only_chunk(syn messages=[{"role": "user", "content": "Hey"}], stream=True, call_type="completion", - start_time=time.time(), + start_time=datetime.datetime.now(), litellm_call_id="usage-fields", function_id="usage-fields", ),