GrepBoard/매뉴얼
51 편 ·

Gemini 답 형식을 시스템 지시와 예시로 고정하기

·Gemini API 문서 기준 · 2026-10·7 단계

같은 질문에 Gemini 의 답 형식이 매번 달라질 때, 시스템 지시에 규칙을 쓰고 예시 2~3개와 Input:/Output: 접두사를 붙여 형식을 맞추는 방법입니다.

답 형식이 흔들리는 프롬프트를 시스템 지시 · 퓨샷 예시 · 접두사 순서로 고칩니다. 예제는 고객 문의를 카테고리 | 긴급도 | 한 줄 요약 으로 분류하는 작업입니다.

[1] 먼저 흔들리는 프롬프트를 확인합니다. 아래처럼 규칙 없이 부탁만 하면, 형식을 정한 곳이 없어 답의 모양이 실행마다 달라질 수 있습니다.

text
Classify this customer message. Message: I was charged twice for my March invoice and nobody answers my emails.

[2] 역할과 형식 규칙은 시스템 지시에 둡니다. 공식 문서는 system_instruction 파라미터로 모델의 동작을 설정한다고 설명합니다. 공식 예시는 "You are a cat. Your name is Neko." 한 줄입니다(문서). 아래는 이 작업용 시스템 지시입니다.

text
You are a support-ticket triage assistant.For every message, reply with exactly one line in this format:<category> | <urgency> | <one-sentence summary> Rules:- category is one of: billing, bug, account, other- urgency is one of: low, medium, high- the summary is one sentence, 15 words or fewer- output nothing else: no greeting, no explanation, no markdown

[3] 규칙만으로 부족하면 예시를 붙입니다. 같은 문서가 「프롬프트에 퓨샷 예시를 항상 넣으라」(We recommend to always include few-shot examples in your prompts)고 권합니다. 예시는 입력과 출력의 짝이며, 규칙에 쓴 형식을 그대로 보여 줍니다. 2~3개로 시작합니다.

text
Message: The app crashes every time I open the settings page.Output: bug | medium | App crashes when the settings page is opened. Message: Please delete my account and all my data today.Output: account | high | Customer asks for immediate account and data deletion. Message: Do you have a student discount?Output: other | low | Customer asks whether a student discount exists.

[4] 예시끼리 구조를 똑같이 맞춥니다. 문서는 XML 태그 · 공백 · 줄바꿈 · 예시 구분자까지 모든 예시에서 같아야 원치 않는 형식의 응답을 피한다고 설명합니다. 위 예시는 세 개 모두 Message: 줄, Output: 줄, 빈 줄 순서입니다. 한 예시만 Answer: 를 쓰지 않습니다.

[5] 접두사로 답의 시작을 이끕니다. 문서는 예시와 응답 접두사(예: Output:)를 주고 모델이 패턴을 이어 쓰게 하는 방식을 설명합니다. 실제 입력은 예시와 같은 모양으로 쓰고 마지막 Output: 뒤를 비워 둡니다.

text
Message: I was charged twice for my March invoice and nobody answers my emails.Output:

요청은 이렇게 나눕니다. 시스템 지시는 system_instruction 으로, 3단계의 예시 세 개와 위 마지막 입력은 이어 붙여 사용자 입력(input)으로 보냅니다.

[6] 코드가 답을 읽어야 하면 문자열 규칙 대신 응답 스키마를 씁니다. 문서는 복잡한 JSON 스키마에는 구조화 출력 기능을 쓰라고 권합니다. 이 기능은 JSON 스키마에 맞는 응답을 내도록 설정하며, 추출하기 쉬운 형태를 보장하려는 것입니다(Structured output). 문서의 Recipe Extractor 예제는 response_format 에 mime_type 과 schema 를 넣습니다.

extract.py
from google import genaifrom pydantic import BaseModel, Field class Ticket(BaseModel):    category: str = Field(description="One of: billing, bug, account, other")    urgency: str = Field(description="One of: low, medium, high")    summary: str = Field(description="One sentence, 15 words or fewer") client = genai.Client()interaction = client.interactions.create(    model=MODEL,    input=prompt,    response_format={        "type": "text",        "mime_type": "application/json",        "schema": Ticket.model_json_schema()    },)ticket = Ticket.model_validate_json(interaction.output_text)

[7] 형식이 고정됐는지는 같은 프롬프트를 여러 번 돌려 확인합니다. 아래는 응답을 한 줄씩 out-1.txt ~ out-10.txt 로 저장했다고 가정하고, 형식에 맞지 않는 줄을 찾는 검사입니다.

console
$ grep -L -E '^(billing|bug|account|other) \| (low|medium|high) \| .+$' out-*.txt$ wc -l out-*.txt

grep -L 은 형식에 맞는 줄이 없는 파일 이름만 출력합니다. 아무것도 안 나오면 열 번 모두 형식을 지킨 것입니다. 걸린 파일은 열어서 어긋난 모양을 보고 규칙 줄이나 예시를 고칩니다.

#gemini#prompting