Skip to content

Commit f300cb5

Browse files
committed
observability: handle multiple chunk in content response
1 parent 90c7e75 commit f300cb5

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/mistralai/extra/observability/streaming.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from typing import Any
1717

18-
from mistralai.client.models import CompletionChunk, UsageInfo
18+
from mistralai.client.models import CompletionChunk, TextChunk, UsageInfo
1919

2020

2121
def parse_sse_chunks(raw_sse_bytes: bytes) -> list[CompletionChunk]:
@@ -66,8 +66,7 @@ def accumulate_chunks_to_response_dict(
6666
delta = choice.delta
6767
if isinstance(delta.role, str):
6868
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)
7170
if isinstance(choice.finish_reason, str):
7271
accumulated["finish_reason"] = choice.finish_reason
7372
if isinstance(delta.tool_calls, list):
@@ -96,3 +95,18 @@ def accumulate_chunks_to_response_dict(
9695
if usage is not None:
9796
result["usage"] = usage.model_dump(mode="json", by_alias=True)
9897
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

Comments
 (0)