Skip to content

Commit cd333ff

Browse files
changelog link
1 parent b00cc26 commit cd333ff

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/content/changelog/workers-ai/2025-02-25-json-mode.mdx

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ date: 2025-02-25T15:00:00Z
66

77
import { TypeScriptExample } from "~/components";
88

9-
Workers AI now supports structured JSON outputs with [JSON mode](/workers-ai/json-mode/), which allows you to request a structured output response when interacting with AI models.
9+
Workers AI now supports structured JSON outputs with [JSON mode](/workers-ai/configuration/json-mode/), which allows you to request a structured output response when interacting with AI models.
1010

1111
This makes it much easier to retrieve structured data from your AI models, and avoids the (error prone!) need to parse large unstructured text responses to extract your data.
1212

@@ -23,13 +23,13 @@ interface Env {
2323

2424
// Define your JSON schema for a calendar event
2525
const CalendarEventSchema = {
26-
type: 'object',
27-
properties: {
28-
name: { type: 'string' },
29-
date: { type: 'string' },
30-
participants: { type: 'array', items: { type: 'string' } },
31-
},
32-
required: ['name', 'date', 'participants']
26+
type: "object",
27+
properties: {
28+
name: { type: "string" },
29+
date: { type: "string" },
30+
participants: { type: "array", items: { type: "string" } },
31+
},
32+
required: ["name", "date", "participants"],
3333
};
3434

3535
export default {
@@ -42,29 +42,32 @@ export default {
4242
});
4343

4444
const response = await client.chat.completions.create({
45-
model: 'gpt-4o-2024-08-06',
46-
messages: [
47-
{ role: 'system', content: 'Extract the event information.' },
48-
{ role: 'user', content: 'Alice and Bob are going to a science fair on Friday.' },
49-
],
45+
model: "gpt-4o-2024-08-06",
46+
messages: [
47+
{ role: "system", content: "Extract the event information." },
48+
{
49+
role: "user",
50+
content: "Alice and Bob are going to a science fair on Friday.",
51+
},
52+
],
5053
// Use the `response_format` option to request a structured JSON output
51-
response_format: {
54+
response_format: {
5255
// Set json_schema and provide ra schema, or json_object and parse it yourself
53-
type: 'json_schema',
54-
schema: CalendarEventSchema, // provide a schema
55-
},
56-
});
56+
type: "json_schema",
57+
schema: CalendarEventSchema, // provide a schema
58+
},
59+
});
5760

5861
// This will be of type CalendarEventSchema
5962
const event = response.choices[0].message.parsed;
6063

6164
return Response.json({
62-
"calendar_event": event,
63-
})
64-
}
65-
}
65+
calendar_event: event,
66+
});
67+
},
68+
};
6669
```
6770

6871
</TypeScriptExample>
6972

70-
To learn more about JSON mode and structured outputs, visit the [Workers AI documentation](/workers-ai/json-mode/).
73+
To learn more about JSON mode and structured outputs, visit the [Workers AI documentation](/workers-ai/json-mode/).

0 commit comments

Comments
 (0)