Skip to content

Commit 5b1f273

Browse files
committed
JSON Mode documentation
1 parent 345fd5b commit 5b1f273

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: Workers AI JSON Mode
3+
description: Workers AI JSON Mode adds structured outputs support
4+
date: 2025-02-25T15:00:00Z
5+
---
6+
7+
We've updated the Workers AI to support [JSON mode](/workers-ai/json-mode/), enabling applications to request a structured output response when interacting with AI models.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
Here's a request to `@cf/meta/llama-3.1-8b-instruct-fp8-fast` using JSON mode:
90+
91+
<Code code={jsonModeRequestExample} lang="json" />
92+
93+
And what the response from the model:
94+
95+
<Code code={jsonModeResponseExample} lang="json" />
96+
97+
As you can see, the model is complying with the JSON schema definition in the request and responding with a validated JSON object.
98+
99+
JSON mode is compatible with OpenAI’s implementation; to enable add the `response_format` property to the request object following this schema:
100+
101+
<Code code={jsonModeSchema} lang="json" />
102+
103+
Where `json_schema` must be a valid [JSON Schema](https://json-schema.org/) declaration.
104+
105+
This is the list of models that now support JSON mode:
106+
107+
- [@cf/meta/llama-3.1-8b-instruct-fast](/workers-ai/models/llama-3.1-8b-instruct-fast/)
108+
- [@cf/meta/llama-3.1-70b-instruct](/workers-ai/models/llama-3.1-70b-instruct/)
109+
- [@cf/meta/llama-3.3-70b-instruct-fp8-fast](/workers-ai/models/llama-3.3-70b-instruct-fp8-fast/)
110+
- [@cf/deepseek-ai/deepseek-r1-distill-qwen-32b](/workers-ai/models/deepseek-r1-distill-qwen-32b/)
111+
- [@cf/meta/llama-3-8b-instruct](/workers-ai/models/llama-3-8b-instruct/)
112+
- [@cf/meta/llama-3.1-8b-instruct](/workers-ai/models/llama-3.1-8b-instruct/)
113+
- [@hf/nousresearch/hermes-2-pro-mistral-7b](/workers-ai/models/hermes-2-pro-mistral-7b/)
114+
115+
We will continue extending this list to keep up with new, and requested models.
116+
117+
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.

0 commit comments

Comments
 (0)