mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-09 22:31:41 +00:00
fix(lint): migrate remaining StreamingChoices callers to ModelResponseStream
After narrowing ModelResponse.choices to List[Choices], several files still assigned StreamingChoices into ModelResponse. Migrate streaming call sites to ModelResponseStream and remove dead streaming branches in qwen2/qwen3 transform_response methods.
This commit is contained in:
parent
b370fcd8de
commit
5eeee88ff8
6 changed files with 25 additions and 37 deletions
|
|
@ -26,7 +26,7 @@ from litellm.types.llms.bedrock_agentcore import (
|
|||
AgentCoreUsage,
|
||||
)
|
||||
from litellm.types.llms.openai import AllMessageValues
|
||||
from litellm.types.utils import Choices, Delta, Message, ModelResponse, StreamingChoices, Usage
|
||||
from litellm.types.utils import Choices, Delta, Message, ModelResponse, ModelResponseStream, StreamingChoices, Usage
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
|
||||
|
|
@ -481,7 +481,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
|
|||
text = delta.get("text", "")
|
||||
|
||||
if text:
|
||||
chunk = ModelResponse(
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=model,
|
||||
|
|
@ -499,7 +499,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
|
|||
# Process metadata/usage
|
||||
metadata = event_payload.get("metadata")
|
||||
if metadata and "usage" in metadata:
|
||||
chunk = ModelResponse(
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=model,
|
||||
|
|
@ -522,7 +522,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
|
|||
|
||||
# Process final message
|
||||
if "message" in data_obj and isinstance(data_obj["message"], dict):
|
||||
chunk = ModelResponse(
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=model,
|
||||
|
|
@ -636,7 +636,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
|
|||
text = delta.get("text", "")
|
||||
|
||||
if text:
|
||||
chunk = ModelResponse(
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=model,
|
||||
|
|
@ -654,7 +654,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
|
|||
# Process metadata/usage
|
||||
metadata = event_payload.get("metadata")
|
||||
if metadata and "usage" in metadata:
|
||||
chunk = ModelResponse(
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=model,
|
||||
|
|
@ -677,7 +677,7 @@ class AmazonAgentCoreConfig(BaseConfig, BaseAWSLLM):
|
|||
|
||||
# Process final message
|
||||
if "message" in data_obj and isinstance(data_obj["message"], dict):
|
||||
chunk = ModelResponse(
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=model,
|
||||
|
|
|
|||
|
|
@ -68,13 +68,8 @@ class AmazonQwen2Config(AmazonQwen3Config):
|
|||
# Set the content in the existing model_response structure
|
||||
if hasattr(model_response, 'choices') and len(model_response.choices) > 0:
|
||||
choice = model_response.choices[0]
|
||||
if hasattr(choice, 'message'):
|
||||
choice.message.content = generated_text
|
||||
choice.finish_reason = "stop"
|
||||
else:
|
||||
# Handle streaming choices
|
||||
choice.delta.content = generated_text
|
||||
choice.finish_reason = "stop"
|
||||
choice.message.content = generated_text
|
||||
choice.finish_reason = "stop"
|
||||
|
||||
# Set usage information if available in response
|
||||
if "usage" in response_data:
|
||||
|
|
|
|||
|
|
@ -190,13 +190,8 @@ class AmazonQwen3Config(AmazonInvokeConfig, BaseConfig):
|
|||
# Set the content in the existing model_response structure
|
||||
if hasattr(model_response, 'choices') and len(model_response.choices) > 0:
|
||||
choice = model_response.choices[0]
|
||||
if hasattr(choice, 'message'):
|
||||
choice.message.content = generated_text
|
||||
choice.finish_reason = "stop"
|
||||
else:
|
||||
# Handle streaming choices
|
||||
choice.delta.content = generated_text
|
||||
choice.finish_reason = "stop"
|
||||
choice.message.content = generated_text
|
||||
choice.finish_reason = "stop"
|
||||
|
||||
# Set usage information if available in response
|
||||
if "usage" in response_data:
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from typing import TYPE_CHECKING, Optional
|
|||
import httpx
|
||||
|
||||
from litellm._logging import verbose_logger
|
||||
from litellm.types.utils import Delta, ModelResponse, StreamingChoices
|
||||
from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
|
@ -139,9 +139,9 @@ class LangGraphSSEStreamIterator:
|
|||
return self._create_final_chunk()
|
||||
return None
|
||||
|
||||
def _create_content_chunk(self, text: str) -> ModelResponse:
|
||||
"""Create a ModelResponse chunk with content."""
|
||||
chunk = ModelResponse(
|
||||
def _create_content_chunk(self, text: str) -> ModelResponseStream:
|
||||
"""Create a ModelResponseStream chunk with content."""
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=self.model,
|
||||
|
|
@ -158,9 +158,9 @@ class LangGraphSSEStreamIterator:
|
|||
|
||||
return chunk
|
||||
|
||||
def _create_final_chunk(self) -> ModelResponse:
|
||||
"""Create a final ModelResponse chunk with finish_reason."""
|
||||
chunk = ModelResponse(
|
||||
def _create_final_chunk(self) -> ModelResponseStream:
|
||||
"""Create a final ModelResponseStream chunk with finish_reason."""
|
||||
chunk = ModelResponseStream(
|
||||
id=f"chatcmpl-{uuid.uuid4()}",
|
||||
created=0,
|
||||
model=self.model,
|
||||
|
|
|
|||
|
|
@ -542,16 +542,16 @@ class OpenAIChatCompletionsHandler(BaseTranslation):
|
|||
if len(choice.message.tool_calls) > 0:
|
||||
return True
|
||||
elif isinstance(response, ModelResponseStream):
|
||||
for choice in response.choices:
|
||||
if isinstance(choice, litellm.StreamingChoices):
|
||||
for streaming_choice in response.choices:
|
||||
if isinstance(streaming_choice, litellm.StreamingChoices):
|
||||
# Check for text content
|
||||
if choice.delta.content and isinstance(choice.delta.content, str):
|
||||
if streaming_choice.delta.content and isinstance(streaming_choice.delta.content, str):
|
||||
return True
|
||||
# Check for tool calls
|
||||
if choice.delta.tool_calls and isinstance(
|
||||
choice.delta.tool_calls, list
|
||||
if streaming_choice.delta.tool_calls and isinstance(
|
||||
streaming_choice.delta.tool_calls, list
|
||||
):
|
||||
if len(choice.delta.tool_calls) > 0:
|
||||
if len(streaming_choice.delta.tool_calls) > 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
|
|
|||
|
|
@ -4960,9 +4960,7 @@ def get_response_string(response_obj: Union[ModelResponse, ModelResponseStream])
|
|||
return delta if isinstance(delta, str) else ""
|
||||
|
||||
# Handle standard ModelResponse and ModelResponseStream
|
||||
_choices: Union[List[Union[Choices, StreamingChoices]], List[StreamingChoices]] = (
|
||||
response_obj.choices
|
||||
)
|
||||
_choices: Union[List[Choices], List[StreamingChoices]] = response_obj.choices
|
||||
|
||||
# Use list accumulation to avoid O(n^2) string concatenation across choices
|
||||
response_parts: List[str] = []
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue