Workers AI now offers structured JSON outputs through its JSON mode, enabling users to request structured responses directly from AI models.
This feature significantly simplifies the retrieval of structured data from AI models, eliminating the error-prone process of parsing extensive unstructured text to extract specific information.
Workers AI’s JSON mode is compatible with the OpenAI SDK’s structured outputs ↗ response_format API, which can be directly implemented within a Worker:
JavaScript Example
import { OpenAI } from "openai";
// Define your JSON schema for a calendar eventconst CalendarEventSchema = { type: "object", properties: { name: { type: "string" }, date: { type: "string" }, participants: { type: "array", items: { type: "string" } }, }, required: ["name", "date", "participants"],};
export default { async fetch(request, env) { const client = new OpenAI({ apiKey: env.OPENAI_API_KEY, // Optional: use AI Gateway to bring logs, evals & caching to your AI requests // https://developers.cloudflare.com/ai-gateway/usage/providers/openai/ // baseUrl: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai" });
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.", }, ], // Use the `response_format` option to request a structured JSON output response_format: { // Set json_schema and provide ra schema, or json_object and parse it yourself 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, }); },};
TypeScript Example
import { OpenAI } from "openai";
interface Env { OPENAI_API_KEY: string;}
// Define your JSON schema for a calendar eventconst CalendarEventSchema = { type: "object", properties: { name: { type: "string" }, date: { type: "string" }, participants: { type: "array", items: { type: "string" } }, }, required: ["name", "date", "participants"],};
export default { async fetch(request: Request, env: Env) { const client = new OpenAI({ apiKey: env.OPENAI_API_KEY, // Optional: use AI Gateway to bring logs, evals & caching to your AI requests // https://developers.cloudflare.com/ai-gateway/usage/providers/openai/ // baseUrl: "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai" });
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.", }, ], // Use the `response_format` option to request a structured JSON output response_format: { // Set json_schema and provide ra schema, or json_object and parse it yourself 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, }); },};
Further details on JSON mode and structured outputs are available in the Workers AI documentation.

