fix(openai): tool_call delta 重复携带 id/type,修复严格客户端兼容性

部分 OpenAI 兼容客户端要求每个 tool_call delta chunk 都包含 id 和 type
字段,否则会将后续 delta 视为无效。通过 block_to_tool_id 映射在
ContentBlockStart 时记录 tool_id,确保后续 ToolCallDelta 能正确回填。
This commit is contained in:
fawney19
2026-03-15 21:51:44 +08:00
parent 65550159bb
commit d58c27d22d
2 changed files with 20 additions and 2 deletions

View File

@@ -857,15 +857,21 @@ def test_tool_call_stream_index_stable_across_deltas() -> None:
# 收集所有 tool_calls chunk
tc_indices: list[int] = []
tc_ids: list[str] = []
for event in all_events:
for choice in event.get("choices", []):
tcs = choice.get("delta", {}).get("tool_calls")
if isinstance(tcs, list):
for tc in tcs:
tc_indices.append(tc["index"])
tc_ids.append(str(tc.get("id") or ""))
assert len(tc_indices) >= 2, f"expected at least 2 tool_call chunks, got {len(tc_indices)}"
# 同一个 tool call 的所有 chunk 必须使用相同的 index
assert all(
idx == tc_indices[0] for idx in tc_indices
), f"tool_call index should be stable, got: {tc_indices}"
assert all(tc_id == "fc_001" for tc_id in tc_ids), (
"tool_call id should be repeated on every delta for strict OpenAI-compatible clients, "
f"got: {tc_ids}"
)