Implementation:Cohere ai Cohere python JsonResponseFormatV2 Model
| 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
- Repository: Cohere Python SDK
- File:
src/cohere/types/json_response_format_v2.py
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}'