-
Notifications
You must be signed in to change notification settings - Fork 10.3k
JSON Mode documentation #20266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
JSON Mode documentation #20266
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| title: Workers AI JSON Mode | ||
| description: Workers AI JSON Mode adds structured outputs support | ||
| date: 2025-02-25T15:00:00Z | ||
| --- | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| --- | ||
| pcx_content_type: navigation | ||
| title: JSON Mode | ||
| hideChildren: true | ||
| sidebar: | ||
| order: 5 | ||
| --- | ||
|
|
||
| import { Code } from "~/components"; | ||
|
|
||
| export const jsonModeSchema = `{ | ||
| response_format: { | ||
| title: "JSON Mode", | ||
| type: "object", | ||
| properties: { | ||
| type: { | ||
| type: "string", | ||
| enum: ["json_object", "json_schema"], | ||
| }, | ||
| json_schema: {}, | ||
| } | ||
| } | ||
| }`; | ||
|
|
||
| export const jsonModeRequestExample = `{ | ||
| "messages": [ | ||
| { | ||
| "role": "system", | ||
| "content": "Extract data about a country." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": "Tell me about India." | ||
| } | ||
| ], | ||
| "response_format": { | ||
| "type": "json_schema", | ||
| "json_schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "name": { | ||
| "type": "string" | ||
| }, | ||
| "capital": { | ||
| "type": "string" | ||
| }, | ||
| "languages": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "string" | ||
| } | ||
| } | ||
| }, | ||
| "required": [ | ||
| "name", | ||
| "capital", | ||
| "languages" | ||
| ] | ||
| } | ||
| } | ||
| }`; | ||
|
|
||
| export const jsonModeResponseExample = `{ | ||
| "response": { | ||
| "name": "India", | ||
| "capital": "New Delhi", | ||
| "languages": [ | ||
| "Hindi", | ||
| "English", | ||
| "Bengali", | ||
| "Telugu", | ||
| "Marathi", | ||
| "Tamil", | ||
| "Gujarati", | ||
| "Urdu", | ||
| "Kannada", | ||
| "Odia", | ||
| "Malayalam", | ||
| "Punjabi", | ||
| "Sanskrit" | ||
| ] | ||
| } | ||
| }`; | ||
|
|
||
| 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. | ||
|
|
||
| Workers AI supports JSON mode, enabling applications to request a structured output response when interacting with AI models. | ||
|
|
||
| Here's a request to `@cf/meta/llama-3.1-8b-instruct-fp8-fast` using JSON mode: | ||
|
|
||
| <Code code={jsonModeRequestExample} lang="json" /> | ||
|
|
||
| And here's the response from the model, in JSON: | ||
|
|
||
| <Code code={jsonModeResponseExample} lang="json" /> | ||
|
|
||
| As you can see, the model is complying with the JSON schema definition in the request and responding with a validated JSON object. | ||
|
|
||
| JSON mode is compatible with OpenAI’s implementation; to enable add the `response_format` property to the request object following this schema: | ||
|
|
||
| <Code code={jsonModeSchema} lang="json" /> | ||
|
|
||
| Where `json_schema` must be a valid [JSON Schema](https://json-schema.org/) declaration. | ||
|
|
||
| This is the list of models that now support JSON mode: | ||
|
|
||
| - [@cf/meta/llama-3.1-8b-instruct-fast](/workers-ai/models/llama-3.1-8b-instruct-fast/) | ||
| - [@cf/meta/llama-3.1-70b-instruct](/workers-ai/models/llama-3.1-70b-instruct/) | ||
| - [@cf/meta/llama-3.3-70b-instruct-fp8-fast](/workers-ai/models/llama-3.3-70b-instruct-fp8-fast/) | ||
| - [@cf/meta/llama-3-8b-instruct](/workers-ai/models/llama-3-8b-instruct/) | ||
| - [@cf/meta/llama-3.1-8b-instruct](/workers-ai/models/llama-3.1-8b-instruct/) | ||
| - [@cf/meta/llama-3.2-11b-vision-instruct](/workers-ai/models/llama-3.2-11b-vision-instruct/) | ||
| - [@hf/nousresearch/hermes-2-pro-mistral-7b](/workers-ai/models/hermes-2-pro-mistral-7b/) | ||
| - [@hf/thebloke/deepseek-coder-6.7b-instruct-awq](/workers-ai/models/deepseek-coder-6.7b-instruct-awq/) | ||
| - [@cf/deepseek-ai/deepseek-r1-distill-qwen-32b](/workers-ai/models/deepseek-r1-distill-qwen-32b/) | ||
|
|
||
| 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. | ||
|
|
||
| JSON Mode currently doesn't support streaming. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.