구조화 출력 — JSON·enum 고정하기

구조화 출력 — JSON·enum 고정하기

Gemini API는 기본적으로 응답을 비정형 텍스트로 돌려줘요. 하지만 다운스트림 작업이 정해진 스키마를 요구한다면, responseSchema를 정의해 출력을 그 스키마에 맞게 고정할 수 있습니다.

출처: https://firebase.google.com/docs/vertex-ai/generate-structured-output

지원 MIME 타입

  • application/json — response schema에 정의된 대로 JSON 출력.
  • text/x.enum — 분류 작업용으로 enum 값 하나를 출력.

지원 스키마 필드

schema는 이런 필드를 지원하며, 지원하지 않는 필드를 쓰면 요청은 처리되되 필드가 무시돼요.

  • enum
  • items
  • maxItems
  • nullable
  • properties
  • required

generatorConfig로 지정

생성 설정에 responseMimeTypeapplication/json으로, responseSchema에 JSON 스키마를 넘겨요.

let model = ai.generativeModel(
    modelName: "gemini-3.5-flash",
    generationConfig: GenerationConfig(
        responseMIMEType: "application/json",
        responseSchema: jsonSchema
    )
)
let prompt = "For use in a children's card game, generate 10 animal-based characters."

더 알아보기

  • https://firebase.google.com/docs/vertex-ai/generate-structured-output — 구조화 출력 가이드
  • https://firebase.google.com/docs/vertex-ai/function-calling — 함수 호출로 더 강한 연결