Implementation:Ollama Ollama Anthropic Handler
| Knowledge Sources | |
|---|---|
| Domains | API Compatibility, Anthropic Protocol |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Implements the Anthropic Messages API compatibility layer, translating Anthropic-format requests into Ollama internal calls and streaming responses back in the Anthropic SSE format.
Description
The anthropic package provides a full HTTP handler that accepts Anthropic Messages API requests, validates them, converts the message format (including text, images, tool calls, and tool results) to Ollama's internal api.ChatRequest, forwards the request to the Ollama chat endpoint, and streams responses back as Anthropic-compatible server-sent events. It handles error mapping between HTTP status codes and Anthropic error types, supports both streaming and non-streaming modes, and generates unique request and message IDs.
Usage
Used when a client sends requests to the /v1/messages endpoint using the Anthropic Messages API format. The handler is registered on the server's router and activated for any Anthropic-compatible client integration.
Code Reference
Source Location
- Repository: Ollama
- File: anthropic/anthropic.go
- Lines: 1-1210
Signature
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
}
type ErrorResponse struct {
Type string `json:"type"`
Error Error `json:"error"`
RequestID string `json:"request_id,omitempty"`
}
func NewError(code int, message string) ErrorResponse
func (s *Server) handleMessages(w http.ResponseWriter, r *http.Request)
func toChatRequest(ar *AnthropicRequest) (*api.ChatRequest, error)
func fromChatResponse(cr *api.ChatResponse, sb *strings.Builder) MessageResponse
func stream(w http.ResponseWriter, ch <-chan any)
Import
import "github.com/ollama/ollama/anthropic"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| r | *http.Request | Yes | HTTP request containing Anthropic Messages API JSON body |
| AnthropicRequest.Model | string | Yes | Model name to use for inference |
| AnthropicRequest.Messages | []Message | Yes | Conversation messages in Anthropic format (text, image, tool_use, tool_result) |
| AnthropicRequest.MaxTokens | int | Yes | Maximum number of tokens to generate |
| AnthropicRequest.Stream | bool | No | Whether to stream the response via SSE |
| AnthropicRequest.System | string | No | System prompt text |
Outputs
| Name | Type | Description |
|---|---|---|
| MessageResponse | JSON | Anthropic-format message response with content blocks, usage stats, and stop reason |
| SSE stream | text/event-stream | Streamed events: message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop |
| ErrorResponse | JSON | Anthropic-format error with type classification (invalid_request_error, authentication_error, etc.) |
Usage Examples
// Register the Anthropic handler on the server
mux.HandleFunc("/v1/messages", s.handleMessages)
// Client sends an Anthropic Messages API request
// POST /v1/messages
// {
// "model": "llama3",
// "max_tokens": 1024,
// "messages": [{"role": "user", "content": "Hello"}],
// "stream": true
// }