fix: gateway proxy reads delta.message and detects truncated SSE

Standardize the stream delta on LangChain's own message dict (delta.message)
instead of a bespoke {text, tool_calls} shape, and raise when the stream ends
without the [DONE] sentinel so mid-stream truncation is no longer silent.
This commit is contained in:
m4
2026-08-20 15:32:27 +08:00
parent 3683cbfc13
commit ce0bec1f91
2 changed files with 65 additions and 5 deletions
+10 -3
View File
@@ -122,11 +122,13 @@ class GatewayProxyChatModel(BaseChatModel):
json=payload,
) as response:
response.raise_for_status()
saw_done = False
async for line in response.aiter_lines():
if not line.startswith("data:"):
continue
data = line[len("data:"):].strip()
if data == "[DONE]":
saw_done = True
break
chunk = json.loads(data)
message = _chunk_to_message(chunk)
@@ -134,13 +136,18 @@ class GatewayProxyChatModel(BaseChatModel):
message=message,
generation_info=chunk.get("generation_info"),
)
if not saw_done:
raise RuntimeError("AI4SCI_MODEL_STREAM_INCOMPLETE")
def _chunk_to_message(chunk: dict[str, Any]) -> BaseMessage:
delta = chunk.get("delta") or {}
parsed = messages_from_dict(
[delta.get("message") or {"type": "AIMessageChunk", "data": {"content": delta.get("text", "")}}]
)
message_dict = delta.get("message")
if message_dict is None:
raise RuntimeError("AI4SCI_MODEL_STREAM_DELTA_INVALID")
parsed = messages_from_dict([message_dict])
if len(parsed) != 1:
raise RuntimeError("AI4SCI_MODEL_STREAM_DELTA_INVALID")
return parsed[0]