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 JsonResponseFormatV2 Model

From Leeroopedia
Revision as of 12:17, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Cohere_ai_Cohere_python_JsonResponseFormatV2_Model.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains SDK, Structured Output
Last Updated 2026-02-15 14:00 GMT

Overview

JsonResponseFormatV2 is a Pydantic model that configures JSON schema-based structured output for Cohere v2 chat API requests.

Description

The JsonResponseFormatV2 class extends UncheckedBaseModel and provides the json_schema field, which accepts an optional JSON schema dictionary. Unlike the v1 JsonResponseFormat which uses the aliased field name schema_, this v2 variant uses the direct field name json_schema. When provided, the Cohere model constrains its output to conform to the specified schema. The schema follows JSON Schema conventions. This field must not be specified when the response format type is set to "text".

Usage

Use JsonResponseFormatV2 when you need structured JSON output from Cohere's v2 chat API. This is the preferred structured output configuration for the v2 API and replaces JsonResponseFormat for v2 endpoints. It is useful for data extraction, form filling, and any scenario requiring predictable JSON structures from model responses.

Code Reference

Source Location

Signature

class JsonResponseFormatV2(UncheckedBaseModel):
    json_schema: typing.Optional[typing.Dict[str, typing.Any]] = pydantic.Field(default=None)

Import

from cohere.types import JsonResponseFormatV2

I/O Contract

Fields

Field Type Required Default Description
json_schema Optional[Dict[str, Any]] No None A JSON schema object that the output will adhere to. Must not be specified when the response format type is set to "text". Refer to the Cohere schema constraints guide for restrictions.

Usage Examples

import cohere
from cohere.types import JsonResponseFormatV2

client = cohere.ClientV2(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 JsonResponseFormatV2 with the v2 chat API
response = client.chat(
    model="command-r-plus",
    messages=[
        {"role": "user", "content": "Extract the name and age from: John is 30 years old."}
    ],
    response_format={
        "type": "json_object",
        "json_schema": schema,
    },
)

print(response.message.content[0].text)
# '{"name": "John", "age": 30}'

Related Pages

Page Connections

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