|
| 1 | +--- |
| 2 | +pcx_content_type: navigation |
| 3 | +title: JSON Mode |
| 4 | +hideChildren: true |
| 5 | +sidebar: |
| 6 | + order: 5 |
| 7 | +--- |
| 8 | + |
| 9 | +import { Code } from "~/components"; |
| 10 | + |
| 11 | +export const jsonModeSchema = `{ |
| 12 | + response_format: { |
| 13 | + title: "JSON Mode", |
| 14 | + type: "object", |
| 15 | + properties: { |
| 16 | + type: { |
| 17 | + type: "string", |
| 18 | + enum: ["json_object", "json_schema"], |
| 19 | + }, |
| 20 | + json_schema: {}, |
| 21 | + } |
| 22 | + } |
| 23 | +}`; |
| 24 | + |
| 25 | +export const jsonModeRequestExample = `{ |
| 26 | + "messages": [ |
| 27 | + { |
| 28 | + "role": "system", |
| 29 | + "content": "Extract data about a country." |
| 30 | + }, |
| 31 | + { |
| 32 | + "role": "user", |
| 33 | + "content": "Tell me about India." |
| 34 | + } |
| 35 | + ], |
| 36 | + "response_format": { |
| 37 | + "type": "json_schema", |
| 38 | + "json_schema": { |
| 39 | + "type": "object", |
| 40 | + "properties": { |
| 41 | + "name": { |
| 42 | + "type": "string" |
| 43 | + }, |
| 44 | + "capital": { |
| 45 | + "type": "string" |
| 46 | + }, |
| 47 | + "languages": { |
| 48 | + "type": "array", |
| 49 | + "items": { |
| 50 | + "type": "string" |
| 51 | + } |
| 52 | + } |
| 53 | + }, |
| 54 | + "required": [ |
| 55 | + "name", |
| 56 | + "capital", |
| 57 | + "languages" |
| 58 | + ] |
| 59 | + } |
| 60 | + } |
| 61 | +}`; |
| 62 | + |
| 63 | +export const jsonModeResponseExample = `{ |
| 64 | + "response": { |
| 65 | + "name": "India", |
| 66 | + "capital": "New Delhi", |
| 67 | + "languages": [ |
| 68 | + "Hindi", |
| 69 | + "English", |
| 70 | + "Bengali", |
| 71 | + "Telugu", |
| 72 | + "Marathi", |
| 73 | + "Tamil", |
| 74 | + "Gujarati", |
| 75 | + "Urdu", |
| 76 | + "Kannada", |
| 77 | + "Odia", |
| 78 | + "Malayalam", |
| 79 | + "Punjabi", |
| 80 | + "Sanskrit" |
| 81 | + ] |
| 82 | + } |
| 83 | +}`; |
| 84 | + |
| 85 | +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. |
| 86 | + |
| 87 | +Workers AI supports JSON Mode, enabling applications to request a structured output response when interacting with AI models. |
| 88 | + |
| 89 | +## Schema |
| 90 | + |
| 91 | +JSON Mode is compatible with OpenAI’s implementation; to enable add the `response_format` property to the request object using the following convention: |
| 92 | + |
| 93 | +<Code code={jsonModeSchema} lang="json" /> |
| 94 | + |
| 95 | +Where `json_schema` must be a valid [JSON Schema](https://json-schema.org/) declaration. |
| 96 | + |
| 97 | +## JSON Mode example |
| 98 | + |
| 99 | +When using JSON Format, pass the schema as in the example below as part of the request you send to the LLM. |
| 100 | + |
| 101 | +<Code code={jsonModeRequestExample} lang="json" /> |
| 102 | + |
| 103 | +The LLM will follow the schema, and return a response such as below: |
| 104 | + |
| 105 | +<Code code={jsonModeResponseExample} lang="json" /> |
| 106 | + |
| 107 | +As you can see, the model is complying with the JSON schema definition in the request and responding with a validated JSON object. |
| 108 | + |
| 109 | +## Supported Models |
| 110 | + |
| 111 | +This is the list of models that now support JSON Mode: |
| 112 | + |
| 113 | +- [@cf/meta/llama-3.1-8b-instruct-fast](/workers-ai/models/llama-3.1-8b-instruct-fast/) |
| 114 | +- [@cf/meta/llama-3.1-70b-instruct](/workers-ai/models/llama-3.1-70b-instruct/) |
| 115 | +- [@cf/meta/llama-3.3-70b-instruct-fp8-fast](/workers-ai/models/llama-3.3-70b-instruct-fp8-fast/) |
| 116 | +- [@cf/meta/llama-3-8b-instruct](/workers-ai/models/llama-3-8b-instruct/) |
| 117 | +- [@cf/meta/llama-3.1-8b-instruct](/workers-ai/models/llama-3.1-8b-instruct/) |
| 118 | +- [@cf/meta/llama-3.2-11b-vision-instruct](/workers-ai/models/llama-3.2-11b-vision-instruct/) |
| 119 | +- [@hf/nousresearch/hermes-2-pro-mistral-7b](/workers-ai/models/hermes-2-pro-mistral-7b/) |
| 120 | +- [@hf/thebloke/deepseek-coder-6.7b-instruct-awq](/workers-ai/models/deepseek-coder-6.7b-instruct-awq/) |
| 121 | +- [@cf/deepseek-ai/deepseek-r1-distill-qwen-32b](/workers-ai/models/deepseek-r1-distill-qwen-32b/) |
| 122 | + |
| 123 | +We will continue extending this list to keep up with new, and requested models. |
| 124 | + |
| 125 | +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. |
| 126 | + |
| 127 | +JSON Mode currently doesn't support streaming. |
0 commit comments