Skip to content

How Should Parallel Tool Calls Be Grouped in OpenAIFunctionsAgent? #1463

Description

@Savkosii

Context

I noticed something that seems inconsistent with how parallel tool calls should be handled. I'd appreciate clarification on whether this is intended behavior or a potential issue.

The Observation

In constructScratchPad, tool calls are grouped by comparing the Log field:

func (o *OpenAIFunctionsAgent) constructScratchPad(steps []schema.AgentStep) []llms.ChatMessage {
	// ...
	for i, step := range steps {
        if i == 0 || step.Action.Log != steps[i-1].Action.Log {
            // Start a new group
        }
    }
}

However, in ParseOutput, each tool call gets a unique Log that includes the tool name:

func (o *OpenAIFunctionsAgent) ParseOutput(contentResp *llms.ContentResponse) (
	[]schema.AgentAction, *schema.AgentFinish, error,
) {
    actions = append(actions, schema.AgentAction{
        Tool:      functionName,
        ToolInput: toolInput,
        Log:       fmt.Sprintf("Invoking: %s with %s %s", functionName, toolInputStr, contentMsg),
        ToolID:    toolCall.ID,
    })
}

The Issue: Even when multiple tool calls come from the same LLM response, each has a different Log (e.g., "Invoking: search..." vs "Invoking: calculator..."). This means step.Action.Log != steps[i-1].Action.Log is always true, causing each tool call to be treated as a separate group.

Example

When LLM returns parallel tool calls [search, calculator]:

Expected:

AI Message (ToolCalls: [search, calculator])
Tool Result (search result)
Tool Result (calculator result)

Actual:

AI Message (ToolCalls: [search, calculator])
Tool Result (search result)
AI Message (ToolCalls: [search, calculator]) ← Duplicate AI message
Tool Result (calculator result)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions