Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Cohere ai Cohere python JsonResponseFormat Model

From Leeroopedia
Knowledge Sources
Domains SDK, Structured Output
Last Updated 2026-02-15 14:00 GMT

Overview

JsonResponseFormat is a Pydantic model that configures JSON schema-based structured output for Cohere chat API requests (v1).

Description

The JsonResponseFormat class extends UncheckedBaseModel and provides a single optional field, schema_, which accepts a JSON schema dictionary defining the structure of the expected output. The field is aliased as "schema" in serialized form using FieldMetadata. When provided, the Cohere model will constrain its output to conform to the specified JSON schema. The schema supports up to 5 levels of nesting. This type is used in the v1 chat API; for v2, see JsonResponseFormatV2.

Usage

Use JsonResponseFormat when you need the Cohere model to return structured JSON output conforming to a specific schema in v1 API calls. This is particularly useful for extracting structured data, building pipelines that require predictable output formats, or integrating with downstream systems that expect specific JSON structures.

Code Reference

Source Location

Signature

class JsonResponseFormat(UncheckedBaseModel):
    schema_: typing_extensions.Annotated[
        typing.Optional[typing.Dict[str, typing.Any]],
        FieldMetadata(alias="schema"),
        pydantic.Field(alias="schema"),
    ] = None

Import

from cohere.types import JsonResponseFormat

I/O Contract

Fields

Field Type Required Default Alias Description
schema_ Optional[Dict[str, Any]] No None schema A JSON schema object that the output will adhere to. Supports up to 5 levels of nesting. Must not be specified when the response format type is set to "text".

Usage Examples

import cohere
from cohere.types import JsonResponseFormat

client = cohere.Client(api_key="YOUR_API_KEY")

# Define a JSON schema for structured output
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer"},
    },
    "required": ["name", "age"],
}

# Use JsonResponseFormat to constrain model output
response = client.chat(
    model="command-r-plus",
    message="Extract the name and age from: John is 30 years old.",
    response_format=JsonResponseFormat(schema_=schema),
)

print(response.text)
# '{"name": "John", "age": 30}'

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment