|
15 | 15 |
|
16 | 16 | from typing import Any |
17 | 17 |
|
18 | | -from mistralai.client.models import CompletionChunk, UsageInfo |
| 18 | +from mistralai.client.models import CompletionChunk, TextChunk, UsageInfo |
19 | 19 |
|
20 | 20 |
|
21 | 21 | def parse_sse_chunks(raw_sse_bytes: bytes) -> list[CompletionChunk]: |
@@ -66,8 +66,7 @@ def accumulate_chunks_to_response_dict( |
66 | 66 | delta = choice.delta |
67 | 67 | if isinstance(delta.role, str): |
68 | 68 | msg["role"] = delta.role |
69 | | - if isinstance(delta.content, str) and delta.content: |
70 | | - msg["content"] += delta.content |
| 69 | + msg["content"] += _extract_output_text(delta.content) |
71 | 70 | if isinstance(choice.finish_reason, str): |
72 | 71 | accumulated["finish_reason"] = choice.finish_reason |
73 | 72 | if isinstance(delta.tool_calls, list): |
@@ -96,3 +95,18 @@ def accumulate_chunks_to_response_dict( |
96 | 95 | if usage is not None: |
97 | 96 | result["usage"] = usage.model_dump(mode="json", by_alias=True) |
98 | 97 | return result |
| 98 | + |
| 99 | + |
| 100 | +def _extract_output_text(content: Any) -> str: |
| 101 | + if isinstance(content, str): |
| 102 | + return content |
| 103 | + |
| 104 | + if not isinstance(content, list): |
| 105 | + return "" |
| 106 | + |
| 107 | + text_parts: list[str] = [] |
| 108 | + for block in content: |
| 109 | + if isinstance(block, TextChunk): |
| 110 | + text_parts.append(block.text) |
| 111 | + |
| 112 | + return "".join(text_parts) |
0 commit comments