Implementation:Cohere ai Cohere python ResponseFormat Union
| Knowledge Sources | |
|---|---|
| Domains | SDK, Structured Output |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
ResponseFormat is a discriminated union type that specifies whether the Cohere model should return plain text or structured JSON output, represented by TextResponseFormat and JsonObjectResponseFormat variants.
Description
The ResponseFormat type is defined as an annotated union of two Pydantic models, discriminated by the type field:
- TextResponseFormat (
type="text") -- Configures the model to return plain text output. This is the default behavior and requires no additional configuration. - JsonObjectResponseFormat (
type="json_object") -- Configures the model to return JSON output. Includes an optional schema_ field (aliased as"schema") that accepts a JSON schema dictionary to constrain the output structure. The schema supports up to 5 levels of nesting.
Both variants extend UncheckedBaseModel and include documentation noting that this feature is supported on Command R 03-2024, Command R+ 04-2024, and newer models. The JSON object format is not supported in RAG mode (when connectors, documents, tools, or tool_results are provided).
Usage
Use ResponseFormat to control the output format of the Cohere chat API. Select TextResponseFormat for standard text responses, or JsonObjectResponseFormat when you need structured JSON output, optionally constrained by a JSON schema. When using JSON mode, always instruct the model explicitly to generate JSON in the message.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/types/response_format.py
Signature
class TextResponseFormat(UncheckedBaseModel):
type: typing.Literal["text"] = "text"
class JsonObjectResponseFormat(UncheckedBaseModel):
type: typing.Literal["json_object"] = "json_object"
schema_: typing_extensions.Annotated[
typing.Optional[typing.Dict[str, typing.Any]],
FieldMetadata(alias="schema"),
pydantic.Field(alias="schema"),
] = None
ResponseFormat = typing_extensions.Annotated[
typing.Union[TextResponseFormat, JsonObjectResponseFormat],
UnionMetadata(discriminant="type"),
]
Import
from cohere.types import TextResponseFormat, JsonObjectResponseFormat
# The ResponseFormat union type itself:
from cohere.types.response_format import ResponseFormat
I/O Contract
TextResponseFormat Fields
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
type |
Literal["text"] |
Yes | "text" |
Discriminator field identifying this as a text response format. |
JsonObjectResponseFormat Fields
| Field | Type | Required | Default | Alias | Description |
|---|---|---|---|---|---|
type |
Literal["json_object"] |
Yes | "json_object" |
-- | Discriminator field identifying this as a JSON object response format. |
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. |
Usage Examples
import cohere
from cohere.types import TextResponseFormat, JsonObjectResponseFormat
client = cohere.Client(api_key="YOUR_API_KEY")
# Example 1: Use plain text response format (default)
response = client.chat(
model="command-r-plus",
message="Tell me a joke.",
response_format=TextResponseFormat(),
)
print(response.text)
# Example 2: Use JSON object response format without schema
response = client.chat(
model="command-r-plus",
message="Generate a JSON object with a joke that has 'setup' and 'punchline' fields.",
response_format=JsonObjectResponseFormat(),
)
print(response.text)
# '{"setup": "Why did the scarecrow win an award?", "punchline": "He was outstanding in his field."}'
# Example 3: Use JSON object response format with a schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"hobbies": {
"type": "array",
"items": {"type": "string"},
},
},
"required": ["name", "age", "hobbies"],
}
response = client.chat(
model="command-r-plus",
message="Generate a JSON profile for a fictional person.",
response_format=JsonObjectResponseFormat(schema_=schema),
)
print(response.text)
# '{"name": "Alice", "age": 28, "hobbies": ["reading", "hiking", "photography"]}'