diff --git a/public/_redirects b/public/_redirects index 2b9455d86c935ef..52201c9d6553167 100644 --- a/public/_redirects +++ b/public/_redirects @@ -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 diff --git a/src/content/changelog/workers-ai/2025-02-25-json-mode.mdx b/src/content/changelog/workers-ai/2025-02-25-json-mode.mdx index 98d13202fe79032..5fb05579bc82bff 100644 --- a/src/content/changelog/workers-ai/2025-02-25-json-mode.mdx +++ b/src/content/changelog/workers-ai/2025-02-25-json-mode.mdx @@ -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. @@ -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 { @@ -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, + }); + }, +}; ``` -To learn more about JSON mode and structured outputs, visit the [Workers AI documentation](/workers-ai/json-mode/). \ No newline at end of file +To learn more about JSON mode and structured outputs, visit the [Workers AI documentation](/workers-ai/configuration/json-mode/). diff --git a/src/content/docs/workers-ai/json-mode/index.mdx b/src/content/docs/workers-ai/configuration/json-mode.mdx similarity index 82% rename from src/content/docs/workers-ai/json-mode/index.mdx rename to src/content/docs/workers-ai/configuration/json-mode.mdx index 452fb278aeaf26e..c86f66b2ab29f71 100644 --- a/src/content/docs/workers-ai/json-mode/index.mdx +++ b/src/content/docs/workers-ai/configuration/json-mode.mdx @@ -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 OpenAI’s 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: @@ -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. \ No newline at end of file +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.