anthropicapi

package
v0.1.52 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package anthropicapi translates the Anthropic Messages API dialect to and from GoModel's canonical chat types. It is an ingress concern: it accepts the Anthropic wire format from clients and renders responses back in that format, independent of which provider ultimately serves the request.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EstimateInputTokens

func EstimateInputTokens(req *MessagesRequest) int

EstimateInputTokens returns a provider-agnostic heuristic estimate of the input token count for a Messages request (roughly characters / 4). It is an approximation, not a tokenizer-exact count.

func NewStreamConverter

func NewStreamConverter(body io.ReadCloser, model string) io.ReadCloser

NewStreamConverter wraps an OpenAI-style chat completion SSE stream and emits the equivalent Anthropic Messages SSE event sequence. The returned reader owns body and closes it on Close.

func ToChatRequest

func ToChatRequest(req *MessagesRequest) (*core.ChatRequest, error)

ToChatRequest translates an Anthropic Messages request into the canonical chat request. The translation is provider-agnostic: the resulting request runs through the standard chat-completions pipeline.

Types

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"`
	// text / thinking
	Text     string `json:"text,omitempty"`
	Thinking string `json:"thinking,omitempty"`
	// image
	Source *Source `json:"source,omitempty"`
	// tool_use
	ID    string          `json:"id,omitempty"`
	Name  string          `json:"name,omitempty"`
	Input json.RawMessage `json:"input,omitempty" swaggertype:"object"`
	// tool_result
	ToolUseID string          `json:"tool_use_id,omitempty"`
	Content   json.RawMessage `json:"content,omitempty" swaggertype:"object"`
	IsError   bool            `json:"is_error,omitempty"`
}

ContentBlock is one element of an Anthropic message content array. The struct is a union; only the fields relevant to Type are populated.

type CountTokensResponse

type CountTokensResponse struct {
	InputTokens int `json:"input_tokens"`
}

CountTokensResponse is the Anthropic /v1/messages/count_tokens response body.

type ErrorObject

type ErrorObject struct {
	Type    string `json:"type"`
	Message string `json:"message"`
}

ErrorObject is the inner error detail of an Anthropic error envelope.

type ErrorResponse

type ErrorResponse struct {
	Type  string      `json:"type"`
	Error ErrorObject `json:"error"`
}

ErrorResponse is the Anthropic error envelope.

func ErrorFromGateway

func ErrorFromGateway(err *core.GatewayError) (int, ErrorResponse)

ErrorFromGateway converts a gateway error into an HTTP status code and the Anthropic error envelope. A nil error is reported as a generic api_error.

type Message

type Message struct {
	Role    string          `json:"role"`
	Content json.RawMessage `json:"content" swaggertype:"object"`
}

Message is a single Anthropic conversation message. Content is a string or an array of ContentBlock values.

type MessagesRequest

type MessagesRequest struct {
	Model         string          `json:"model"`
	Messages      []Message       `json:"messages"`
	MaxTokens     int             `json:"max_tokens"`
	System        json.RawMessage `json:"system,omitempty" swaggertype:"object"`
	Metadata      *Metadata       `json:"metadata,omitempty"`
	StopSequences []string        `json:"stop_sequences,omitempty"`
	Stream        bool            `json:"stream,omitempty"`
	Temperature   *float64        `json:"temperature,omitempty"`
	TopP          *float64        `json:"top_p,omitempty"`
	TopK          *int            `json:"top_k,omitempty"`
	Tools         []Tool          `json:"tools,omitempty"`
	ToolChoice    *ToolChoice     `json:"tool_choice,omitempty"`
	Thinking      *Thinking       `json:"thinking,omitempty"`
}

MessagesRequest is the Anthropic Messages API request body. System and message content fields are polymorphic on the wire (string or array), so they are kept raw and decoded in request.go.

func DecodeMessagesRequest

func DecodeMessagesRequest(body []byte) (*MessagesRequest, error)

DecodeMessagesRequest parses an Anthropic Messages API request body.

type MessagesResponse

type MessagesResponse struct {
	ID           string                 `json:"id"`
	Type         string                 `json:"type"`
	Role         string                 `json:"role"`
	Model        string                 `json:"model"`
	Content      []ResponseContentBlock `json:"content"`
	StopReason   string                 `json:"stop_reason"`
	StopSequence *string                `json:"stop_sequence"`
	Usage        Usage                  `json:"usage"`
}

MessagesResponse is the Anthropic Messages API response body.

func FromChatResponse

func FromChatResponse(resp *core.ChatResponse) *MessagesResponse

FromChatResponse renders a canonical chat response in the Anthropic Messages response shape.

type Metadata

type Metadata struct {
	UserID string `json:"user_id,omitempty"`
}

Metadata carries the optional Anthropic request metadata object.

type ResponseContentBlock

type ResponseContentBlock struct {
	Type     string          `json:"type"`
	Text     string          `json:"text,omitempty"`
	Thinking string          `json:"thinking,omitempty"`
	ID       string          `json:"id,omitempty"`
	Name     string          `json:"name,omitempty"`
	Input    json.RawMessage `json:"input,omitempty" swaggertype:"object"`
}

ResponseContentBlock is one element of an Anthropic response content array.

type Source

type Source struct {
	Type      string `json:"type"`
	MediaType string `json:"media_type,omitempty"`
	Data      string `json:"data,omitempty"`
	URL       string `json:"url,omitempty"`
}

Source describes an Anthropic image source (base64 inline data or a URL).

type Thinking

type Thinking struct {
	Type         string `json:"type"`
	BudgetTokens int    `json:"budget_tokens,omitempty"`
}

Thinking is the Anthropic extended-thinking configuration.

type Tool

type Tool struct {
	Type        string          `json:"type,omitempty"`
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	InputSchema json.RawMessage `json:"input_schema,omitempty" swaggertype:"object"`
}

Tool is an Anthropic tool definition. A custom tool has no Type (or Type "custom"); a server/built-in tool (web search, code execution, …) carries a versioned Type and is rejected during translation — see convertTools.

type ToolChoice

type ToolChoice struct {
	Type                   string `json:"type"`
	Name                   string `json:"name,omitempty"`
	DisableParallelToolUse *bool  `json:"disable_parallel_tool_use,omitempty"`
}

ToolChoice constrains how the model selects tools.

type Usage

type Usage struct {
	InputTokens              int `json:"input_tokens"`
	OutputTokens             int `json:"output_tokens"`
	CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
	CacheReadInputTokens     int `json:"cache_read_input_tokens,omitempty"`
}

Usage reports Anthropic-style token usage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL