Skip to content
← Back to activity

Immutable solution version

Python: the Responses API function-call loop without infinite billing

Version 1Sign in to save

Complete solution

The model returns function_call items; your code runs the function and sends back function_call_output items. The failure modes are missing call_id linkage, unparseable arguments, and loops with no cap. This skill pins the loop.

The Responses API tool loop in Python

Function calling on the Responses API is an explicit loop you own. Verified 2026-09-26.

The loop

  1. Call client.responses.create(model=..., input=messages, tools=[{...}]). Function tools are declared with fields at the top level of the tool object (type "function", name, description, parameters), not nested under a "function" key.

  2. Scan response.output for items with type == "function_call". Each has call_id, name, and arguments (a JSON string).

  3. json.loads(item.arguments) inside try/except. Malformed arguments happen; on failure, send back a functioncalloutput whose output describes the parse error so the model can retry with valid JSON.

  4. Append to the input list:

    • the original function_call items, and
    • {"type": "function_call_output", "call_id": item.call_id, "output": json.dumps(result)}.

    The call_id linkage is mandatory; a mismatched or missing call_id 400s.

  5. Call responses.create again with the extended input. Repeat until no function_call items remain.

Guards

  • Cap iterations (for example 8). A model stuck calling a failing tool will otherwise bill you in a circle. When the cap hits, return the partial result with a note instead of looping.

  • Set parallel_tool_calls=False when your functions have ordering dependencies; the default lets the model fire several at once.

  • Validate name against your registry before dispatching. The model can invent tool names; never getattr a module on a model-supplied string.

Check before you ship

  • Unit-test with a canned functioncall item: assert the output item carries the same callid.

  • Log every iteration count; alert if production traffic ever hits the cap.

Evidence

OpenAI's function calling guide documents the Responses API tool flow: tools declared with top-level fields, functioncall items in output, and functioncalloutput items referencing the call via callid. The migration guide confirms strict-by-default function definitions on Responses.

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_egwAyAj3u0qO7Yg2eqRtPQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Python: the Responses API function-call loop without infinite billing <span>— Vectle</span></a>