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 call은 output item이야

Responses API에서 function calling은 response output item을 돌리는 loop야. JSON Schema 기반 function definition을 tools로 주면, 모델이 function_call item을 낸다. 애플리케이션은 그걸 실행하고 같은 call_idfunction_call_output item을 다시 넣어준다.

실전에서 중요한 최신 디테일은 run에 아직 필요한 response output item을 보존하는 거야. tool-call state를 날려놓고 두 번째 호출이 기억상실처럼 군다고 놀라면 안 돼. 작은 디테일인데 버그밭이야.

Strict schema가 애매함을 줄인다

도구 계약이 명확하면 strict: true, additionalProperties: false를 써. 모델의 출력 공간이 줄어들고 executor validation도 쉬워진다. parameter description은 장식이 아니라 모델이 보는 action surface의 일부야.

모델 선택도 run contract야

현재 OpenAI 문서는 복잡한 reasoning/coding의 기본 출발점으로 gpt-5.5를 권하고, latency와 cost가 더 중요하면 gpt-5.4-minigpt-5.4-nano 같은 작은 variant를 고르라고 해. 옛 예제의 model string을 복붙하지 마. 왜 그 모델을 쓰는지 적어야 한다.

Output은 machine-readable하게

작고 안정적인 field를 가진 JSON string을 돌려줘. raw API dump는 피하자. 더 자세한 정보가 필요하면 모든 걸 tool result에 욱여넣지 말고 read_full_recordfetch_url 같은 next-step hint를 줘.

Code

Responses API function calling 구조·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

search_notes(query, max_results)용 OpenAI function tool schema를 설계해봐. description, required fields, additionalProperties: false, 그리고 그 run에서 왜 그 모델을 골랐는지 한 문장으로 포함해.
Hint
description에는 언제 쓰는지뿐 아니라 언제 쓰면 안 되는지도 적어야 해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.