Implementation:Openai Openai python Completion Choice Model
| Knowledge Sources | |
|---|---|
| Domains | API_Types, Python |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete type for a single completion choice within a legacy completion response, provided by the openai-python SDK.
Description
The CompletionChoice Pydantic model represents one generated completion from the legacy completions API. It includes a finish_reason (stop, length, or content_filter), an index indicating its position in the choices list, the generated text, and optional logprobs via the nested Logprobs model. The Logprobs model contains text_offset, token_logprobs, tokens, and top_logprobs arrays for detailed token probability analysis.
Usage
Import this type when you need to type-hint or inspect individual choices from a Completion response object.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/types/completion_choice.py
Signature
class Logprobs(BaseModel):
text_offset: Optional[List[int]] = None
token_logprobs: Optional[List[float]] = None
tokens: Optional[List[str]] = None
top_logprobs: Optional[List[Dict[str, float]]] = None
class CompletionChoice(BaseModel):
finish_reason: Literal["stop", "length", "content_filter"]
index: int
logprobs: Optional[Logprobs] = None
text: str
Import
from openai.types import CompletionChoice
I/O Contract
Fields
| Name | Type | Required | Description |
|---|---|---|---|
| finish_reason | Literal["stop", "length", "content_filter"] | Yes | Why the model stopped: natural stop, max tokens, or content filter |
| index | int | Yes | Position of this choice in the choices list |
| logprobs | Optional[Logprobs] | No | Token log probability information |
| text | str | Yes | The generated completion text |
Logprobs Fields
| Name | Type | Required | Description |
|---|---|---|---|
| text_offset | Optional[List[int]] | No | Character offsets for each token in the text |
| token_logprobs | Optional[List[float]] | No | Log probabilities for each token |
| tokens | Optional[List[str]] | No | The individual tokens |
| top_logprobs | Optional[List[Dict[str, float]]] | No | Top candidate tokens and their log probabilities |
Usage Examples
from openai import OpenAI
client = OpenAI()
completion = client.completions.create(
model="gpt-3.5-turbo-instruct",
prompt="The capital of France is",
max_tokens=10,
logprobs=3,
)
choice = completion.choices[0]
print(choice.text) # e.g. " Paris"
print(choice.finish_reason) # e.g. "stop"
if choice.logprobs:
print(choice.logprobs.tokens)