Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/_redirects
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,7 @@
/workers-ai/tutorials/creating-a-recommendation-api/ /developer-spotlight/tutorials/creating-a-recommendation-api/ 301
/workers/observability/baselime-integration/ /workers/observability/integrations/baselime-integration/ 301
/workers-ai/tutorials/image-generator-flux/ /workers-ai/tutorials/image-generation-playground/ 301
/workers-ai/json-mode/ /workers-ai/configuration/json-mode/ 301

# workers KV
/kv/platform/environments/ /kv/reference/environments/ 301
Expand Down
49 changes: 26 additions & 23 deletions src/content/changelog/workers-ai/2025-02-25-json-mode.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ date: 2025-02-25T15:00:00Z

import { TypeScriptExample } from "~/components";

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.
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.

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.

Expand All @@ -23,13 +23,13 @@ interface Env {

// Define your JSON schema for a calendar event
const CalendarEventSchema = {
type: 'object',
properties: {
name: { type: 'string' },
date: { type: 'string' },
participants: { type: 'array', items: { type: 'string' } },
},
required: ['name', 'date', 'participants']
type: "object",
properties: {
name: { type: "string" },
date: { type: "string" },
participants: { type: "array", items: { type: "string" } },
},
required: ["name", "date", "participants"],
};

export default {
Expand All @@ -42,29 +42,32 @@ export default {
});

const response = await client.chat.completions.create({
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'Extract the event information.' },
{ role: 'user', content: 'Alice and Bob are going to a science fair on Friday.' },
],
model: "gpt-4o-2024-08-06",
messages: [
{ role: "system", content: "Extract the event information." },
{
role: "user",
content: "Alice and Bob are going to a science fair on Friday.",
},
],
// Use the `response_format` option to request a structured JSON output
response_format: {
response_format: {
// Set json_schema and provide ra schema, or json_object and parse it yourself
type: 'json_schema',
schema: CalendarEventSchema, // provide a schema
},
});
type: "json_schema",
schema: CalendarEventSchema, // provide a schema
},
});

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

return Response.json({
"calendar_event": event,
})
}
}
calendar_event: event,
});
},
};
```

</TypeScriptExample>

To learn more about JSON mode and structured outputs, visit the [Workers AI documentation](/workers-ai/json-mode/).
To learn more about JSON mode and structured outputs, visit the [Workers AI documentation](/workers-ai/configuration/json-mode/).
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export const jsonModeResponseExample = `{
}
}`;

When we want text-generation AI models to interact with databases, services, and external systems programmatically, typically when using tool calling or building AI agents, we must have structured response formats rather than natural language.
When we want text-generation AI models to interact with databases, services, and external systems programmatically, typically when using tool calling or [building AI agents](/agents/), we must have structured response formats rather than natural language.

Workers AI supports JSON Mode, enabling applications to request a structured output response when interacting with AI models.

## Schema

JSON Mode is compatible with OpenAIs implementation; to enable add the `response_format` property to the request object using the following convention:
JSON Mode is compatible with OpenAI's implementation. To enable JSON Mode, add the `response_format` property to the request object using the following convention:

<Code code={jsonModeSchema} lang="json" />

Expand Down Expand Up @@ -122,6 +122,10 @@ This is the list of models that now support JSON Mode:

We will continue extending this list to keep up with new, and requested models.

Note that Workers AI can't guarantee that the model responds according to the requested JSON Schema. Depending on the complexity of the task and adequacy of the JSON Schema, the model may not be able to satisfy the request in extreme situations. If that's the case, then an error `JSON Mode couldn't be met` is returned and must be handled.
:::note[Note]

JSON Mode currently doesn't support streaming.
Workers AI cannot guarantee that the model responds according to the requested JSON Schema. Depending on the complexity of the task and adequacy of the JSON Schema, the model may not be able to satisfy the request in extreme situations. If that is the case, then an error `JSON Mode couldn't be met` is returned and must be handled.

:::

JSON Mode currently does not support streaming.
Loading