fix: resolve streaming duplication and markdown formatting issues

- Fix text duplication bug with proper delta calculation
- Preserve markdown formatting with chunk-based delivery (50 chars)
- Handle GeneratorExit errors from async context managers
- Update Chat service streaming to preserve formatting
- Ensure proper word-by-word streaming without duplicates
This commit is contained in:
2025-12-07 00:12:52 +01:00
parent 67481515cc
commit 4216d89f12
2 changed files with 47 additions and 37 deletions
+7 -4
View File
@@ -214,9 +214,12 @@ async def create_chat_completion_stream(
in_reasoning = False
elif item.type == "message":
# Stream message content word by word
# Stream message content in chunks (preserves newlines, markdown, etc.)
text = item.data["content"][0]["text"]
for word in text.split():
chunk_size = 50 # characters per chunk
for i in range(0, len(text), chunk_size):
chunk = text[i:i+chunk_size]
yield ChatCompletionChunk(
id=completion_id,
object=constants.CHAT_COMPLETION_CHUNK_OBJECT,
@@ -225,12 +228,12 @@ async def create_chat_completion_stream(
choices=[
ChatCompletionChunkChoice(
index=0,
delta=ChatCompletionChunkDelta(content=f"{word} "),
delta=ChatCompletionChunkDelta(content=chunk),
finish_reason=None,
)
],
)
await asyncio.sleep(0.05) # Simulate typing
await asyncio.sleep(0.02) # Faster since chunks are larger
# Final chunk with finish_reason
yield ChatCompletionChunk(