fix(common_request_processing.py): handle chunk being in bytes

fixes anthropic streaming error
This commit is contained in:
Krrish Dholakia
2025-05-24 12:50:18 -07:00
parent 119a73a3fc
commit 9c3cac3046
+7 -2
View File
@@ -40,8 +40,11 @@ else:
from litellm.proxy.litellm_pre_call_utils import add_litellm_data_to_request
async def _parse_event_data_for_error(event_line: str) -> Optional[int]:
async def _parse_event_data_for_error(event_line: Union[str, bytes]) -> Optional[int]:
"""Parses an event line and returns an error code if present, else None."""
event_line = (
event_line.decode("utf-8") if isinstance(event_line, bytes) else event_line
)
if event_line.startswith("data: "):
json_str = event_line[len("data: ") :].strip()
if not json_str or json_str == "[DONE]": # handle empty data or [DONE] message
@@ -121,7 +124,9 @@ async def create_streaming_response(
)
except Exception as e:
# Unexpected error consuming first chunk.
verbose_proxy_logger.error(f"Error consuming first chunk from generator: {e}")
verbose_proxy_logger.exception(
f"Error consuming first chunk from generator: {e}"
)
# Fallback to a generic error stream
async def error_gen_message() -> AsyncGenerator[str, None]: