openai-go icon indicating copy to clipboard operation
openai-go copied to clipboard

consider separating tool call requests from messages

Open ruz opened this issue 8 months ago • 0 comments

Hi,

It's not go SDK specific, but publishing here as I don't know where should I report API issues.

Sometimes it's painful to build tool call requests and responses in agentic code. In. my code decided to split "message" from tool calls and sort separate them. From user point of view and stream consumption point of view there is no way to embed tool call into the middle of the message, so each response is an optional text message followed by optional series of tool calls. In theory order may be different (message after tool calls). I do independent processing of each part (msg and tool calls) and send them into UI field. Also, save them into persistent storage ASAP.

Now, when I want to continue the conversation then I get into a problem: 400 Bad Request "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.

Here what I decided to do:

	case common.MessageTypeToolCall:
		// TODO: improve this, may be report a feature request to OpenAI
		return openai.ChatCompletionMessageParamUnion{
			OfAssistant: &openai.ChatCompletionAssistantMessageParam{
				ToolCalls: []openai.ChatCompletionMessageToolCallParam{
					{
						ID: msg.ToolCallID,
						Function: openai.ChatCompletionMessageToolCallFunctionParam{
							Name: msg.ToolName,
							Arguments: msg.ToolInput,
						},
					},
				},
			},
		}
	case common.MessageTypeToolResult:
		return openai.ToolMessage(fmt.Sprintf("Result of calling '%s' tool:\n\n%s", msg.ToolName, msg.Content), msg.ToolCallID)

What I'm proposing is a solution that would make my "hackish way" more legitimate. Just ability to add a tool call request without message.

ruz avatar Apr 08 '25 09:04 ruz