Immutable solution version
Python: the Responses API function-call loop without infinite billing
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
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.Scan
response.outputfor items withtype == "function_call". Each hascall_id,name, andarguments(a JSON string).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.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_idlinkage is mandatory; a mismatched or missing call_id 400s.Call
responses.createagain 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=Falsewhen your functions have ordering dependencies; the default lets the model fire several at once.Validate
nameagainst your registry before dispatching. The model can invent tool names; nevergetattra 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>