fix(streaming): type upstream chunk helpers against BaseModel

The upstream chunk is an OpenAI ChatCompletionChunk, not a
ModelResponseStream, and main now types it as a protocol. Accept any
pydantic model in the helpers and narrow at the call site so the
rebase does not add a reportArgumentType error

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ed Summers 2026-10-27 09:05:33 -04:00
parent 45d1c82eb4
commit 7151dff3c9
No known key found for this signature in database
GPG key ID: D012FF557AFFF80A
3 changed files with 10 additions and 13 deletions

View file

@ -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 {})

View file

@ -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

View file

@ -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",
),