anthropic/streaming/handler

Streaming HTTP handler for Anthropic API

This module provides the HTTP handling for streaming responses from the Anthropic Messages API. It processes Server-Sent Events (SSE) and yields parsed streaming events.

Note: Currently uses synchronous HTTP with SSE parsing. True streaming with real-time chunk processing can be added via Erlang interop if needed.

Types

Callback function type for processing events as they are parsed

pub type EventCallback =
  fn(streaming.StreamEvent) -> Nil

Error during streaming

pub type StreamError {
  HttpError(error: error.AnthropicError)
  SseParseError(message: String)
  EventDecodeError(message: String)
  ApiError(status: Int, body: String)
}

Constructors

  • HttpError(error: error.AnthropicError)

    HTTP error during request

  • SseParseError(message: String)

    Error parsing SSE data

  • EventDecodeError(message: String)

    Error decoding event JSON

  • ApiError(status: Int, body: String)

    API returned an error response

Result of streaming a message

pub type StreamResult {
  StreamResult(events: List(streaming.StreamEvent))
}

Constructors

Values

pub fn get_error(
  events: List(streaming.StreamEvent),
) -> Result(streaming.StreamError, Nil)

Get error from events if present

pub fn get_full_text(
  events: List(streaming.StreamEvent),
) -> String

Get the full text from a stream of events

pub fn get_message_id(
  events: List(streaming.StreamEvent),
) -> Result(String, Nil)

Get the message ID from events

pub fn get_model(
  events: List(streaming.StreamEvent),
) -> Result(String, Nil)

Get the model from events

pub fn get_text_deltas(
  events: List(streaming.StreamEvent),
) -> List(String)

Filter events to only text deltas

pub fn has_error(events: List(streaming.StreamEvent)) -> Bool

Check if stream ended with an error

pub fn is_complete(events: List(streaming.StreamEvent)) -> Bool

Check if stream completed successfully

pub fn stream_message(
  api_client: client.Client,
  message_request: request.CreateMessageRequest,
) -> Result(StreamResult, StreamError)

Stream a message request and return all events

This function sends a streaming request to the Anthropic API, processes the SSE response, and returns all parsed events.

Example

let request = create_request(model, messages, max_tokens)
  |> with_stream(True)

case stream_message(client, request) {
  Ok(result) -> {
    list.each(result.events, fn(event) {
      io.println(event_type_string(event))
    })
  }
  Error(err) -> handle_error(err)
}
pub fn stream_message_with_callback(
  api_client: client.Client,
  message_request: request.CreateMessageRequest,
  callback: fn(streaming.StreamEvent) -> Nil,
) -> Result(StreamResult, StreamError)

Stream a message request with a callback for each event

This function is similar to stream_message but calls the provided callback function for each event as it is parsed.

Example

stream_message_with_callback(client, request, fn(event) {
  case event {
    ContentBlockDeltaEventVariant(delta) -> {
      case delta.delta {
        TextContentDelta(text_delta) -> {
          io.print(text_delta.text)
        }
        _ -> Nil
      }
    }
    _ -> Nil
  }
})
Search Document