anthropic_completions
This module defines Pydantic models for Anthropic's chat completion API responses.
It extends the base models from spark_instructor.completions.base to provide specific implementations for Anthropic's API structure. These models can be used to parse and validate Anthropic API responses, ensuring type safety and providing easy access to response data.
Anthropic's completion models are not serializable out of the box, primarily due to Tools schema. We may add support for Anthropic Tools later on.
AnthropicCompletion
Bases: BaseCompletion
Represents a complete Anthropic chat completion response.
This class extends BaseCompletion to include all fields specific to Anthropic's API response structure.
Attributes:
Name | Type | Description |
---|---|---|
content |
List[AnthropicContent]
|
A list of content items in the completion. |
role |
str
|
The role of the message (e.g., "assistant"). |
stop_reason |
str
|
The reason why the completion stopped. |
stop_sequence |
Optional[str]
|
The stop sequence used, if any. Defaults to None. |
type |
str
|
The type of the completion, typically "message" for Anthropic. |
usage |
AnthropicUsage
|
An object containing token usage information. |
Source code in spark_instructor/completions/anthropic_completions.py
AnthropicContent
Bases: BaseModel
Represents a content item in an Anthropic chat completion response.
Attributes:
Name | Type | Description |
---|---|---|
text |
str
|
The text content of the message. |
type |
str
|
The type of the content, typically "text" for Anthropic responses. |
Source code in spark_instructor/completions/anthropic_completions.py
AnthropicUsage
Bases: BaseModel
Represents the token usage information for an Anthropic chat completion.
Attributes:
Name | Type | Description |
---|---|---|
input_tokens |
int
|
The number of tokens in the input prompt. |
output_tokens |
int
|
The number of tokens in the generated completion. |
Source code in spark_instructor/completions/anthropic_completions.py
anthropic_tool_call_to_openai_tool_call(block)
Convert an Anthropic tool call to an OpenAI tool call.
Source code in spark_instructor/completions/anthropic_completions.py
transform_message_to_chat_completion(message, enable_created_at=False)
Transform a Message object to a ChatCompletion object.
This function converts the structure of a Message (from the original schema) to a ChatCompletion (matching the target schema). It combines text blocks, converts tool use blocks to tool calls, maps stop reasons to finish reasons, and restructures usage information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
message |
Message | PromptCachingBetaMessage
|
The original Message object to be transformed. |
required |
enable_created_at |
bool
|
Whether to include a unix timestamp. |
False
|
Returns:
Name | Type | Description |
---|---|---|
ChatCompletion |
ChatCompletion
|
A new ChatCompletion object structured according to the target schema. |
Note
- Text blocks are combined into a single content string.
- Tool use blocks are converted to ChatCompletionMessageToolCall objects.
- The stop_reason is mapped to a corresponding finish_reason.
- Usage information is restructured to fit the CompletionUsage model.
- The timestamp is 0 by default but is updated when
enable_created_at
is True