Structured Output enables models to return type-safe data conforming to a predefined Schema instead of free-form text — ideal for information extraction, data classification, form filling, and more.
By default, models return free-form text. But in many scenarios, you need structured data — extracting contact information from text, classifying user feedback by sentiment, generating configuration objects in a specific format. deepseek-kit's structured output feature lets you define the expected data structure via a Zod Schema, and the model generates JSON data conforming to that schema, with automatic validation and retry to ensure type-safe results.
Structured output can be used simultaneously with tools. The agent first calls tools through a multi-step loop to gather information, then generates structured output in the final step:
import { createAgent, createModel, tool } from 'deepseek-kit'
import { z } from 'zod'
const model = createModel({ model: 'deepseek-v4-flash' })
const weatherTool = tool({
name: 'getWeather',
description: 'Query weather information for a city',
schema: z.object({
city: z.string().describe('City name'),
}),
execute: async (input) => {
return { city: input.city, temperature: 22, condition: 'Sunny' }
},
})
const agent = createAgent({
model,
tools: [weatherTool],
output: {
schema: z.object({
city: z.string(),
temperature: z.number(),
condition: z.string(),
recommendation: z.string(),
}),
},
})
const result = await agent.generate({
prompt: 'How\'s the weather in Beijing? Do I need an umbrella?',
})
console.log(result.output)
// { city: 'Beijing', temperature: 22, condition: 'Sunny', recommendation: 'No umbrella needed, the weather is clear.' }
Structured output generation consumes an additional step. When combined with tools, make sure maxSteps is large enough to accommodate both tool calls and the final structured output step.
Use .describe() to add descriptions to schema properties, helping the model understand the meaning and expected format of each field, thereby improving generation quality:
If the model outputs invalid JSON, deepseek-kit prompts the model to output valid JSON:
Model output: "Here is the result: { "name": "John" }" ← Contains extra text
Feedback: "Your previous output is not valid JSON. Please output only a valid JSON object..."
If the JSON is valid but doesn't conform to the schema, deepseek-kit formats the Zod validation errors and feeds them back to the model:
Model output: { "rating": 10 } ← Exceeds 1-5 range
Feedback: "Your previous JSON output does not conform to the required schema...
- Field 'rating': Number must be less than or equal to 5"
The default maximum retry count is 3. If the output still doesn't conform to the schema after reaching the limit, an AgentError is thrown:
import { createAgent, createModel } from 'deepseek-kit'
import { z } from 'zod'
const model = createModel({ model: 'deepseek-v4-flash' })
const agent = createAgent({
model,
output: {
schema: z.object({
code: z.string().regex(/^[A-Z]{3}-\d{4}$/, 'Format should be XXX-0000'),
}),
},
})
try {
const result = await agent.generate({
prompt: 'Generate a code.',
})
}
catch (error) {
if (error.type === 'schema_error') {
console.error('Structured output validation failed:', error.message)
}
}
When using the stream() method, the structured output generation process is pushed as stream events. You can get the final text result in the finish event:
const stream = agent.stream({
prompt: 'Extract contact information: Jane, jane@example.com',
})
for await (const event of stream) {
switch (event.type) {
case 'text-delta':
process.stdout.write(event.textDelta)
break
case 'tool-call':
console.log(`\nCalling tool: ${event.toolCalls.map(t => t.function.name).join(', ')}`)
break
case 'step':
console.log(`\nStep ${event.step}`)
break
case 'finish':
console.log('\nDone!')
break
}
}
The structured output generation step also triggers lifecycle hooks. In afterStep, the step type is 'format', which you can use to track the structured output generation process:
Zod Schema that defines the structured output format. The model generates JSON data conforming to this schema, which is validated before being returned.