구조화된 출력과 함께하는 도구 호출

구조화된 출력과 함께하는 도구 호출 (Tool calling with structured outputs)

구조화된 출력(structured outputs) 생성과 도구 호출을 함께 쓰고 싶을 때 필요한 주의점을 정리한 문서예요. output 옵션을 도구 호출과 함께 쓰면 실행 흐름에 추가 스텝이 하나 더 생긴다는 점을 꼭 기억해야 해요. stopWhen 조건을 그만큼 여유 있게 잡아주는 게 핵심이에요.

출처: 문서

본문

문제 (Issue)

도구 호출과 구조화된 출력 생성을 결합하고 싶을 수 있습니다.

배경 (Background)

구조화된 출력과 함께 도구 호출을 사용하려면, output 옵션과 함께 generateText 또는 streamText를 사용하세요.

중요: 도구 호출과 함께 output을 사용하면 구조화된 출력 생성이 실행 흐름에서 하나의 추가 스텝(step) 으로 계산됩니다.

해결 방법 (Solution)

output을 도구 호출과 함께 사용할 때, 구조화된 출력 생성에 필요한 추가 스텝을 반영하도록 stopWhen 조건을 조정하세요:

const result = await generateText({
  model: __MODEL__,
  output: Output.object({
    schema: z.object({
      summary: z.string(),
      sentiment: z.enum(['positive', 'neutral', 'negative']),
    }),
  }),
  tools: {
    analyze: tool({
      description: 'Analyze data',
      inputSchema: z.object({
        data: z.string(),
      }),
      execute: async ({ data }) => {
        return { result: 'analyzed' };
      }),
    },
  },
  // Add at least 1 to your intended step count to account for structured output
  stopWhen: isStepCount(3), // Now accounts for: tool call + tool result + structured output
  prompt: 'Analyze the data and provide a summary',
});

generateText와 streamText에서 구조화된 출력을 사용하는 방법에 대한 자세한 내용은 구조화된 데이터 생성을 참고하세요.

더 알아보기 (Learn more)

전체 사이트맵