본문 바로가기
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 객체의 최상위에 있고, Chat Completions 에서는 function 하위 객체 안에 있어. parameter schema 자체는 같아.

API 를 옮길 때 wrapper 차이를 확인해

Chat Completions 는 tools[i].function.name, Responses 는 tools[i].name 을 사용해. 두 구조를 섞으면 도구 호출이 깨질 수 있으니 migration 때 가장 먼저 확인해.

description 도 모델이 읽는 지시야

'Get the weather'처럼 모호하게 쓰면 모델이 날씨와 조금만 관련 있어도 도구를 부를 수 있어. '도시의 현재 날씨에만 사용하고 24 시간 이후 예보나 도시가 아닌 장소에는 사용하지 않는다'처럼 사용 범위를 적으면 routing 이 더 정확해져.

parameter 설명에는 의미를 적어

parameters.properties[i].description 도 모델이 읽어. units: 'temperature unit, celsius or fahrenheit' 처럼 값의 의미와 허용 범위를 설명해. 단순히 'required'라고 쓰는 건 schema 가 이미 알려주는 정보를 되풀이할 뿐이야.

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

get_weather(location, units) function tool 을 하나 정의해. 도구를 호출할 질문을 보내 function_call 항목을 parsing 하고, 가짜 handler 가 정해둔 결과를 반환하게 한 뒤 input=[function_call_output] 으로 다시 보내 최종 답변을 받아.

Progress

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

댓글 0

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

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