@devries-cameron20 / composio Mirror · read-only / commits / e78ed312c
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 19:45 UTC

fix(core): run all parallel OpenAI tool calls from the first choice (#3726)

This PR: - supersedes https://github.com/ComposioHQ/composio/pull/3712, preserving the original commits by @serhiizghama (kept as author/co-author) - fixes `OpenAIProvider.handleToolCalls` to execute **every** tool call in an assistant message — it previously only read `tool_calls[0]`, so parallel tool calls (on by default) were dropped and their `tool_call_id`s went unanswered, failing the next request with the "must be followed by tool messages responding to each `tool_call_id`" error - limits execution to the **first choice** — with `n > 1` the previous loop ran each tool call once per choice and orphaned the `tool_call_id`s belonging to the alternative completions - aligns the Python SDK (`OpenAIProvider.handle_tool_calls`) with the same first-choice behavior so both SDKs stay in parity - fixes the stale `@composio/openai` test that modeled "multiple tool calls" as separate `n` choices rather than one message's `tool_calls` array - clarifies in the changeset that the calls run **sequentially**, in the order the model returned them (here "parallel" means the model issued several calls in one turn, not concurrent execution), so each `tool_call_id` is answered exactly once and deterministically - adds regression tests (TS + Python): both calls run for parallel tool calls in one message, and only the first choice runs when `n > 1` Full `@composio/core` (996) and `@composio/openai` suites green; Python `handle_tool_calls` tests, Ruff, and mypy clean; `tsc` clean. --------- Co-authored-by: serhiizghama <zmrser@gmail.com>

Alberto Schiabel committed Jul 2, 2026 at 13:27 UTC e78ed312c6de218ab93592fd9c628b081844969e
6 files changed +222 -34
.changeset/openai-parallel-tool-calls.md new
+9
@@ -0,0 +1,9 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Execute every parallel tool call in `OpenAIProvider.handleToolCalls`. It previously only ran the first tool call in each assistant message, so parallel tool calls (on by default) dropped the rest and left their `tool_call_id`s unanswered, failing the next request.
6 +
7 +The calls are run sequentially, in the order the model returned them — here "parallel" means the model issued several calls in one turn, not that they execute concurrently — so each `tool_call_id` is answered exactly once and the tool messages come back in a deterministic order.
8 +
9 +Only the first choice is handled. Tool results are fed back into a single assistant turn, so with `n > 1` iterating over every choice would run each tool call once per choice and orphan the `tool_call_id`s from the alternative completions.
python/composio/core/provider/_openai.py
+8 -4
@@ -82,10 +82,14 @@ class OpenAIProvider(
82 :return: A list of output objects from the function calls.
83 """
84 outputs = []
85 - for choice in response.choices:
86 - if choice.message.tool_calls is None:
87 - continue
88 -
85 + # Only the first choice is actionable: its tool results feed back into a
86 + # single assistant turn. With n > 1, iterating every choice would run each
87 + # tool call once per choice and orphan the tool_call_ids belonging to the
88 + # alternative completions.
89 + choice = response.choices[0] if response.choices else None
90 + # A single assistant message can carry several tool calls (parallel tool
91 + # calls, on by default); each one needs its own tool result.
92 + if choice is not None and choice.message.tool_calls is not None:
93 for tool_call in choice.message.tool_calls:
94 outputs.append(
95 self.execute_tool_call(
python/tests/test_provider.py
+68
@@ -500,6 +500,74 @@ class TestNonAgenticProviderHelperMethods:
500 assert results[0]["successful"] is True
501 assert results[0]["data"]["starred"] is True
502
503 + def test_openai_provider_handle_tool_calls_only_first_choice(self):
504 + """Only the first choice runs; n > 1 alternatives would orphan tool_call_ids."""
505 + from openai.types.chat import ChatCompletion
506 + from openai.types.chat.chat_completion import Choice
507 + from openai.types.chat.chat_completion_message import ChatCompletionMessage
508 + from openai.types.chat.chat_completion_message_tool_call import (
509 + ChatCompletionMessageToolCall,
510 + Function,
511 + )
512 +
513 + mock_client = mock_http_client()
514 + from composio.core.provider._openai import OpenAIProvider
515 +
516 + provider = OpenAIProvider()
517 +
518 + Tools(
519 + client=mock_client,
520 + provider=provider,
521 + toolkit_versions={"github": "12012025_00"},
522 + )
523 +
524 + github_tool = create_mock_tool("GITHUB_STAR_REPO", "github", "12012025_00")
525 + mock_client.tools.retrieve.return_value = github_tool
526 +
527 + mock_execute_response = Mock()
528 + mock_execute_response.model_dump.return_value = {
529 + "data": {"starred": True},
530 + "error": None,
531 + "successful": True,
532 + }
533 + mock_client.tools.execute.return_value = mock_execute_response
534 +
535 + def make_choice(index: int, call_id: str) -> Choice:
536 + return Choice(
537 + finish_reason="tool_calls",
538 + index=index,
539 + message=ChatCompletionMessage(
540 + role="assistant",
541 + content=None,
542 + tool_calls=[
543 + ChatCompletionMessageToolCall(
544 + id=call_id,
545 + function=Function(
546 + name="GITHUB_STAR_REPO",
547 + arguments='{"repo": "composio/composio"}',
548 + ),
549 + type="function",
550 + )
551 + ],
552 + ),
553 + )
554 +
555 + completion = ChatCompletion(
556 + id="chatcmpl-123",
557 + choices=[make_choice(0, "call_first"), make_choice(1, "call_second")],
558 + created=1234567890,
559 + model="gpt-4",
560 + object="chat.completion",
561 + )
562 +
563 + results = provider.handle_tool_calls(
564 + user_id="test-user",
565 + response=completion,
566 + )
567 +
568 + assert len(results) == 1
569 + assert mock_client.tools.execute.call_count == 1
570 +
571 def test_openai_provider_wrap_tools(self):
572 """Test that OpenAIProvider.wrap_tools creates proper tool definitions."""
573 from composio.core.provider._openai import OpenAIProvider
ts/packages/core/src/provider/OpenAIProvider.ts
+17 -13
@@ -246,20 +246,24 @@ export class OpenAIProvider extends BaseNonAgenticProvider<
246 modifiers?: ExecuteToolModifiers
247 ): Promise<OpenAI.ChatCompletionToolMessageParam[]> {
248 const outputs: OpenAI.ChatCompletionToolMessageParam[] = [];
249 - for (const message of chatCompletion.choices) {
250 - if (message.message.tool_calls && message.message.tool_calls[0].type === 'function') {
251 - const toolResult = await this.executeToolCall(
252 - userId,
253 - message.message.tool_calls[0],
254 - options,
255 - modifiers
256 - );
257 - outputs.push({
258 - role: 'tool',
259 - tool_call_id: message.message.tool_calls[0].id,
260 - content: toolResult,
261 - });
249 + // Only the first choice is actionable: its tool results feed back into a
250 + // single assistant turn. With n > 1, iterating every choice would run each
251 + // tool call once per choice and orphan the tool_call_ids belonging to the
252 + // alternative completions.
253 + const [choice] = chatCompletion.choices;
254 + // A single assistant message can carry several tool calls (parallel tool
255 + // calls, on by default). Each one needs its own tool result, otherwise the
256 + // next request fails since some tool_call_ids go unanswered.
257 + for (const toolCall of choice?.message.tool_calls ?? []) {
258 + if (toolCall.type !== 'function') {
259 + continue;
260 }
261 + const toolResult = await this.executeToolCall(userId, toolCall, options, modifiers);
262 + outputs.push({
263 + role: 'tool',
264 + tool_call_id: toolCall.id,
265 + content: toolResult,
266 + });
267 }
268 return outputs;
269 }
ts/packages/core/test/provider/openai-provider.test.ts
+65 -4
@@ -222,9 +222,8 @@ describe('OpenAIProvider', () => {
222 ]);
223 });
224
225 - it('should handle multiple tool calls', async () => {
225 + it('should handle multiple parallel tool calls in a single message', async () => {
226 const userId = 'test-user';
227 - // Create a mock with correct TypeScript type but using casting to avoid property errors
227 const chatCompletion = {
228 id: 'chat-123',
229 model: 'gpt-4',
@@ -241,7 +240,15 @@ describe('OpenAIProvider', () => {
240 type: 'function',
241 function: {
242 name: 'test-tool',
244 - arguments: JSON.stringify({ input: 'test-value' }),
243 + arguments: JSON.stringify({ input: 'first' }),
244 + },
245 + } as const,
246 + {
247 + id: 'call-456',
248 + type: 'function',
249 + function: {
250 + name: 'other-tool',
251 + arguments: JSON.stringify({ input: 'second' }),
252 },
253 } as const,
254 ],
@@ -263,9 +270,63 @@ describe('OpenAIProvider', () => {
270
271 const results = await provider.handleToolCalls(userId, chatCompletion);
272
266 - expect(executeToolCallSpy).toHaveBeenCalledTimes(1);
273 + // Every parallel tool call must be executed and answered, one tool message
274 + // per tool_call_id — otherwise the follow-up request errors on the unanswered ids.
275 + expect(executeToolCallSpy).toHaveBeenCalledTimes(2);
276 expect(results).toEqual([
277 { role: 'tool', tool_call_id: 'call-123', content: JSON.stringify({ result: 'success' }) },
278 + { role: 'tool', tool_call_id: 'call-456', content: JSON.stringify({ result: 'success' }) },
279 + ]);
280 + });
281 +
282 + it('should only handle tool calls from the first choice when n > 1', async () => {
283 + const userId = 'test-user';
284 + const makeChoice = (index: number, callId: string) => ({
285 + message: {
286 + role: 'assistant',
287 + content: null,
288 + tool_calls: [
289 + {
290 + id: callId,
291 + type: 'function',
292 + function: {
293 + name: 'test-tool',
294 + arguments: JSON.stringify({ input: 'value' }),
295 + },
296 + } as const,
297 + ],
298 + refusal: null,
299 + },
300 + index,
301 + finish_reason: 'tool_calls',
302 + });
303 + const chatCompletion = {
304 + id: 'chat-123',
305 + model: 'gpt-4',
306 + created: 123456789,
307 + object: 'chat.completion',
308 + // n > 1: alternative completions the caller never continues. Only the
309 + // first choice's tool calls should run; the rest would orphan their ids.
310 + choices: [makeChoice(0, 'call-first'), makeChoice(1, 'call-second')],
311 + usage: {
312 + prompt_tokens: 10,
313 + completion_tokens: 20,
314 + total_tokens: 30,
315 + },
316 + } as OpenAI.ChatCompletion;
317 +
318 + const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
319 + executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
320 +
321 + const results = await provider.handleToolCalls(userId, chatCompletion);
322 +
323 + expect(executeToolCallSpy).toHaveBeenCalledTimes(1);
324 + expect(results).toEqual([
325 + {
326 + role: 'tool',
327 + tool_call_id: 'call-first',
328 + content: JSON.stringify({ result: 'success' }),
329 + },
330 ]);
331 });
332 });
ts/packages/providers/openai/test/openai.test.ts
+55 -13
@@ -291,7 +291,7 @@ describe('OpenAIProvider', () => {
291 ]);
292 });
293
294 - it('should handle multiple tool calls', async () => {
294 + it('should handle multiple parallel tool calls in a single message', async () => {
295 const userId = 'test-user';
296 const chatCompletion = {
297 id: 'chat-123',
@@ -303,6 +303,8 @@ describe('OpenAIProvider', () => {
303 message: {
304 role: 'assistant',
305 content: null,
306 + // Parallel tool calls arrive as several entries in one message's
307 + // tool_calls array (on by default), not as separate choices.
308 tool_calls: [
309 {
310 id: 'call-123',
@@ -312,16 +314,6 @@ describe('OpenAIProvider', () => {
314 arguments: JSON.stringify({ input: 'test-value-1' }),
315 },
316 } as const,
315 - ],
316 - },
317 - index: 0,
318 - finish_reason: 'tool_calls' as const,
319 - },
320 - {
321 - message: {
322 - role: 'assistant',
323 - content: null,
324 - tool_calls: [
317 {
318 id: 'call-456',
319 type: 'function',
@@ -332,7 +324,7 @@ describe('OpenAIProvider', () => {
324 } as const,
325 ],
326 },
335 - index: 1,
327 + index: 0,
328 finish_reason: 'tool_calls' as const,
329 },
330 ],
@@ -361,7 +353,7 @@ describe('OpenAIProvider', () => {
353 expect(executeToolCallSpy).toHaveBeenNthCalledWith(
354 2,
355 userId,
364 - chatCompletion.choices[1].message.tool_calls![0],
356 + chatCompletion.choices[0].message.tool_calls![1],
357 undefined,
358 undefined
359 );
@@ -378,6 +370,56 @@ describe('OpenAIProvider', () => {
370 },
371 ]);
372 });
373 +
374 + it('should only handle tool calls from the first choice when n > 1', async () => {
375 + const userId = 'test-user';
376 + const makeChoice = (index: number, callId: string) => ({
377 + message: {
378 + role: 'assistant',
379 + content: null,
380 + tool_calls: [
381 + {
382 + id: callId,
383 + type: 'function',
384 + function: {
385 + name: 'test-tool',
386 + arguments: JSON.stringify({ input: 'value' }),
387 + },
388 + } as const,
389 + ],
390 + },
391 + index,
392 + finish_reason: 'tool_calls' as const,
393 + });
394 + const chatCompletion = {
395 + id: 'chat-123',
396 + model: 'gpt-4',
397 + created: 123456789,
398 + object: 'chat.completion',
399 + // n > 1: alternative completions the caller never continues. Only the
400 + // first choice's tool calls should run; the rest would orphan their ids.
401 + choices: [makeChoice(0, 'call-first'), makeChoice(1, 'call-second')],
402 + usage: {
403 + prompt_tokens: 10,
404 + completion_tokens: 20,
405 + total_tokens: 30,
406 + },
407 + } as OpenAI.ChatCompletion;
408 +
409 + const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
410 + executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
411 +
412 + const results = await provider.handleToolCalls(userId, chatCompletion);
413 +
414 + expect(executeToolCallSpy).toHaveBeenCalledTimes(1);
415 + expect(results).toEqual([
416 + {
417 + role: 'tool',
418 + tool_call_id: 'call-first',
419 + content: JSON.stringify({ result: 'success' }),
420 + },
421 + ]);
422 + });
423 });
424
425 describe('handleAssistantMessage', () => {