C.W.K.
Stream
Lesson 02 of 05 · published

OpenAI Responses API: Function Calling Loop

~34 min · openai, responses-api, function-calling

Level 0Observer
0 XP0/40 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

OpenAI tool calls are output items

With the Responses API, function calling is a loop over response output items. You provide JSON Schema-backed function definitions. The model emits function_call items. Your application executes those calls and sends function_call_output items back with the matching call_id.

The modern detail that matters in real systems: preserve the response output items that are still part of the run. Do not throw away tool-call state and then wonder why the second call feels amnesiac. Tiny detail, huge bug farm.

Strict schemas reduce ambiguity

Use strict: true and additionalProperties: false when the tool has a known contract. This narrows the model's output space and makes executor validation simpler. Parameter descriptions are not decorative; they are part of the action surface the model sees.

Model choice is part of the run contract

Current OpenAI docs recommend gpt-5.5 as the default starting point for complex reasoning and coding, with smaller variants such as gpt-5.4-mini or gpt-5.4-nano when latency and cost dominate. Do not cargo-cult a model string from an old example. Write down why the run uses that model.

Outputs should be machine-readable

Return small JSON strings with stable fields. Avoid raw API dumps. If the model needs more detail, include a next-step hint like read_full_record or fetch_url instead of stuffing everything into one result.

Code

Responses API function calling shape·python
from openai import OpenAI
import json

client = OpenAI()

tools = [{
    "type": "function",
    "name": "lookup_order",
    "description": "Look up one order by id. Use when the user asks about order status.",
    "parameters": {
        "type": "object",
        "properties": {
            "order_id": {"type": "string", "description": "Public order id, e.g. ORD-1042"}
        },
        "required": ["order_id"],
        "additionalProperties": False,
    },
    "strict": True,
}]

input_items = [{"role": "user", "content": "Where is ORD-1042?"}]

while True:
    response = client.responses.create(
        model="gpt-5.5",
        input=input_items,
        tools=tools,
    )

    calls = [item for item in response.output if item.type == "function_call"]
    if not calls:
        print(response.output_text)
        break

    input_items += response.output
    for call in calls:
        args = json.loads(call.arguments)
        result = {"order_id": args["order_id"], "status": "shipped"}
        input_items.append({
            "type": "function_call_output",
            "call_id": call.call_id,
            "output": json.dumps(result),
        })

External links

Exercise

Design an OpenAI function tool for search_notes(query, max_results). Include descriptions, required fields, additionalProperties: false, and a one-sentence note explaining why you chose the model for the run.
Hint
The description should say when to use the tool and when not to use it.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.