Handle unexpected chunks streamed from Openai (compatible) APIs

This commit is contained in:
Debanjum
2025-07-09 17:49:52 -07:00
parent a601cca79b
commit 3141035f48

View File

@@ -527,7 +527,19 @@ async def adefault_stream_processor(
Async generator to cast and return chunks from the standard openai chat completions stream. Async generator to cast and return chunks from the standard openai chat completions stream.
""" """
async for chunk in chat_stream: async for chunk in chat_stream:
yield ChatCompletionWithThoughtsChunk.model_validate(chunk.model_dump()) try:
# Validate the chunk has the required fields before processing
chunk_data = chunk.model_dump()
# Skip chunks that don't have the required object field or have invalid values
if not chunk_data.get("object") or chunk_data.get("object") != "chat.completion.chunk":
logger.warning(f"Skipping invalid chunk with object field: {chunk_data.get('object', 'missing')}")
continue
yield ChatCompletionWithThoughtsChunk.model_validate(chunk_data)
except Exception as e:
logger.warning(f"Error processing chunk: {e}. Skipping malformed chunk.")
continue
async def adeepseek_stream_processor( async def adeepseek_stream_processor(
@@ -537,14 +549,26 @@ async def adeepseek_stream_processor(
Async generator to cast and return chunks from the deepseek chat completions stream. Async generator to cast and return chunks from the deepseek chat completions stream.
""" """
async for chunk in chat_stream: async for chunk in chat_stream:
tchunk = ChatCompletionWithThoughtsChunk.model_validate(chunk.model_dump()) try:
if ( # Validate the chunk has the required fields before processing
len(tchunk.choices) > 0 chunk_data = chunk.model_dump()
and hasattr(tchunk.choices[0].delta, "reasoning_content")
and tchunk.choices[0].delta.reasoning_content # Skip chunks that don't have the required object field or have invalid values
): if not chunk_data.get("object") or chunk_data.get("object") != "chat.completion.chunk":
tchunk.choices[0].delta.thought = chunk.choices[0].delta.reasoning_content logger.warning(f"Skipping invalid chunk with object field: {chunk_data.get('object', 'missing')}")
yield tchunk continue
tchunk = ChatCompletionWithThoughtsChunk.model_validate(chunk_data)
if (
len(tchunk.choices) > 0
and hasattr(tchunk.choices[0].delta, "reasoning_content")
and tchunk.choices[0].delta.reasoning_content
):
tchunk.choices[0].delta.thought = chunk.choices[0].delta.reasoning_content
yield tchunk
except Exception as e:
logger.warning(f"Error processing chunk: {e}. Skipping malformed chunk.")
continue
def in_stream_thought_processor( def in_stream_thought_processor(