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

Google Gemini Function Declaration

~22 min · gemini, google, function-declaration, automatic-fc

Level 0호기심 많은 독자
0 XP0/48 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Google Gemini 는 tool calling 을 살짝 더 OOP 한 envelope 로 감싸. Tool 은 Tool 안에 묶인 FunctionDeclaration object 들, response 는 nameargs (real dict, Anthropic 처럼) 들고 있는 function_call part 를 carry.

google-genai 의 unique feature 는 automatic function calling: loop 직접 짜는 대신 SDK 에 진짜 Python function 넘기면 자동 실행하고 결과 넣어줌. Prototyping 엔 편하고 production 엔 제한적 — error handling, parallelism, audit trail control 을 잃어. Loop 느껴보는 용으로 쓰고, ship 할 땐 직접 짜.

Gemini 도 function-calling 모드 노출: AUTO (default), ANY (뭐든 호출 must), NONE (호출 금지). OpenAI tool_choice 와 같은 세 semantic, enum 으로. 모드는 GenerationConfig 안에 살아 — top-level parameter 아님.

Streaming 은 generate_content_stream 으로 자라는 partsCandidate object yield. Function-call part 는 모델 결정할 때 한 번에 도착 — Gemini 는 OpenAI 처럼 argument delta 를 stream 안 함. Tool-call streaming 소비가 더 쉬워지지만 first byte 는 살짝 느려.

Code

google-genai 로 manual loop·python
from google import genai
from google.genai import types

client = genai.Client()

def get_weather(location: str) -> str:
    return f"68°F and clear in {location}"

tools = [types.Tool(function_declarations=[types.FunctionDeclaration(
    name="get_weather",
    description="Get current weather for a city.",
    parameters=types.Schema(
        type=types.Type.OBJECT,
        properties={"location": types.Schema(type=types.Type.STRING)},
        required=["location"],
    ),
)])]

contents = [types.Content(role="user", parts=[types.Part(text="Weather in Seoul?")])]
while True:
    resp = client.models.generate_content(
        model="gemini-2.5-flash",
        contents=contents,
        config=types.GenerateContentConfig(tools=tools),
    )
    contents.append(resp.candidates[0].content)
    fc = next((p.function_call for p in resp.candidates[0].content.parts if p.function_call), None)
    if not fc:
        print(resp.text)
        break
    result = get_weather(**dict(fc.args))
    contents.append(types.Content(role="user", parts=[types.Part(
        function_response=types.FunctionResponse(name=fc.name, response={"result": result})
    )]))
Automatic function calling — 편의 모드·python
# 진짜 Python function 넘기면 SDK 가 loop 처리.
resp = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Weather in Seoul?",
    config=types.GenerateContentConfig(tools=[get_weather]),
)
print(resp.text)

External links

Exercise

Tool 하나로 같은 prompt 를 두 모드 — automatic function calling 과 manual loop — 로 돌려. Auto path 에서 잃는 control 적어: error handling, parallelism, intermediate logging. 그 gap 이 정확히 loop 직접 짤 때 지키는 surface area 야.

Progress

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

댓글 0

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

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