anthropic/internal/decoder

Shared JSON response decoders for Anthropic API

This module provides common JSON decoders used across the library for parsing API responses. Both the high-level api.gleam and the sans-io http.gleam modules use these shared decoders.

Internal Module

This module is primarily for internal use. Most users should use the higher-level functions in anthropic/api or anthropic/http instead.

Values

pub fn content_block_decoder() -> decode.Decoder(
  message.ContentBlock,
)

Decoder for ContentBlock

pub fn decode_errors_to_string(
  errors: List(decode.DecodeError),
) -> String

Convert decode errors to a human-readable string

pub fn decode_response(
  value: dynamic.Dynamic,
) -> Result(request.CreateMessageResponse, error.AnthropicError)

Decode a dynamic value into CreateMessageResponse

pub fn input_decoder() -> decode.Decoder(String)

Decoder for tool input (converts dynamic to JSON string)

pub fn parse_api_error(
  status_code: Int,
  body: String,
  default_type: error.ApiErrorType,
) -> error.AnthropicError

Parse an API error response body into an AnthropicError

This function attempts to parse the Anthropic API error format:

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "..."
  }
}

If parsing fails, it falls back to creating an error with the default type and the raw body as the message.

pub fn parse_error_body(
  body: String,
) -> Result(
  #(
    error.ApiErrorType,
    String,
    option.Option(String),
    option.Option(String),
  ),
  Nil,
)

Parse error body JSON into error details tuple

Returns a tuple of (ApiErrorType, message, optional param, optional code)

pub fn parse_json(json: String) -> Result(dynamic.Dynamic, Nil)

Parse a JSON string to Dynamic

pub fn parse_response_body(
  body: String,
) -> Result(request.CreateMessageResponse, error.AnthropicError)

Parse response body JSON and decode into CreateMessageResponse

pub fn parse_role(str: String) -> message.Role

Parse a role string to Role type

pub fn parse_stop_reason(
  str: String,
) -> option.Option(request.StopReason)

Parse a stop reason string to Option(StopReason)

pub fn response_decoder() -> decode.Decoder(
  request.CreateMessageResponse,
)

Decoder for CreateMessageResponse

pub fn text_block_decoder() -> decode.Decoder(
  message.ContentBlock,
)

Decoder for text blocks

pub fn tool_use_block_decoder() -> decode.Decoder(
  message.ContentBlock,
)

Decoder for tool use blocks

pub fn usage_decoder() -> decode.Decoder(request.Usage)

Decoder for Usage

Search Document