Implementation:Cohere ai Cohere python LogprobItem Model
| Knowledge Sources | |
|---|---|
| Domains | SDK, Token Probabilities |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
LogprobItem is a Pydantic model that represents token-level log probability information for a text chunk generated by the Cohere model.
Description
The LogprobItem class extends UncheckedBaseModel and provides three fields for inspecting the log probabilities of generated tokens. The text field contains the text chunk for which log probabilities were calculated. The token_ids field is a required list of integer token IDs that compose the text chunk. The logprobs field is an optional list of float values representing the log probability of each corresponding token. This type is typically found within model responses when log probabilities are requested.
Usage
Use LogprobItem when you need to analyze the confidence of generated text at the token level. This is valuable for tasks such as evaluating model uncertainty, implementing custom decoding strategies, detecting hallucinations, or building confidence-aware applications. Log probabilities are returned when explicitly requested in chat or generate API calls.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/types/logprob_item.py
Signature
class LogprobItem(UncheckedBaseModel):
text: typing.Optional[str] = pydantic.Field(default=None)
token_ids: typing.List[int] = pydantic.Field()
logprobs: typing.Optional[typing.List[float]] = pydantic.Field(default=None)
Import
from cohere.types import LogprobItem
I/O Contract
Fields
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
text |
Optional[str] |
No | None |
The text chunk for which the log probabilities were calculated. |
token_ids |
List[int] |
Yes | -- | The token IDs of each token used to construct the text chunk. |
logprobs |
Optional[List[float]] |
No | None |
The log probability of each token used to construct the text chunk. |
Usage Examples
from cohere.types import LogprobItem
# LogprobItem instances are typically returned from API responses
# when log probabilities are requested.
# Example: inspecting log probabilities from a response
logprob_item = LogprobItem(
text="Hello",
token_ids=[12345, 67890],
logprobs=[-0.105, -0.032],
)
print(f"Text: {logprob_item.text}")
print(f"Token IDs: {logprob_item.token_ids}")
print(f"Log probabilities: {logprob_item.logprobs}")
# Calculate token-level confidence
import math
if logprob_item.logprobs:
for token_id, lp in zip(logprob_item.token_ids, logprob_item.logprobs):
probability = math.exp(lp)
print(f"Token {token_id}: probability={probability:.4f}")