diff --git a/litellm/llms/openai/chat/gpt_transformation.py b/litellm/llms/openai/chat/gpt_transformation.py index 6b7ec4dfb1c..5e54d492cf0 100644 --- a/litellm/llms/openai/chat/gpt_transformation.py +++ b/litellm/llms/openai/chat/gpt_transformation.py @@ -803,6 +803,22 @@ class OpenAIChatCompletionStreamingHandler(BaseModelResponseIterator): def chunk_parser(self, chunk: dict) -> ModelResponseStream: try: + # Handle error responses embedded in SSE streams (e.g. from sglang) + if "error" in chunk: + error_data = chunk["error"] + if isinstance(error_data, dict): + error_message = error_data.get("message", str(error_data)) + error_code = error_data.get("code", 400) + else: + error_message = str(error_data) + error_code = 400 + raise litellm.BadRequestError( + message=f"Received error in stream: {error_message}", + model=chunk.get("model", ""), + llm_provider=self.custom_llm_provider, + response=httpx.Response(status_code=error_code, request=httpx.Request(method="POST", url="")), + ) + choices = chunk.get("choices", []) choices = self._map_reasoning_to_reasoning_content(choices)