Tool Use & Function Calling

How to Run Parallel OpenAI Tool Calls and Match Each Result in Python

When a Responses API turn returns several function calls, running them one at a time can leave independent work waiting in line. This guide shows how to dispatch independent application-side calls concurrently with Python’s ThreadPoolExecutor, keep each future tied to its original call_id, and send a matching function_call_output back to the model.

The important part is not just concurrency: results can finish in a different order from the calls that produced them. I’ll also cover per-tool errors, checking that every call has exactly one corresponding output, and when to avoid parallel execution—especially for tools that change shared state or have side effects.

The example uses the synchronous OpenAI Python SDK and the Responses API; built-in tools are not part of the parallel function-call batch.

Digital illustration of cyberpunk cats in neon-lit urban scenes, engaged in AI-agent tasks such as coding, debugging, or analyzing data streams. The style features glowing outlines, high contrast, and a vibrant acid-color palette. No brand logos, no invented software screenshots, no legible text.: How to Run Parallel OpenAI Tool Calls and Match Each Result in Python

Quick answer

The short version: inspect response.output for function_call items, parse their arguments, and keep each call’s call_id attached as you dispatch its application-side function. Submit only work you have judged independent to a ThreadPoolExecutor, storing each future with its call ID so completion order cannot scramble the results.

As each future finishes, create one function_call_output for that call. If the tool raises an exception, return an explicit error result with the same ID rather than dropping the output.

Submit the output items in a follow-up Responses API request using previous_response_id, then check whether the model returned more function calls.

Use parallel_tool_calls=False when you need to prevent multiple function calls in a turn. The SDK requires Python 3.10 or later; install it with pip install openai and configure your API key.

Keep SDK request errors distinct from tool-function failures, and check the backing service’s concurrency and idempotency rules before running or retrying side-effecting operations.

Step-by-step guide

Prerequisites and starting state

Sources: Async tool calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Request a Responses API turn and identify function calls

Use the client and function-tool definitions from the prerequisites to make a Responses API request. The response may contain one or more function_call items in response.output; each supplies a tool name, JSON arguments, and a call_id for matching its eventual result.

import json

response = client.responses.create(
    model=model_name,
    input=user_input,
    tools=tools,
)

calls = [item for item in response.output if item.type == "function_call"]
parsed_calls = [
    {"call_id": item.call_id, "name": item.name,
     "arguments": json.loads(item.arguments)}
    for item in calls
]

parsed_calls now holds the identity and decoded arguments for each requested function. Use the name to select your application-side implementation; keep each call_id attached to its call as you move into execution.

If calls is empty, there is no function call to dispatch in this turn.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Choose which calls are safe to run together

Before submitting calls to a thread pool, check that each tool can run without depending on another call’s result or changing shared state in a way that affects another call. For example, fetching a customer profile and checking an unrelated delivery estimate may be independent; two calls that update the same account are not automatically safe to run together.

If you want to prevent the model from returning multiple function calls in a turn, set parallel_tool_calls=False on the Responses API request:

response = client.responses.create(
    model=model_name,
    input=user_input,
    tools=tools,
    parallel_tool_calls=False,
)

This is a useful guard when your tools must run one at a time, but it does not establish that a side-effecting tool is safe to retry. Built-in tools cannot be included in a parallel function-call batch, so keep this batch limited to your application-side functions.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Submit independent work to a thread pool

Submit each independent call to a ThreadPoolExecutor, keeping its future paired with the originating call_id. The executor runs the application-side functions; it does not send the results to the model.

from concurrent.futures import ThreadPoolExecutor

# execute_tool(name, arguments) dispatches to your application functions.
with ThreadPoolExecutor() as pool:
    future_to_call_id = {
        pool.submit(execute_tool, call["name"], call["arguments"]): call["call_id"]
        for call in parsed_calls
    }

Use future_to_call_id when collecting results, rather than assuming jobs finish in submission order. Keep this work limited to calls already judged independent; the next step handles results and individual tool exceptions.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Collect results and preserve call-ID mapping

Collect futures as they finish, but retrieve each call ID from the future mapping—not from the result order. Serialize successful results and exceptions alike so every call has an output tied to its original ID.

import json
from concurrent.futures import as_completed

outputs = []
for future in as_completed(future_to_call_id):
    call_id = future_to_call_id[future]
    try:
        result = future.result()
    except Exception as exc:
        result = {"error": str(exc)}

    outputs.append({
        "type": "function_call_output",
        "call_id": call_id,
        "output": json.dumps(result),
    })

A failed tool now has an explicit error result rather than disappearing from the batch. Before submitting these outputs, check that each requested call ID appears exactly once; the next step sends them back to the model.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Return outputs and continue the response

Send the outputs you assembled in the previous step as the next input, and link the request to the response that produced the tool calls. Each output already carries its matching call_id.

next_response = client.responses.create(
    model=model_name,
    previous_response_id=response.id,
    input=outputs,
)

Check next_response.output for new function_call items. If it contains any, parse and execute them using the same safety checks, thread-pool mapping, and output-building steps; then submit those outputs with previous_response_id=next_response.id.

The model may need another tool round before it returns a response without function calls.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Verify the completed tool round

Before sending the batch, compare the IDs from the original function_call items with those in outputs. A set comparison catches missing or unexpected IDs; a Counter also catches duplicates, so each call must have exactly one matching output.

from collections import Counter

expected = [call.call_id for call in calls]
submitted = [item["call_id"] for item in outputs]
assert Counter(expected) == Counter(submitted)

Then confirm the follow-up request uses previous_response_id=response.id. Inspect its returned output for any new function_call items; if there are any, handle them as another tool round.

This check verifies the submitted batch’s ID coverage, not whether the tool’s result is correct or a side effect succeeded.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Troubleshoot failures and unsafe parallel work

If the API request itself fails, distinguish that from an exception raised by a tool function: the OpenAI Python SDK documents APIConnectionError for connection problems and APIStatusError for unsuccessful HTTP responses. A tool exception occurs in your application code, so handle it in the future and return an error output with that call’s original call_id, as in the previous step.

The SDK retries certain eligible API errors twice by default, and you can configure max_retries. I would avoid adding another retry layer until you understand how it interacts with those retries and whether repeating the operation is safe.

For side-effecting tools, such as updates or purchases, do not assume concurrent execution or retries are safe. Check the backing service’s concurrency and idempotency requirements; if you need to prevent multiple function calls in a turn, set parallel_tool_calls=False on the Responses API request.

Sources: Async tool calling | OpenAI API; Function calling | OpenAI API; OpenAI Python API library | OpenAI API Reference.

Next steps

Concurrent execution is a good fit when the application-side tool calls are genuinely independent and you want to avoid waiting for each one in sequence. Preserve the call-ID mapping from dispatch through result submission, including when a tool fails, and verify that every original call has exactly one output with the same ID.

If calls update shared state, trigger purchases, or otherwise have side effects, pause before batching or adding retries. Check the backing service’s concurrency and idempotency requirements; when multiple calls should not be returned in a turn, set parallel_tool_calls=False.

Then send the matched outputs with the prior response ID and inspect the next response for another tool round.