fix: convert tool_use blocks to text in NativeOAISession history to avoid API content type error

This commit is contained in:
Liang Jiaqing
2026-03-29 19:08:20 +08:00
parent 08ac18be6e
commit 844573387d

View File

@@ -510,7 +510,7 @@ class NativeOAISession:
while True: yield next(gen) while True: yield next(gen)
except StopIteration as e: content_blocks = e.value or [] except StopIteration as e: content_blocks = e.value or []
if content_blocks and not (len(content_blocks) == 1 and content_blocks[0].get("text", "").startswith("Error:")): if content_blocks and not (len(content_blocks) == 1 and content_blocks[0].get("text", "").startswith("Error:")):
self.history.append({"role": "assistant", "content": content_blocks}) self.history.append({"role": "assistant", "content": [b if b.get("type") != "tool_use" else {"type": "text", "text": json.dumps(b, ensure_ascii=False)} for b in content_blocks]})
text_parts = [b["text"] for b in content_blocks if b.get("type") == "text"] text_parts = [b["text"] for b in content_blocks if b.get("type") == "text"]
content = "\n".join(text_parts).strip() content = "\n".join(text_parts).strip()
tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in content_blocks if b.get("type") == "tool_use"] tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in content_blocks if b.get("type") == "tool_use"]