🤖 Two-stage AI calls for structured output
Unless latency is critical, you should use 2 AI calls to generate structured output.
- First, generate the raw text or reasoning.
- Then, structure or format it in a second call.
The benefit is that you can let the AI generate step-by-step reasoning and analysis to solve the problem without forcing the solution into a strict JSON schema. It can focus on the reasoning itself, not the format.
If you add a key like reasoning_steps to your JSON, you're still confining the reasoning to a rigid structure. That structure doesn't improve the quality of the reasoning, and focusing on it might get in the way.
Example Implementation
// Step 1: Generate raw analysis with detailed reasoning
const analysis = await generateText({
model: ANALYSIS_MODEL,
providerOptions: {
openai: {
reasoningEffort: 'high',
}
},
system: analysisSystemMessage,
prompt: analysisPrompt,
});
// Step 2: Structure the raw analysis into desired format
const structuredResponse = await convertTextToJson(
analysis.content,
ResponseSchema
);
The key difference is in the prompt structure:
Analysis Prompt (Step 1): Focuses purely on thinking and reasoning without format constraints. Encourages comprehensive analysis, exploration of multiple angles, and thorough examination of the problem.
Structuring Prompt (Step 2): Takes the raw analysis and formats it according to specific schema requirements, extracting key insights and organizing them into the desired structure.
© nem035RSS