Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Cohere ai Cohere python SingleGeneration Model

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

Overview

SingleGeneration is a Pydantic model representing a single text generation result returned by the Cohere Generate API.

Description

The SingleGeneration model encapsulates one generation output from the Cohere text generation endpoint. Each instance contains the generated text, a unique identifier, and optional metadata including the generation index (when multiple generations are requested) and token-level log-likelihoods for model evaluation.

The token_likelihoods field is only populated when the return_likelihoods parameter is set to GENERATION or ALL in the API request. The likelihood value represents the average log-likelihood of the entire generated string, which is useful for evaluating model performance, especially with custom models.

Usage

Use SingleGeneration when working with responses from the Cohere Generate API. It is typically returned as part of a list within a Generation response object when num_generations is greater than zero.

Code Reference

Source Location

Signature

class SingleGeneration(UncheckedBaseModel):
    id: str
    text: str
    index: typing.Optional[int] = pydantic.Field(default=None)
    likelihood: typing.Optional[float] = None
    token_likelihoods: typing.Optional[
        typing.List[SingleGenerationTokenLikelihoodsItem]
    ] = pydantic.Field(default=None)

Import

from cohere.types import SingleGeneration

I/O Contract

Fields

Field Type Required Description
id str Yes Unique identifier for this generation.
text str Yes The generated text content.
index Optional[int] No The nth generation index. Only present when num_generations is greater than zero.
likelihood Optional[float] No Average log-likelihood of the entire generated string.
token_likelihoods Optional[List[SingleGenerationTokenLikelihoodsItem]] No Per-token log-likelihoods. Only returned if return_likelihoods is set to GENERATION or ALL. The first token will not have a likelihood.

SingleGenerationTokenLikelihoodsItem Fields

Field Type Required Description
token str Yes The token string.
likelihood float Yes The log-likelihood value for this token.

Usage Examples

from cohere.types import SingleGeneration

# Accessing fields from a generate response
response = client.generate(
    prompt="Write a tagline for a coffee shop",
    num_generations=3,
    return_likelihoods="GENERATION"
)

for generation in response.generations:
    print(f"ID: {generation.id}")
    print(f"Text: {generation.text}")
    print(f"Index: {generation.index}")
    print(f"Likelihood: {generation.likelihood}")

    if generation.token_likelihoods:
        for token_info in generation.token_likelihoods:
            print(f"  Token: {token_info.token}, Likelihood: {token_info.likelihood}")

Related Pages

Page Connections

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