You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 30, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: apps/next/src/content/docs/llamaindex/modules/agents/agent_workflow.mdx
+52Lines changed: 52 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,58 @@ console.log(result.data.result); // Baby Llama is called cria
37
37
console.log(result.data.message); // { role: 'assistant', content: 'Baby Llama is called cria' }
38
38
```
39
39
40
+
### Structured Output
41
+
42
+
You can extract structured data from agent responses by providing a `responseFormat` with a Zod schema. This is useful when you need the agent's response in a specific format for further processing:
43
+
44
+
```typescript
45
+
import { z } from"zod";
46
+
import { tool } from"llamaindex";
47
+
import { agent } from"@llamaindex/workflow";
48
+
import { openai } from"@llamaindex/openai";
49
+
50
+
// Define a weather tool
51
+
const weatherTool =tool({
52
+
name: "weatherTool",
53
+
description: "Get weather information",
54
+
parameters: z.object({
55
+
location: z.string(),
56
+
}),
57
+
execute: ({ location }) => {
58
+
return`The weather in ${location} is sunny. The temperature is 72 degrees. The humidity is 50%. The wind speed is 10 mph.`;
59
+
},
60
+
});
61
+
62
+
// Define the structure you want for the response
63
+
const responseSchema =z.object({
64
+
temperature: z.number(),
65
+
humidity: z.number(),
66
+
windSpeed: z.number(),
67
+
});
68
+
69
+
// Create the agent
70
+
const weatherAgent =agent({
71
+
name: "weatherAgent",
72
+
tools: [weatherTool],
73
+
llm: openai({ model: "gpt-4.1-mini" }),
74
+
});
75
+
76
+
// Run with structured output
77
+
const result =awaitweatherAgent.run("What's the weather in Tokyo?", {
78
+
responseFormat: responseSchema,
79
+
});
80
+
81
+
console.log("Natural language result:", result.data.result);
A common pattern is to use `llm.exec` in a loop until the LLM stops making tool calls:
@@ -102,7 +135,7 @@ For real-time responses, use the `stream` option to get the assistant's response
102
135
103
136
```ts
104
137
import { openai } from"@llamaindex/openai";
105
-
import { tool } from"llamaindex";
138
+
import { ChatMessage, tool } from"llamaindex";
106
139
importzfrom"zod";
107
140
108
141
asyncfunction streamingAgentLoop() {
@@ -153,6 +186,7 @@ async function streamingAgentLoop() {
153
186
154
187
-**`newMessages`**: Array of new chat messages including the LLM response and any tool call messages (call or result). This is a function return the array when streaming.
155
188
-**`toolCalls`**: Array of tool calls made by the LLM
189
+
-**`object`**: The structured object when using `responseFormat` with a Zod schema (undefined if no schema is provided)
156
190
-**`stream`**: Async iterable for streaming responses (only when `stream: true`)
157
191
158
192
## Best Practices
@@ -161,4 +195,4 @@ For using `llm.exec` in an agent loop, take care to:
161
195
162
196
1.**Maintain message history**: Always add `newMessages` to your conversation history
163
197
2.**Set exit conditions**: Implement proper logic to avoid infinite loops
164
-
198
+
3.**Handle structured output**: When using `responseFormat`, the `object` property contains your parsed data
0 commit comments