You can now generate responses as JSON

Developers are now able to force Command R and Command R+ to generate outputs as JSON objects by setting the response_format parameter in the Chat API.

This feature is available across all of Cohere's SDKs (Python, Typescript, Java, Go), but only on the Cohere platform -- we expect to release to cloud distributions in the coming weeks.

Here's an example request that forces a JSON-formatted response, in cURL and the Python SDK:

curl --location 'https://api.cohere.ai/v1/chat' \
--header 'Authorization: bearer <YOUR_API_KEY>' \
--header 'Content-Type: application/json' \
--data '{
    "message": "Generate a JSON that represents a person, with name and age",
    "model": "command-r",
    "response_format": {
        "type": "json_object"
    }
}'
import cohere
co = cohere.Client(api_key="<YOUR API KEY>")

response = co.chat(
  model="command-r", # or command-r-plus
  message="Generate a JSON that represents a person, with name and age",
  response_format={
        "type": "json_object"
    }
)

print(response.text) 

We're also releasing a beta feature that allows users to pass in a defined schema to structure the JSON. Here's an example of what that looks like, in cURL and the Python SDK:

curl --location 'https://api.cohere.ai/v1/chat' \
--header 'Authorization: bearer <YOUR_API_KEY>' \
--header 'Content-Type: application/json' \
--data '{
    "message": "Generate a JSON that represents a person, with name and age",
    "model": "command-r",
    "response_format": {
        "type": "json_object",
        "schema": {
            "type": "object",
            "required": ["name", "age"],
            "properties": {
                "name": { "type": "string" },
                "age": { "type": "integer" }
            }
        }
    }
}'

response = co.chat(
  model="command-r", #or command-r-plus
  message="Generate a JSON that represents a person, with name and age",
  response_format={
        "type": "json_object",
        "schema": {
            "type": "object",
            "required": ["name", "age"],
            "properties": {
                "name": { "type": "string" },
                "age": { "type": "integer" }
            }
        }
    }
)