@mastra/mcp-docs-server 1.0.0-beta.6 → 1.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +47 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +196 -196
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Flance.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +40 -40
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +43 -43
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +52 -52
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Freact.md +14 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
- package/.docs/organized/changelogs/create-mastra.md +7 -7
- package/.docs/organized/changelogs/mastra.md +14 -14
- package/.docs/organized/code-examples/ai-elements.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-useChat.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-v5.md +1 -1
- package/.docs/organized/code-examples/assistant-ui.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +1 -1
- package/.docs/organized/code-examples/crypto-chatbot.md +1 -1
- package/.docs/organized/code-examples/mcp-server-adapters.md +2 -2
- package/.docs/organized/code-examples/server-app-access.md +2 -2
- package/.docs/organized/code-examples/server-express-adapter.md +87 -0
- package/.docs/organized/code-examples/server-hono-adapter.md +85 -0
- package/.docs/raw/agents/overview.mdx +2 -111
- package/.docs/raw/agents/processors.mdx +1 -1
- package/.docs/raw/agents/structured-output.mdx +224 -0
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +381 -431
- package/.docs/raw/guides/getting-started/quickstart.mdx +11 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +40 -1
- package/.docs/raw/memory/working-memory.mdx +1 -0
- package/.docs/raw/observability/tracing/bridges/otel.mdx +25 -1
- package/.docs/raw/observability/tracing/exporters/arize.mdx +19 -0
- package/.docs/raw/observability/tracing/exporters/langfuse.mdx +63 -0
- package/.docs/raw/observability/tracing/exporters/otel.mdx +30 -19
- package/.docs/raw/observability/tracing/exporters/posthog.mdx +20 -0
- package/.docs/raw/observability/tracing/overview.mdx +6 -1
- package/.docs/raw/reference/ai-sdk/chat-route.mdx +127 -0
- package/.docs/raw/reference/ai-sdk/handle-chat-stream.mdx +117 -0
- package/.docs/raw/reference/ai-sdk/handle-network-stream.mdx +64 -0
- package/.docs/raw/reference/ai-sdk/handle-workflow-stream.mdx +116 -0
- package/.docs/raw/reference/ai-sdk/network-route.mdx +99 -0
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-stream.mdx +289 -0
- package/.docs/raw/reference/ai-sdk/workflow-route.mdx +110 -0
- package/.docs/raw/reference/observability/tracing/bridges/otel.mdx +27 -0
- package/.docs/raw/reference/observability/tracing/exporters/arize.mdx +25 -0
- package/.docs/raw/reference/observability/tracing/exporters/langfuse.mdx +43 -0
- package/.docs/raw/reference/observability/tracing/exporters/otel.mdx +27 -43
- package/.docs/raw/reference/server/create-route.mdx +314 -0
- package/.docs/raw/reference/server/express-adapter.mdx +193 -0
- package/.docs/raw/reference/server/hono-adapter.mdx +174 -0
- package/.docs/raw/reference/server/mastra-server.mdx +316 -0
- package/.docs/raw/reference/server/routes.mdx +250 -0
- package/.docs/raw/reference/streaming/workflows/timeTravelStream.mdx +170 -0
- package/.docs/raw/reference/tools/mcp-client.mdx +54 -1
- package/.docs/raw/reference/workflows/run-methods/timeTravel.mdx +310 -0
- package/.docs/raw/reference/workflows/run.mdx +14 -0
- package/.docs/raw/server-db/custom-adapters.mdx +380 -0
- package/.docs/raw/server-db/custom-api-routes.mdx +5 -5
- package/.docs/raw/server-db/mastra-server.mdx +11 -32
- package/.docs/raw/server-db/server-adapters.mdx +286 -0
- package/.docs/raw/workflows/workflow-state.mdx +4 -5
- package/CHANGELOG.md +15 -0
- package/package.json +4 -4
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
### package.json
|
|
2
|
+
```json
|
|
3
|
+
{
|
|
4
|
+
"name": "examples-server-hono-adapter",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@ai-sdk/openai": "latest",
|
|
7
|
+
"@mastra/core": "latest",
|
|
8
|
+
"@mastra/hono": "latest",
|
|
9
|
+
"@hono/node-server": "^1.14.3",
|
|
10
|
+
"hono": "^4.10.4",
|
|
11
|
+
"zod": "^3.25.76"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "22.13.17",
|
|
15
|
+
"tsx": "^4.19.3",
|
|
16
|
+
"typescript": "^5.8.3"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### mastra/agents/assistant.ts
|
|
22
|
+
```typescript
|
|
23
|
+
import { Agent } from '@mastra/core/agent';
|
|
24
|
+
import { openai } from '@ai-sdk/openai';
|
|
25
|
+
|
|
26
|
+
export const assistantAgent = new Agent({
|
|
27
|
+
id: 'assistantAgent',
|
|
28
|
+
name: 'Assistant',
|
|
29
|
+
instructions: 'You are a helpful assistant. Answer questions concisely.',
|
|
30
|
+
model: openai('gpt-4o-mini'),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### mastra/index.ts
|
|
36
|
+
```typescript
|
|
37
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
38
|
+
|
|
39
|
+
import { assistantAgent } from './agents/assistant';
|
|
40
|
+
|
|
41
|
+
export const mastra = new Mastra({
|
|
42
|
+
agents: { assistantAgent },
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### server.ts
|
|
48
|
+
```typescript
|
|
49
|
+
/**
|
|
50
|
+
* Hono Server Adapter Example
|
|
51
|
+
*
|
|
52
|
+
* This demonstrates how to use @mastra/hono to run Mastra with Hono.
|
|
53
|
+
*
|
|
54
|
+
* Features shown:
|
|
55
|
+
* - Basic server setup with MastraServer
|
|
56
|
+
* - Custom routes added after init()
|
|
57
|
+
* - Accessing Mastra context in custom routes
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
import { serve } from '@hono/node-server';
|
|
61
|
+
import { Hono } from 'hono';
|
|
62
|
+
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono';
|
|
63
|
+
|
|
64
|
+
import { mastra } from './mastra';
|
|
65
|
+
|
|
66
|
+
const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>();
|
|
67
|
+
const server = new MastraServer({ app: app as any, mastra });
|
|
68
|
+
|
|
69
|
+
await server.init();
|
|
70
|
+
|
|
71
|
+
// Custom route with access to Mastra context
|
|
72
|
+
app.get('/health', c => {
|
|
73
|
+
const mastraInstance = c.get('mastra');
|
|
74
|
+
const agents = Object.keys(mastraInstance.listAgents());
|
|
75
|
+
return c.json({ status: 'ok', agents });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const port = 4111;
|
|
79
|
+
|
|
80
|
+
serve({ fetch: app.fetch, port }, () => {
|
|
81
|
+
console.log(`Server running on http://localhost:${port}`);
|
|
82
|
+
console.log(`Try: curl http://localhost:${port}/api/agents`);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
```
|
|
@@ -212,118 +212,9 @@ for await (const chunk of stream.textStream) {
|
|
|
212
212
|
|
|
213
213
|
## Structured output
|
|
214
214
|
|
|
215
|
-
Agents can return structured, type-safe data
|
|
215
|
+
Agents can return structured, type-safe data using Zod or JSON Schema. The parsed result is available on `response.object`.
|
|
216
216
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
Define the `output` shape using [Zod](https://zod.dev/):
|
|
220
|
-
|
|
221
|
-
```typescript showLineNumbers copy
|
|
222
|
-
import { z } from "zod";
|
|
223
|
-
|
|
224
|
-
const response = await testAgent.generate(
|
|
225
|
-
[
|
|
226
|
-
{
|
|
227
|
-
role: "system",
|
|
228
|
-
content: "Provide a summary and keywords for the following text:",
|
|
229
|
-
},
|
|
230
|
-
{
|
|
231
|
-
role: "user",
|
|
232
|
-
content: "Monkey, Ice Cream, Boat",
|
|
233
|
-
},
|
|
234
|
-
],
|
|
235
|
-
{
|
|
236
|
-
structuredOutput: {
|
|
237
|
-
schema: z.object({
|
|
238
|
-
summary: z.string(),
|
|
239
|
-
keywords: z.array(z.string()),
|
|
240
|
-
}),
|
|
241
|
-
},
|
|
242
|
-
},
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
console.log(response.object);
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
### Structuring sub agent
|
|
249
|
-
|
|
250
|
-
Use the `model` property to have a separate agent generate the structured output for you.
|
|
251
|
-
|
|
252
|
-
```typescript showLineNumbers copy
|
|
253
|
-
import { z } from "zod";
|
|
254
|
-
|
|
255
|
-
const response = await testAgentWithTools.generate(
|
|
256
|
-
[
|
|
257
|
-
{
|
|
258
|
-
role: "system",
|
|
259
|
-
content: "Provide a summary and keywords for the following text:",
|
|
260
|
-
},
|
|
261
|
-
{
|
|
262
|
-
role: "user",
|
|
263
|
-
content: "Please use your test tool and let me know the results",
|
|
264
|
-
},
|
|
265
|
-
],
|
|
266
|
-
{
|
|
267
|
-
structuredOutput: {
|
|
268
|
-
schema: z.object({
|
|
269
|
-
summary: z.string(),
|
|
270
|
-
keywords: z.array(z.string()),
|
|
271
|
-
}),
|
|
272
|
-
model: "openai/gpt-5.1",
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
);
|
|
276
|
-
|
|
277
|
-
console.log(response.object);
|
|
278
|
-
console.log(response.toolResults);
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
### Response format
|
|
282
|
-
|
|
283
|
-
By default `structuredOutput` will use `response_format` to pass the schema to the model provider. If the model provider does not natively support `response_format` it's possible that this will error or not give the desired results. To keep using the same model use `jsonPromptInjection` to bypass response format and inject a system prompt message to coerce the model to return structured output.
|
|
284
|
-
|
|
285
|
-
```typescript showLineNumbers copy
|
|
286
|
-
import { z } from "zod";
|
|
287
|
-
|
|
288
|
-
const response = await testAgentThatDoesntSupportStructuredOutput.generate(
|
|
289
|
-
[
|
|
290
|
-
{
|
|
291
|
-
role: "system",
|
|
292
|
-
content: "Provide a summary and keywords for the following text:",
|
|
293
|
-
},
|
|
294
|
-
{
|
|
295
|
-
role: "user",
|
|
296
|
-
content: "Monkey, Ice Cream, Boat",
|
|
297
|
-
},
|
|
298
|
-
],
|
|
299
|
-
{
|
|
300
|
-
structuredOutput: {
|
|
301
|
-
schema: z.object({
|
|
302
|
-
summary: z.string(),
|
|
303
|
-
keywords: z.array(z.string()),
|
|
304
|
-
}),
|
|
305
|
-
jsonPromptInjection: true,
|
|
306
|
-
},
|
|
307
|
-
},
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
console.log(response.object);
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
:::info[Gemini 2.5 with tools]
|
|
314
|
-
|
|
315
|
-
Gemini 2.5 models do not support combining `response_format` (structured output) with function calling (tools) in the same API call. If your agent has tools and you're using `structuredOutput` with a Gemini 2.5 model, you must set `jsonPromptInjection: true` to avoid the error `Function calling with a response mime type: 'application/json' is unsupported`.
|
|
316
|
-
|
|
317
|
-
```typescript
|
|
318
|
-
const response = await agentWithTools.generate("Your prompt", {
|
|
319
|
-
structuredOutput: {
|
|
320
|
-
schema: yourSchema,
|
|
321
|
-
jsonPromptInjection: true, // Required for Gemini 2.5 when tools are present
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
:::
|
|
217
|
+
> See [Structured Output](/docs/v1/agents/structured-output) for more information.
|
|
327
218
|
|
|
328
219
|
## Analyzing images
|
|
329
220
|
|
|
@@ -276,4 +276,4 @@ const agent = new Agent({
|
|
|
276
276
|
## Related documentation
|
|
277
277
|
|
|
278
278
|
- [Guardrails](/docs/v1/agents/guardrails) - Security and validation processors
|
|
279
|
-
- [Memory Processors](/docs/v1/memory/memory-processors) - Memory-specific processors and automatic integration
|
|
279
|
+
- [Memory Processors](/docs/v1/memory/memory-processors) - Memory-specific processors and automatic integration
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Structured Output | Agents"
|
|
3
|
+
description: "Learn how to generate structured data from agents using schemas and validation."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Tabs from "@theme/Tabs";
|
|
7
|
+
import TabItem from "@theme/TabItem";
|
|
8
|
+
|
|
9
|
+
# Structured Output
|
|
10
|
+
|
|
11
|
+
Structured output lets an agent return an object that matches the shape defined by a schema instead of returning text. The schema tells the model what fields to produce, and the model ensures the final result fits that shape.
|
|
12
|
+
|
|
13
|
+
## When to use structured output
|
|
14
|
+
|
|
15
|
+
Use structured output when you need an agent to return a data object rather than text. Having well defined fields can make it simpler to pull out the values you need for API calls, UI rendering, or application logic.
|
|
16
|
+
|
|
17
|
+
## Defining schemas
|
|
18
|
+
|
|
19
|
+
Agents can return structured data by defining the expected output with either [Zod](https://zod.dev/) or [JSON Schema](https://json-schema.org/). Zod is recommended because it provides TypeScript type inference and runtime validation, while JSON Schema is useful when you need a language agnostic format.
|
|
20
|
+
|
|
21
|
+
<Tabs>
|
|
22
|
+
<TabItem value="zod" label="Zod">
|
|
23
|
+
|
|
24
|
+
Define the `output` shape using [Zod](https://zod.dev/):
|
|
25
|
+
|
|
26
|
+
```typescript showLineNumbers copy
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
|
|
29
|
+
const response = await testAgent.generate("Help me plan my day.", {
|
|
30
|
+
structuredOutput: {
|
|
31
|
+
schema: z.array(
|
|
32
|
+
z.object({
|
|
33
|
+
name: z.string(),
|
|
34
|
+
activities: z.array(z.string()),
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log(response.object);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
</TabItem>
|
|
44
|
+
<TabItem value="json-schema" label="JSON Schema">
|
|
45
|
+
|
|
46
|
+
You can also use JSON Schema to define your output structure:
|
|
47
|
+
|
|
48
|
+
```typescript showLineNumbers copy
|
|
49
|
+
const response = await testAgent.generate("Help me plan my day.", {
|
|
50
|
+
structuredOutput: {
|
|
51
|
+
schema: {
|
|
52
|
+
type: "array",
|
|
53
|
+
items: {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
name: { type: "string" },
|
|
57
|
+
activities: {
|
|
58
|
+
type: "array",
|
|
59
|
+
items: { type: "string" },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
required: ["name", "activities"],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
console.log(response.object);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
</TabItem>
|
|
72
|
+
</Tabs>
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
> See [.generate()](/reference/v1/agents/generate#structuredoutput) for a full list of configuration options.
|
|
76
|
+
|
|
77
|
+
### Example output
|
|
78
|
+
|
|
79
|
+
The `response.object` will contain the structured data as defined by the schema.
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
[
|
|
83
|
+
{
|
|
84
|
+
"name": "Morning Routine",
|
|
85
|
+
"activities": ["Wake up at 7am", "Exercise", "Shower", "Breakfast"]
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"name": "Work",
|
|
89
|
+
"activities": ["Check emails", "Team meeting", "Lunch break"]
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
"name": "Evening",
|
|
93
|
+
"activities": ["Dinner", "Relax", "Read a book", "Sleep by 10pm"]
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Streaming
|
|
99
|
+
|
|
100
|
+
Streaming also supports structured output. The final structured object is available on `stream.fullStream` and after the stream completes on `stream.object`. Text stream chunks are still emitted, but they contain natural language text rather than structured data.
|
|
101
|
+
|
|
102
|
+
```typescript showLineNumbers copy
|
|
103
|
+
import { z } from "zod";
|
|
104
|
+
|
|
105
|
+
const stream = await testAgent.stream("Help me plan my day.", {
|
|
106
|
+
structuredOutput: {
|
|
107
|
+
schema: z.array(
|
|
108
|
+
z.object({
|
|
109
|
+
name: z.string(),
|
|
110
|
+
activities: z.array(z.string())
|
|
111
|
+
})
|
|
112
|
+
),
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
for await (const chunk of stream.fullStream) {
|
|
117
|
+
if (chunk.type === "object-result") {
|
|
118
|
+
console.log("\n", JSON.stringify(chunk, null, 2));
|
|
119
|
+
}
|
|
120
|
+
process.stdout.write(JSON.stringify(chunk));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log(await stream.object)
|
|
124
|
+
|
|
125
|
+
for await (const chunk of stream.textStream) {
|
|
126
|
+
process.stdout.write(chunk);
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Structuring agent
|
|
131
|
+
|
|
132
|
+
When your main agent isn't proficient at creating structured output you can provide a `model` to `structuredOutput`. In this case, Mastra uses a second agent under the hood to extract structured data from the main agent's natural language response. This makes two LLM calls, one to generate the response and another to turn that response into the structured object, which adds some latency and cost but can improve accuracy for complex structuring tasks.
|
|
133
|
+
|
|
134
|
+
```typescript showLineNumbers copy
|
|
135
|
+
import { z } from "zod";
|
|
136
|
+
|
|
137
|
+
const response = await testAgent.generate("Analyze the TypeScript programming language.", {
|
|
138
|
+
structuredOutput: {
|
|
139
|
+
schema: z.object({
|
|
140
|
+
overview: z.string(),
|
|
141
|
+
strengths: z.array(z.string()),
|
|
142
|
+
weaknesses: z.array(z.string()),
|
|
143
|
+
useCases: z.array(z.object({
|
|
144
|
+
scenario: z.string(),
|
|
145
|
+
reasoning: z.string(),
|
|
146
|
+
})),
|
|
147
|
+
comparison: z.object({
|
|
148
|
+
similarTo: z.array(z.string()),
|
|
149
|
+
differentiators: z.array(z.string()),
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
model: "openai/gpt-4o",
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
console.log(response.object);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Response format
|
|
160
|
+
|
|
161
|
+
By default, Mastra passes the schema to the model provider using the `response_format` API parameter. Most model providers have built-in support for this, which reliably enforces the schema.
|
|
162
|
+
|
|
163
|
+
If your model provider doesn't support `response_format`, you'll get an error from the API. When this happens, set `jsonPromptInjection: true`. This adds the schema to the system prompt instead, instructing the model to output JSON. This is less reliable than the API parameter approach.
|
|
164
|
+
|
|
165
|
+
```typescript showLineNumbers copy
|
|
166
|
+
import { z } from "zod";
|
|
167
|
+
|
|
168
|
+
const response = await testAgent.generate("Help me plan my day.", {
|
|
169
|
+
structuredOutput: {
|
|
170
|
+
schema: z.array(
|
|
171
|
+
z.object({
|
|
172
|
+
name: z.string(),
|
|
173
|
+
activities: z.array(z.string()),
|
|
174
|
+
}),
|
|
175
|
+
),
|
|
176
|
+
jsonPromptInjection: true,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
console.log(response.object);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
:::info[Gemini 2.5 with tools]
|
|
184
|
+
|
|
185
|
+
Gemini 2.5 models do not support combining `response_format` (structured output) with function calling (tools) in the same API call. If your agent has tools and you're using `structuredOutput` with a Gemini 2.5 model, you must set `jsonPromptInjection: true` to avoid the error `Function calling with a response mime type: 'application/json' is unsupported`.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
const response = await agentWithTools.generate("Your prompt", {
|
|
189
|
+
structuredOutput: {
|
|
190
|
+
schema: yourSchema,
|
|
191
|
+
jsonPromptInjection: true, // Required for Gemini 2.5 when tools are present
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
:::
|
|
196
|
+
|
|
197
|
+
## Error handling
|
|
198
|
+
|
|
199
|
+
When schema validation fails, you can control how errors are handled using `errorStrategy`. The default `strict` strategy throws an error, while `warn` logs a warning and continues. The `fallback` strategy returns the values provided using `fallbackValue`.
|
|
200
|
+
|
|
201
|
+
```typescript showLineNumbers copy
|
|
202
|
+
import { z } from "zod";
|
|
203
|
+
|
|
204
|
+
const response = await testAgent.generate("Tell me about TypeScript.", {
|
|
205
|
+
structuredOutput: {
|
|
206
|
+
schema: z.object({
|
|
207
|
+
summary: z.string(),
|
|
208
|
+
keyFeatures: z.array(z.string())
|
|
209
|
+
}),
|
|
210
|
+
errorStrategy: "fallback",
|
|
211
|
+
fallbackValue: {
|
|
212
|
+
summary: "TypeScript is a typed superset of JavaScript",
|
|
213
|
+
keyFeatures: ["Static typing", "Compiles to JavaScript", "Better tooling"]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
console.log(response.object);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Related
|
|
222
|
+
|
|
223
|
+
- [Using Tools](/docs/v1/agents/using-tools)
|
|
224
|
+
- [Agent Memory](/docs/v1/agents/agent-memory)
|