anthropic_gleam
Anthropic Gleam - A typed Gleam client for Anthropic’s Claude API
This library provides a well-typed, idiomatic interface to Claude’s API, including support for streaming responses and tool use.
Quick Start
import anthropic/types/message.{user_message}
import anthropic/types/request.{create_request, request_to_json_string}
let request = create_request(
"claude-sonnet-4-20250514",
[user_message("Hello, Claude!")],
1024,
)
let json_string = request_to_json_string(request)
Message Types
The library provides comprehensive types for the Messages API:
Role- User or AssistantContentBlock- TextBlock, ImageBlock, ToolUseBlock, ToolResultBlockMessage- A complete message with role and content
Request/Response Types
CreateMessageRequest- Request to create a messageCreateMessageResponse- Response from the APIUsage- Token usage informationStopReason- Why generation stopped
Error Types
AnthropicError- Sum type for all error categoriesApiErrorType- Specific API error typesApiErrorDetails- Details from API error responses
All types support JSON encoding/decoding for API communication.
Types
pub type AccumulatorState =
accumulator.AccumulatorState
pub type AnthropicError =
error.AnthropicError
pub type ApiErrorDetails =
error.ApiErrorDetails
pub type ApiErrorType =
error.ApiErrorType
pub type Client =
client.Client
pub type Config =
config.Config
pub type ConfigOptions =
config.ConfigOptions
pub type ContentBlock =
message.ContentBlock
pub type ContentBlockDelta =
streaming.ContentBlockDelta
pub type ContentBlockDeltaEvent =
streaming.ContentBlockDeltaEvent
pub type ContentBlockStart =
streaming.ContentBlockStart
pub type ContentBlockState =
accumulator.ContentBlockState
pub type ContentBlockStop =
streaming.ContentBlockStop
pub type CreateMessageRequest =
request.CreateMessageRequest
pub type CreateMessageResponse =
request.CreateMessageResponse
pub type DecodeError =
decoder.DecodeError
pub type EventCallback =
fn(streaming.StreamEvent) -> Nil
pub type ImageSource =
message.ImageSource
pub type ImageSourceType =
message.ImageSourceType
pub type InputJsonDelta =
streaming.InputJsonDelta
pub type Message =
message.Message
pub type MessageDelta =
streaming.MessageDelta
pub type MessageDeltaEvent =
streaming.MessageDeltaEvent
pub type MessageDeltaUsage =
streaming.MessageDeltaUsage
pub type MessageStart =
streaming.MessageStart
pub type Metadata =
request.Metadata
pub type Role =
message.Role
pub type SseError =
sse.SseError
pub type SseEvent =
sse.SseEvent
pub type SseParseResult =
sse.SseParseResult
pub type SseParserState =
sse.SseParserState
pub type StopReason =
request.StopReason
pub type StreamError =
streaming.StreamError
pub type StreamEvent =
streaming.StreamEvent
pub type StreamHandlerError =
handler.StreamError
pub type StreamResult =
handler.StreamResult
pub type TextDelta =
streaming.TextDelta
pub type Usage =
request.Usage
Values
pub const accumulate: fn(List(streaming.StreamEvent)) -> Result(
request.CreateMessageResponse,
String,
)
pub const accumulator_has_content: fn(
accumulator.AccumulatorState,
) -> Bool
pub const accumulator_has_error: fn(accumulator.AccumulatorState) -> Bool
pub const api_error: fn(Int, error.ApiErrorDetails) -> error.AnthropicError
pub const api_error_details: fn(error.ApiErrorType, String) -> error.ApiErrorDetails
pub const api_error_details_full: fn(
error.ApiErrorType,
String,
option.Option(String),
option.Option(String),
) -> error.ApiErrorDetails
pub const api_error_details_to_json: fn(error.ApiErrorDetails) -> json.Json
pub const api_error_details_to_string: fn(error.ApiErrorDetails) -> String
pub const api_error_type_from_string: fn(String) -> error.ApiErrorType
pub const api_error_type_to_string: fn(error.ApiErrorType) -> String
pub const api_version: String
pub const assistant_message: fn(String) -> message.Message
pub const authentication_error: fn(String) -> error.AnthropicError
pub const build_response: fn(accumulator.AccumulatorState) -> Result(
request.CreateMessageResponse,
String,
)
pub const config_error: fn(String) -> error.AnthropicError
pub const config_options: fn() -> config.ConfigOptions
pub const content_block_stop: fn(Int) -> streaming.ContentBlockStop
pub const content_block_to_json: fn(message.ContentBlock) -> json.Json
pub const content_block_to_json_string: fn(message.ContentBlock) -> String
pub const content_block_type: fn(message.ContentBlock) -> String
pub const create_message: fn(
client.Client,
request.CreateMessageRequest,
) -> Result(request.CreateMessageResponse, error.AnthropicError)
pub const create_request: fn(String, List(message.Message), Int) -> request.CreateMessageRequest
pub const create_response: fn(
String,
List(message.ContentBlock),
String,
option.Option(request.StopReason),
request.Usage,
) -> request.CreateMessageResponse
pub const create_response_with_stop_sequence: fn(
String,
List(message.ContentBlock),
String,
request.StopReason,
String,
request.Usage,
) -> request.CreateMessageResponse
pub const decode_event: fn(sse.SseEvent) -> Result(
streaming.StreamEvent,
decoder.DecodeError,
)
pub const default_base_url: String
pub const default_max_retries: Int
pub const default_timeout_ms: Int
pub const error_category: fn(error.AnthropicError) -> String
pub const error_to_json: fn(error.AnthropicError) -> json.Json
pub const error_to_json_string: fn(error.AnthropicError) -> String
pub const error_to_string: fn(error.AnthropicError) -> String
pub const event_type_from_string: fn(String) -> Result(
String,
String,
)
pub const event_type_string: fn(streaming.StreamEvent) -> String
pub const fixture_conversation_response: fn() -> request.CreateMessageResponse
pub const fixture_max_tokens_response: fn() -> request.CreateMessageResponse
pub const fixture_simple_response: fn() -> request.CreateMessageResponse
pub const fixture_stop_sequence_response: fn() -> request.CreateMessageResponse
pub const fixture_tool_use_response: fn() -> request.CreateMessageResponse
pub const flush: fn(sse.SseParserState) -> Result(
sse.SseEvent,
sse.SseError,
)
pub const get_accumulated_text: fn(accumulator.AccumulatorState) -> String
pub const get_accumulated_tool_inputs: fn(
accumulator.AccumulatorState,
) -> List(#(String, String, String))
pub const get_data: fn(sse.SseEvent) -> String
pub const get_delta_json: fn(streaming.ContentBlockDelta) -> option.Option(
String,
)
pub const get_delta_text: fn(streaming.ContentBlockDelta) -> option.Option(
String,
)
pub const get_event_type: fn(sse.SseEvent) -> String
pub const get_full_text: fn(List(streaming.StreamEvent)) -> String
pub const get_message_id: fn(List(streaming.StreamEvent)) -> Result(
String,
Nil,
)
pub const get_model: fn(List(streaming.StreamEvent)) -> Result(
String,
Nil,
)
pub const get_status_code: fn(error.AnthropicError) -> option.Option(
Int,
)
pub const get_stream_error: fn(List(streaming.StreamEvent)) -> Result(
streaming.StreamError,
Nil,
)
pub const get_text_deltas: fn(List(streaming.StreamEvent)) -> List(
String,
)
pub const get_tool_uses: fn(message.Message) -> List(
message.ContentBlock,
)
pub const handle_response: fn(response.Response(String)) -> Result(
String,
error.AnthropicError,
)
pub const has_api_key: fn() -> Bool
pub const has_tool_use: fn(message.Message) -> Bool
pub const http_error: fn(String) -> error.AnthropicError
pub const image: fn(String, String) -> message.ContentBlock
pub const image_source_to_json: fn(message.ImageSource) -> json.Json
pub const input_json_delta: fn(String) -> streaming.InputJsonDelta
pub const input_json_delta_event: fn(Int, String) -> streaming.ContentBlockDeltaEvent
pub const internal_api_error: fn(String) -> error.AnthropicError
pub const invalid_api_key_error: fn() -> error.AnthropicError
pub const invalid_request_error: fn(String) -> error.AnthropicError
pub const is_authentication_error: fn(error.AnthropicError) -> Bool
pub const is_keepalive: fn(sse.SseEvent) -> Bool
pub const is_overloaded_error: fn(error.AnthropicError) -> Bool
pub const is_rate_limit_error: fn(error.AnthropicError) -> Bool
pub const is_retryable: fn(error.AnthropicError) -> Bool
pub const is_terminal_event: fn(streaming.StreamEvent) -> Bool
pub const json_error: fn(String) -> error.AnthropicError
pub const load_config: fn(config.ConfigOptions) -> Result(
config.Config,
error.AnthropicError,
)
pub const message_delta_event: fn(
option.Option(request.StopReason),
option.Option(String),
Int,
) -> streaming.MessageDeltaEvent
pub const message_start: fn(String, message.Role, String, Int) -> streaming.MessageStart
pub const message_text: fn(message.Message) -> String
pub const message_to_json: fn(message.Message) -> json.Json
pub const message_to_json_string: fn(message.Message) -> String
pub const messages_endpoint: String
pub const messages_to_json: fn(List(message.Message)) -> json.Json
pub const metadata_to_json: fn(request.Metadata) -> json.Json
pub const missing_api_key_error: fn() -> error.AnthropicError
pub const mock_auth_error: fn() -> response.Response(String)
pub const mock_error_body: fn(String, String) -> String
pub const mock_error_response: fn(Int, String, String) -> response.Response(
String,
)
pub const mock_invalid_request_error: fn(String) -> response.Response(
String,
)
pub const mock_overloaded_error: fn() -> response.Response(String)
pub const mock_rate_limit_error: fn() -> response.Response(String)
pub const mock_text_response: fn(String) -> response.Response(
String,
)
pub const mock_text_response_body: fn(String, String) -> String
pub const mock_tool_use_response: fn(String, String, String) -> response.Response(
String,
)
pub const mock_tool_use_response_body: fn(
String,
String,
String,
String,
) -> String
pub const network_error: fn(String) -> error.AnthropicError
pub const new_accumulator: fn() -> accumulator.AccumulatorState
pub const new_client: fn(config.Config) -> client.Client
pub const new_parser_state: fn() -> sse.SseParserState
pub const overloaded_error: fn(String) -> error.AnthropicError
pub const parse_chunk: fn(sse.SseParserState, String) -> sse.SseParseResult
pub const parse_event: fn(String) -> Result(
sse.SseEvent,
sse.SseError,
)
pub const parse_event_lines: fn(List(String)) -> Result(
sse.SseEvent,
sse.SseError,
)
pub const parse_line: fn(sse.SseParserState, String) -> sse.SseParserState
pub const post_and_handle: fn(client.Client, String, String) -> Result(
String,
error.AnthropicError,
)
pub const post_json: fn(client.Client, String, String) -> Result(
response.Response(String),
error.AnthropicError,
)
pub const process_event: fn(
accumulator.AccumulatorState,
streaming.StreamEvent,
) -> accumulator.AccumulatorState
pub const process_events: fn(List(streaming.StreamEvent)) -> accumulator.AccumulatorState
pub const process_events_with_callback: fn(
List(streaming.StreamEvent),
fn(streaming.StreamEvent, accumulator.AccumulatorState) -> Nil,
) -> accumulator.AccumulatorState
pub const rate_limit_error: fn(String) -> error.AnthropicError
pub const request_to_json: fn(request.CreateMessageRequest) -> json.Json
pub const request_to_json_string: fn(request.CreateMessageRequest) -> String
pub const reset_event_state: fn(sse.SseParserState) -> sse.SseParserState
pub const response_get_tool_uses: fn(
request.CreateMessageResponse,
) -> List(message.ContentBlock)
pub const response_has_tool_use: fn(request.CreateMessageResponse) -> Bool
pub const response_text: fn(request.CreateMessageResponse) -> String
pub const response_to_json: fn(request.CreateMessageResponse) -> json.Json
pub const response_to_json_string: fn(
request.CreateMessageResponse,
) -> String
pub const role_from_string: fn(String) -> Result(
message.Role,
String,
)
pub const role_to_json: fn(message.Role) -> json.Json
pub const role_to_string: fn(message.Role) -> String
pub const sse_event: fn(
option.Option(String),
option.Option(String),
) -> sse.SseEvent
pub const sse_event_full: fn(
option.Option(String),
option.Option(String),
option.Option(String),
option.Option(Int),
) -> sse.SseEvent
pub const stop_reason_from_string: fn(String) -> Result(
request.StopReason,
String,
)
pub const stop_reason_to_string: fn(request.StopReason) -> String
pub const stream_error: fn(String, String) -> streaming.StreamError
pub const stream_has_error: fn(List(streaming.StreamEvent)) -> Bool
pub const stream_is_complete: fn(List(streaming.StreamEvent)) -> Bool
pub const stream_message: fn(
client.Client,
request.CreateMessageRequest,
) -> Result(handler.StreamResult, handler.StreamError)
pub const stream_message_with_callback: fn(
client.Client,
request.CreateMessageRequest,
fn(streaming.StreamEvent) -> Nil,
) -> Result(handler.StreamResult, handler.StreamError)
pub const text: fn(String) -> message.ContentBlock
pub const text_block_start: fn(Int) -> streaming.ContentBlockStart
pub const text_delta: fn(String) -> streaming.TextDelta
pub const text_delta_event: fn(Int, String) -> streaming.ContentBlockDeltaEvent
pub const timeout_error: fn(Int) -> error.AnthropicError
pub const tool_error: fn(String, String) -> message.ContentBlock
pub const tool_result: fn(String, String) -> message.ContentBlock
pub const tool_use: fn(String, String, String) -> message.ContentBlock
pub const tool_use_block_start: fn(Int, String, String) -> streaming.ContentBlockStart
pub const total_tokens: fn(accumulator.AccumulatorState) -> Int
pub const usage: fn(Int, Int) -> request.Usage
pub const usage_to_json: fn(request.Usage) -> json.Json
pub const user_message: fn(String) -> message.Message
pub const with_api_key: fn(config.ConfigOptions, String) -> config.ConfigOptions
pub const with_base_url: fn(config.ConfigOptions, String) -> config.ConfigOptions
pub const with_default_model: fn(config.ConfigOptions, String) -> config.ConfigOptions
pub const with_max_retries: fn(config.ConfigOptions, Int) -> config.ConfigOptions
pub const with_metadata: fn(
request.CreateMessageRequest,
request.Metadata,
) -> request.CreateMessageRequest
pub const with_stop_sequences: fn(
request.CreateMessageRequest,
List(String),
) -> request.CreateMessageRequest
pub const with_stream: fn(request.CreateMessageRequest, Bool) -> request.CreateMessageRequest
pub const with_system: fn(request.CreateMessageRequest, String) -> request.CreateMessageRequest
pub const with_temperature: fn(
request.CreateMessageRequest,
Float,
) -> request.CreateMessageRequest
pub const with_timeout_ms: fn(config.ConfigOptions, Int) -> config.ConfigOptions
pub const with_top_k: fn(request.CreateMessageRequest, Int) -> request.CreateMessageRequest
pub const with_top_p: fn(request.CreateMessageRequest, Float) -> request.CreateMessageRequest
pub const with_user_id: fn(request.CreateMessageRequest, String) -> request.CreateMessageRequest