Gemini API finishReason RECITATION 해결
Gemini API finishReason RECITATION 은 출력이 기존 데이터와 닮았을 수 있어 멈췄다는 뜻입니다. 프롬프트·컨텍스트를 고유하게 바꾸고 citationMetadata 로 겹친 구간을 찾습니다.
{"candidates": [{"finishReason": "RECITATION", "index": 0}]}RECITATION 은 모델 출력이 특정 데이터와 닮았을 수 있다는 플래그로 생성이 멈췄다는 뜻이고(실제로 어느 출처와 일치한다는 판정은 아닙니다), 공식 해법은 프롬프트와 컨텍스트를 최대한 고유하게 바꾸는 것입니다. 안전 필터(SAFETY)와 달리 임계값을 낮춰 푸는 설정이 없습니다.
[1] 어느 API 의 응답인지 먼저 봅니다. 같은 차단이 API 에 따라 다른 자리에 옵니다.
| 받은 것 | API | 문서의 설명 |
|---|---|---|
candidates[0].finishReason = RECITATION | generateContent | The response candidate content was flagged for recitation reasons. |
candidates[0].finishReason = IMAGE_RECITATION | generateContent | Image generation stopped due to recitation. |
error.code = recitation | Interactions API | Copyright or recitation restrictions blocked the request. |
error.code = image_recitation | Interactions API | Copyright or recitation restrictions blocked image generation. |
오류 문서는 이 코드들을 「Generation blocked codes」로 묶고 "modify your input and retry" 라고 적습니다. 같은 입력을 다시 보내는 것은 해법이 아닙니다. finishMessage 가 있으면 중단 사유가 더 자세히 적혀 있으니 같이 로그에 남깁니다.
[2] 응답에 남은 글과 citationMetadata 를 꺼냅니다. 레퍼런스는 이 필드에 content 안 텍스트의 recitation 정보가 들어갈 수 있고, 그것이 기반 모델 학습 데이터 속 저작물에서 "recited" 된 구간이라고 설명합니다. 출처마다 startIndex·endIndex·uri·license 가 붙고, 코드 인용에는 라이선스가 필수입니다.
import json, sys def recitation_check(resp): err = resp.get("error") if err: return f"error code={err.get('code')}" cands = resp.get("candidates") or [] if not cands: return "no candidates" c = cands[0] fr = c.get("finishReason") parts = (c.get("content") or {}).get("parts") or [] text = "".join(p.get("text", "") for p in parts if not p.get("thought")) srcs = (c.get("citationMetadata") or {}).get("citationSources") or [] lines = [f"finishReason={fr} text={len(text)} chars citations={len(srcs)}"] raw = text.encode("utf-8") for s in srcs: a, b = s.get("startIndex", 0), s.get("endIndex", len(raw)) seg = raw[a:b].decode("utf-8", errors="replace") lines.append(f" bytes {a}-{b} uri={s.get('uri')} license={s.get('license') or '-'}") lines.append(f" segment: {seg!r}") return "\n".join(lines) for path in sys.argv[1:]: print(f"{path}: {recitation_check(json.load(open(path, encoding='utf-8')))}")레퍼런스 필드 이름에 맞춰 만든 샘플 JSON 여섯 개로 돌린 결과입니다.
$ python3 -I ../recitation_check.py a-recitation.json b-partial.json c-partial-ko.json d-code.json e-image.json f-interactions.jsona-recitation.json: finishReason=RECITATION text=0 chars citations=0b-partial.json: finishReason=RECITATION text=47 chars citations=1 bytes 33-47 uri=https://example.com/poem license=- segment: 'Roses are red,'c-partial-ko.json: finishReason=RECITATION text=17 chars citations=1 bytes 25-41 uri=https://example.com/poem license=- segment: '장미는 붉고'd-code.json: finishReason=STOP text=31 chars citations=1 bytes 0-31 uri=https://github.com/example/repo license=mit segment: 'def add(a, b):\n return a + b'e-image.json: finishReason=IMAGE_RECITATION text=0 chars citations=0f-interactions.json: error code=recitationd-code 처럼 STOP 으로 정상 종료한 응답에도 인용 정보가 붙을 수 있습니다. 차단이 아니어도 license 가 있으면 그 코드의 라이선스 조건을 따로 확인합니다.
[3] 인덱스는 바이트로 자릅니다. 레퍼런스는 startIndex 를 "measured in bytes" 라고 적습니다. 한글처럼 글자당 여러 바이트인 텍스트를 문자 인덱스로 자르면 엉뚱한 구간이 나옵니다.
$ python3 -I -c 'import json;t=json.load(open("c-partial-ko.json"))["candidates"][0]["content"]["parts"][0]["text"];print(repr(t[25:41]));print(repr(t.encode()[25:41].decode()))''''장미는 붉고'문자열 t[25:41] 은 빈 문자열이고, UTF-8 로 인코딩한 뒤 자른 쪽이 표시된 구간입니다.
[4] 프롬프트와 컨텍스트를 고유하게 바꿉니다. 문제 해결 문서의 문장은 이것뿐입니다: "try to make prompt / context as unique as possible." 겹친 구간을 [2]에서 찾았다면 그 부분을 원문 그대로 다시 만들도록 요구하는 지시가 있는지 봅니다.
[5] 샘플링 매개변수를 받는 이전 모델이라면 temperature 를 올려 봅니다. 문서는 "For earlier models that support sampling parameters, you can also use a higher temperature." 라고 적고, 레퍼런스의 값 범위는 [0.0, 2.0] 입니다. 이 값을 무시하는 모델에서는 효과가 없습니다. 어느 모델이 무시하는지는 Gemini 4 temperature·top_p 가 무시될 때를 봅니다.
[6] 안전 설정으로는 풀리지 않습니다. 안전 설정 문서가 조정 가능하다고 적은 범주는 harassment·hate_speech·sexually_explicit·dangerous_content·jailbreak 이고, recitation 은 없습니다. 임계값을 off 로 바꿔도 이 차단은 그대로라는 점을 기억하고 입력을 바꿉니다. 같은 응답의 finishReason 이 SAFETY 이거나 텍스트가 비어 있는 다른 경우는 Gemini 응답 text 가 비었을 때 finishReason 확인하기에 정리돼 있습니다.