From 729094e224b6a998c3e6802b157ba8b8719157ee Mon Sep 17 00:00:00 2001 From: huajijiaozhu <87363216+huajijiaozhu@users.noreply.github.com> Date: Tue, 28 Oct 2025 23:53:00 +0800 Subject: [PATCH] Adapt to latest OpenAI API tool call format Current format implemented in code is deprecated and not supported by certain OpenAI API-compatible model providers (e.g. DeepSeek), and causes crashes and/or malfunctions when tool call happens. NOTE: Current code still lacks support for the rare case of multiple tool calls happen in a single completion response; should fix later if proved necessary Co-authored-by: EZForever <34133756+EZForever@users.noreply.github.com> --- sgpt/handlers/handler.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sgpt/handlers/handler.py b/sgpt/handlers/handler.py index a17d8024..17847273 100644 --- a/sgpt/handlers/handler.py +++ b/sgpt/handlers/handler.py @@ -1,4 +1,5 @@ import json +import uuid from pathlib import Path from typing import Any, Callable, Dict, Generator, List, Optional @@ -62,11 +63,21 @@ def handle_function_call( name: str, arguments: str, ) -> Generator[str, None, None]: + tool_call_id = str(uuid.uuid4()) messages.append( { "role": "assistant", "content": "", - "function_call": {"name": name, "arguments": arguments}, + "tool_calls": [ + { + "id": tool_call_id, + "type": "function", + "function": { + "name": name, + "arguments": arguments + } + } + ] } ) @@ -80,7 +91,7 @@ def handle_function_call( result = get_function(name)(**dict_args) if cfg.get("SHOW_FUNCTIONS_OUTPUT") == "true": yield f"```text\n{result}\n```\n" - messages.append({"role": "function", "content": result, "name": name}) + messages.append({"role": "tool", "tool_call_id": tool_call_id, "content": result}) @cache def get_completion(