-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstructured_outputs.php
More file actions
executable file
·231 lines (195 loc) · 7.78 KB
/
structured_outputs.php
File metadata and controls
executable file
·231 lines (195 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env php
<?php
/**
* Structured Outputs - PHP examples from:
* https://docs.claude.com/en/docs/build-with-claude/structured-outputs
*
* Get JSON responses that conform to your schema with guaranteed structure.
* Requires 'structured-outputs-2025-11-13' beta header (auto-added by parse()).
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/helpers.php';
use ClaudePhp\ClaudePhp;
loadEnv(__DIR__ . '/../.env');
$client = new ClaudePhp(apiKey: getApiKey());
echo "=== Structured Outputs - Guaranteed JSON Schema ===\n\n";
// Example 1: Basic structured output
echo "Example 1: Basic Structured Output\n";
echo "-----------------------------------\n";
echo "Extract structured data with guaranteed schema compliance\n\n";
try {
$schema = [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'age' => ['type' => 'integer'],
'email' => ['type' => 'string']
],
'required' => ['name', 'age']
];
$result = $client->beta()->messages()->parse([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
[
'role' => 'user',
'content' => 'Extract information: John Smith is 30 years old. Contact: john@example.com'
]
],
'output_format' => $schema
]);
echo "Input: 'John Smith is 30 years old. Contact: john@example.com'\n";
echo "Schema: name (string), age (integer), email (string)\n\n";
echo "Parsed output:\n";
print_r($result);
echo "\nNote: parse() automatically adds 'structured-outputs-2025-11-13' beta header\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 2: Complex nested schema
echo "Example 2: Complex Nested Schema\n";
echo "---------------------------------\n";
echo "Extract structured data with nested objects and arrays\n\n";
try {
$schema = [
'type' => 'object',
'properties' => [
'order_id' => ['type' => 'string'],
'customer' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string'],
'email' => ['type' => 'string']
],
'required' => ['name']
],
'items' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'product' => ['type' => 'string'],
'quantity' => ['type' => 'integer'],
'price' => ['type' => 'number']
],
'required' => ['product', 'quantity', 'price']
]
],
'total' => ['type' => 'number']
],
'required' => ['order_id', 'customer', 'items', 'total']
];
$result = $client->beta()->messages()->parse([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 2048,
'messages' => [
[
'role' => 'user',
'content' => 'Parse this order: Order #12345 for Jane Doe (jane@example.com). ' .
'Items: 2x Widget ($10 each), 1x Gadget ($25). Total: $45'
]
],
'output_format' => $schema
]);
echo "Complex order extraction with nested structure\n\n";
echo "Parsed order:\n";
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 3: Streaming structured outputs
echo "Example 3: Streaming Structured Outputs\n";
echo "----------------------------------------\n";
echo "Stream structured data as it's generated\n\n";
try {
$schema = [
'type' => 'object',
'properties' => [
'summary' => ['type' => 'string'],
'key_points' => [
'type' => 'array',
'items' => ['type' => 'string']
]
],
'required' => ['summary', 'key_points']
];
$rawStream = $client->beta()->messages()->streamStructured([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 2048,
'messages' => [
[
'role' => 'user',
'content' => 'Summarize with key points: Machine learning revolutionizes data analysis.'
]
],
'output_format' => $schema
]);
echo "Streaming structured output:\n";
foreach ($rawStream as $event) {
if (isset($event['parsed_output'])) {
echo "\nParsed output:\n";
echo json_encode($event['parsed_output'], JSON_PRETTY_PRINT) . "\n";
}
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 4: Schema design tips
echo "Example 4: Schema Design Tips\n";
echo "------------------------------\n\n";
echo "✓ Start Simple:\n";
echo " • Begin with basic object structure\n";
echo " • Add complexity incrementally\n";
echo " • Test with real data\n\n";
echo "✓ Be Specific:\n";
echo " • Use appropriate types (string, integer, number, boolean)\n";
echo " • Mark required fields\n";
echo " • Add descriptions for clarity\n";
echo " • Use enums for fixed values\n\n";
echo "✓ Handle Arrays:\n";
echo " • Define item schema clearly\n";
echo " • Consider min/max items\n";
echo " • Nested arrays work but add complexity\n\n";
echo "✓ Validation:\n";
echo " • Schema is enforced by Claude\n";
echo " • Output always matches schema\n";
echo " • No need for manual validation\n";
echo " • Type safety guaranteed\n";
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 5: Common use cases
echo "Example 5: Common Use Cases\n";
echo "---------------------------\n\n";
echo "✓ Data Extraction:\n";
echo " • Parse invoices, receipts, forms\n";
echo " • Extract entities from text\n";
echo " • Structured information retrieval\n\n";
echo "✓ API Integration:\n";
echo " • Generate API request payloads\n";
echo " • Parse API responses\n";
echo " • Data transformation\n\n";
echo "✓ Classification:\n";
echo " • Sentiment analysis with structured output\n";
echo " • Multi-label classification\n";
echo " • Confidence scores\n\n";
echo "✓ Workflow Automation:\n";
echo " • Consistent data format for pipelines\n";
echo " • Reliable parsing for downstream systems\n";
echo " • No post-processing needed\n";
echo "\n" . str_repeat("=", 80) . "\n\n";
echo "✓ Structured outputs examples completed!\n\n";
echo "Key Takeaways:\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "• Use \$client->beta()->messages()->parse() for structured outputs\n";
echo "• Requires 'output_format' parameter with JSON schema\n";
echo "• Beta header 'structured-outputs-2025-11-13' auto-added\n";
echo "• Guaranteed schema compliance (no manual validation needed)\n";
echo "• Supports streaming with streamStructured()\n";
echo "• Works with complex nested schemas\n";
echo "• Ideal for: Data extraction, API integration, classification\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Related examples:\n";
echo " • examples/beta_features.php - Beta features usage\n";
echo " • examples/tools.php - Function calling (similar structured outputs)\n";