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

Custom Function Tools — schema 와 shape

~22 min · function-tools, tool-loop

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Custom function tool 은 JSON Schema 의 parameters + 세 필드 — type: "function", name, description. Responses 에선 셋 다 tool object 의 top level. Chat Completions 에선 nested function sub-object 안. Schema body 는 동일.

Migration trip-up 1 위

두 shape 섞으면 silent break. Chat Completions 에서 tools[i].function.name, Responses 에서 tools[i].name. 옮길 때 wrapper 차이를 의식 못하면 호출이 조용히 깨짐.

Description 은 prompt 의 일부

모델은 description 을 읽어. Vague description ('Get the weather') = 모델이 가까운 모든 query 에 호출. Explicit boundary 있는 description ('Get current weather for a city; 24h 너머 forecast 나 "the moon" 같은 non-city 엔 호출 X') = 라우팅 정확.

Per-parameter description 도

parameters.properties[i].description 도 모델이 읽어. units: 'temperature unit, celsius or fahrenheit' 는 OK. units: 'required' 는 X — schema 가 이미 required.

Code

Tool 정의 (Responses shape)·python
import json
from openai import OpenAI

client = OpenAI()

tools = [{
    "type": "function",
    "name": "get_weather",           # top-level, not nested
    "description": "Get current weather for a location.",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {"type": "string", "description": "City name"},
            "units": {"type": ["string", "null"], "enum": ["celsius", "fahrenheit"]},
        },
        "required": ["location"],
        "additionalProperties": False,
    },
    "strict": True,
}]

# Multi-turn tool loop
input_items = [{"role": "user", "content": "What's the weather in Tokyo?"}]

while True:
    response = client.responses.create(
        model="gpt-5.4", tools=tools, input=input_items,
    )
    input_items.extend(response.output)
    tool_calls = [i for i in response.output if i.type == "function_call"]
    if not tool_calls:
        print(response.output_text)
        break
    for tc in tool_calls:
        result = get_weather(**json.loads(tc.arguments))
        input_items.append({
            "type": "function_call_output",
            "call_id": tc.call_id,
            "output": json.dumps(result),
        })

External links

Exercise

function tool 1 개 정의 — get_weather(location, units). 그걸 trigger 하는 쿼리 보내고, function_call output item 파싱, 가짜 handler 가 hardcoded 결과 반환, 그 결과를 input=[function_call_output] 로 다시 보내서 final answer 받기.

Progress

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

댓글 0

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

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