表助手:绑定工具出错(模型不支持工具调用之类)也在流里照实交一条错误事件,不让流半路断掉

This commit is contained in:
ThearchyHelios
2026-10-14 08:28:30 +08:00
parent 77ddbe96ea
commit 8c270987bd
2 changed files with 15 additions and 1 deletions
+2 -1
View File
@@ -813,11 +813,12 @@ async def stream(model: Any, model_id: str, ctx: AssistantContext,
history: list[Any] = [SystemMessage(content=ctx.system), *_history(messages)]
tools = _Tools(ctx)
runner = model.bind_tools(ctx.tools())
usage = {"input_tokens": 0, "output_tokens": 0}
wrote = ""
names = [t["function"]["name"] for t in ctx.tools()]
try:
# 绑工具也放在这里面:响应头已经发出去了,出了错要照实交一条错误事件,不能让流半路断掉
runner = model.bind_tools(ctx.tools())
for round_no in range(MAX_ROUNDS):
fresh = True
message: Any = None
+13
View File
@@ -524,6 +524,19 @@ async def test_errors_end_the_stream_without_done(client, tmp_path, monkeypatch)
assert "上限" in events[-1]["message"]
async def test_a_model_without_tools_reports_an_error_event(client, tmp_path, monkeypatch):
sid = await _source(client, tmp_path)
class NoTools(Scripted):
def bind_tools(self, tools, **_):
raise NotImplementedError("这个模型不支持工具调用")
use(monkeypatch, NoTools([]))
events, _ = await _talk(client, sid, _ask("你好"))
assert events == [{"type": "error", "code": "assistant_failed", "message": events[0]["message"]}]
assert "工具调用" in events[0]["message"]
async def test_transient_errors_are_retried_before_any_output(client, tmp_path, monkeypatch):
sid = await _source(client, tmp_path)
monkeypatch.setattr(catalog_assistant, "RETRY_DELAYS_S", (0.0, 0.0))