Implementation:Anthropics Anthropic sdk python Completion
| Knowledge Sources | |
|---|---|
| Domains | API Types, Legacy Completions |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
Completion is a Pydantic model that represents the response from the legacy Text Completions API. It contains the generated completion text, the model used, a unique identifier, stop reason, and a type discriminator.
Description
The Completion class extends BaseModel and models the response payload returned by the legacy /v1/complete endpoint. This is the older completion-style API (as opposed to the newer Messages API).
The model contains the following fields:
- id -- A unique string identifier for the completion. The format and length may change over time.
- completion -- The generated text, up to and excluding any stop sequences.
- model -- The model that produced the completion, typed as the
Modeltype. - stop_reason -- An optional string indicating why generation stopped. Values include
"stop_sequence"(a stop sequence was reached) or"max_tokens"(themax_tokens_to_samplelimit was exceeded). - type -- A literal discriminator always set to
"completion".
Usage
Use Completion when working with responses from the legacy Text Completions API. This model is returned by client.completions.create() when stream=False. Note that the Text Completions API is a legacy interface; the Messages API is recommended for new development.
Code Reference
Source Location
- Repository: Anthropic SDK Python
- File:
src/anthropic/types/completion.py
Signature
class Completion(BaseModel):
id: str
completion: str
model: Model
stop_reason: Optional[str] = None
type: Literal["completion"]
Import
from anthropic.types import Completion
I/O Contract
Fields
| Field | Type | Required | Description |
|---|---|---|---|
id |
str |
Yes | Unique object identifier. |
completion |
str |
Yes | The generated completion text, excluding stop sequences. |
model |
Model |
Yes | The model that produced the completion. |
stop_reason |
Optional[str] |
No | Why generation stopped: "stop_sequence" or "max_tokens".
|
type |
Literal["completion"] |
Yes | Object type. Always "completion".
|
Usage Examples
import anthropic
client = anthropic.Anthropic()
# Legacy Text Completions API
completion = client.completions.create(
model="claude-2.1",
max_tokens_to_sample=256,
prompt="\n\nHuman: What is the capital of France?\n\nAssistant:",
)
print(f"ID: {completion.id}")
print(f"Model: {completion.model}")
print(f"Completion: {completion.completion}")
print(f"Stop reason: {completion.stop_reason}")
print(f"Type: {completion.type}")
Related Pages
- CompletionCreateParams -- Request parameters for the legacy Text Completions API that produces this response.