Skip to content

Commit 59f2cd3

Browse files
committed
Fix duplicate AI messages for parallel tool calls (#1463)
Signed-off-by: vishnuvv27 <vardhanvishnu520@gmail.com>
1 parent 8fea3de commit 59f2cd3

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

agents/openai_functions_agent.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"strings"
78

89
"github.com/tmc/langchaingo/callbacks"
910
"github.com/tmc/langchaingo/chains"
@@ -270,8 +271,20 @@ func (o *OpenAIFunctionsAgent) ParseOutput(contentResp *llms.ContentResponse) (
270271

271272
// Check for new-style tool calls first
272273
if len(choice.ToolCalls) > 0 {
273-
// Handle multiple tool calls properly
274274
actions := make([]schema.AgentAction, 0, len(choice.ToolCalls))
275+
contentMsg := "\n"
276+
if choice.Content != "" {
277+
contentMsg = fmt.Sprintf("responded: %s\n", choice.Content)
278+
}
279+
280+
// Generate a shared log for all tool calls in this choice to ensure they are grouped together
281+
// in constructScratchPad.
282+
var logBuilder strings.Builder
283+
logBuilder.WriteString(contentMsg)
284+
for _, tc := range choice.ToolCalls {
285+
logBuilder.WriteString(fmt.Sprintf("Invoking: %s with %s\n", tc.FunctionCall.Name, tc.FunctionCall.Arguments))
286+
}
287+
sharedLog := logBuilder.String()
275288

276289
for _, toolCall := range choice.ToolCalls {
277290
functionName := toolCall.FunctionCall.Name
@@ -292,15 +305,10 @@ func (o *OpenAIFunctionsAgent) ParseOutput(contentResp *llms.ContentResponse) (
292305
// If JSON parsing failed, use the raw string as tool input
293306
// This handles cases like calculator expressions
294307

295-
contentMsg := "\n"
296-
if choice.Content != "" {
297-
contentMsg = fmt.Sprintf("responded: %s\n", choice.Content)
298-
}
299-
300308
actions = append(actions, schema.AgentAction{
301309
Tool: functionName,
302310
ToolInput: toolInput,
303-
Log: fmt.Sprintf("Invoking: %s with %s %s", functionName, toolInputStr, contentMsg),
311+
Log: sharedLog,
304312
ToolID: toolCall.ID,
305313
})
306314
}

0 commit comments

Comments
 (0)