anthropic/types/streaming

Streaming event types for the Anthropic Messages API

This module defines types for Server-Sent Events (SSE) from the streaming API, including message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop, ping, and error events.

Types

Union type for content block deltas

pub type ContentBlockDelta {
  TextContentDelta(delta: TextDelta)
  InputJsonContentDelta(delta: InputJsonDelta)
}

Constructors

  • TextContentDelta(delta: TextDelta)

    Text content delta

  • InputJsonContentDelta(delta: InputJsonDelta)

    Tool input JSON delta

Delta update for a content block

pub type ContentBlockDeltaEvent {
  ContentBlockDeltaEvent(index: Int, delta: ContentBlockDelta)
}

Constructors

  • ContentBlockDeltaEvent(index: Int, delta: ContentBlockDelta)

    Arguments

    index

    Index of the content block being updated

    delta

    The delta update

Information about a content block starting

pub type ContentBlockStart {
  ContentBlockStart(
    index: Int,
    content_block: message.ContentBlock,
  )
}

Constructors

  • ContentBlockStart(
      index: Int,
      content_block: message.ContentBlock,
    )

    Arguments

    index

    Index of this content block in the message

    content_block

    The initial content block (with empty/partial content)

Marks the end of a content block

pub type ContentBlockStop {
  ContentBlockStop(index: Int)
}

Constructors

  • ContentBlockStop(index: Int)

    Arguments

    index

    Index of the content block that ended

Delta type for tool input JSON streaming

pub type InputJsonDelta {
  InputJsonDelta(partial_json: String)
}

Constructors

  • InputJsonDelta(partial_json: String)

    Arguments

    partial_json

    Partial JSON string for tool input

Delta update for message-level information

pub type MessageDelta {
  MessageDelta(
    stop_reason: option.Option(request.StopReason),
    stop_sequence: option.Option(String),
  )
}

Constructors

  • MessageDelta(
      stop_reason: option.Option(request.StopReason),
      stop_sequence: option.Option(String),
    )

    Arguments

    stop_reason

    The reason generation stopped (if applicable)

    stop_sequence

    The stop sequence that triggered stopping (if applicable)

Full message delta event with usage

pub type MessageDeltaEvent {
  MessageDeltaEvent(
    delta: MessageDelta,
    usage: MessageDeltaUsage,
  )
}

Constructors

Usage information in message delta

pub type MessageDeltaUsage {
  MessageDeltaUsage(output_tokens: Int)
}

Constructors

  • MessageDeltaUsage(output_tokens: Int)

    Arguments

    output_tokens

    Number of output tokens generated

Initial message information sent at stream start

pub type MessageStart {
  MessageStart(
    id: String,
    message_type: String,
    role: message.Role,
    model: String,
    usage: request.Usage,
  )
}

Constructors

  • MessageStart(
      id: String,
      message_type: String,
      role: message.Role,
      model: String,
      usage: request.Usage,
    )

    Arguments

    id

    Unique identifier for this message

    message_type

    Object type, always “message”

    role

    Role of the response, always “assistant”

    model

    Model that is generating the response

    usage

    Initial usage information (input tokens)

Error information from a streaming error event

pub type StreamError {
  StreamError(error_type: String, message: String)
}

Constructors

  • StreamError(error_type: String, message: String)

    Arguments

    error_type

    Error type string

    message

    Error message

All possible streaming events from the API

pub type StreamEvent {
  MessageStartEvent(message: MessageStart)
  ContentBlockStartEvent(content_block_start: ContentBlockStart)
  ContentBlockDeltaEventVariant(
    content_block_delta: ContentBlockDeltaEvent,
  )
  ContentBlockStopEvent(content_block_stop: ContentBlockStop)
  MessageDeltaEventVariant(message_delta: MessageDeltaEvent)
  MessageStopEvent
  PingEvent
  ErrorEvent(error: StreamError)
}

Constructors

  • MessageStartEvent(message: MessageStart)

    Initial message information

  • ContentBlockStartEvent(content_block_start: ContentBlockStart)

    Start of a content block

  • ContentBlockDeltaEventVariant(
      content_block_delta: ContentBlockDeltaEvent,
    )

    Delta update for a content block

  • ContentBlockStopEvent(content_block_stop: ContentBlockStop)

    End of a content block

  • MessageDeltaEventVariant(message_delta: MessageDeltaEvent)

    Delta update for message-level information

  • MessageStopEvent

    End of the message

  • PingEvent

    Keepalive ping

  • ErrorEvent(error: StreamError)

    Error during streaming

Delta type for text content streaming

pub type TextDelta {
  TextDelta(text: String)
}

Constructors

  • TextDelta(text: String)

    Arguments

    text

    The text fragment being streamed

Values

pub fn content_block_delta_to_json(
  delta: ContentBlockDelta,
) -> json.Json

Encode a ContentBlockDelta to JSON

pub fn content_block_stop(index: Int) -> ContentBlockStop

Create a ContentBlockStop

pub fn event_type_from_string(
  str: String,
) -> Result(String, String)

Parse an event type string

pub fn event_type_string(event: StreamEvent) -> String

Get the event type string for a StreamEvent

pub fn get_delta_json(
  delta: ContentBlockDelta,
) -> option.Option(String)

Get the partial JSON from a content block delta, if it’s an input JSON delta

pub fn get_delta_text(
  delta: ContentBlockDelta,
) -> option.Option(String)

Get the text from a content block delta, if it’s a text delta

pub fn input_json_delta(partial_json: String) -> InputJsonDelta

Create an InputJsonDelta

pub fn input_json_delta_event(
  index: Int,
  partial_json: String,
) -> ContentBlockDeltaEvent

Create a ContentBlockDeltaEvent for tool input JSON

pub fn input_json_delta_to_json(
  delta: InputJsonDelta,
) -> json.Json

Encode an InputJsonDelta to JSON

pub fn is_terminal_event(event: StreamEvent) -> Bool

Check if an event is a terminal event (message_stop or error)

pub fn message_delta_event(
  stop_reason: option.Option(request.StopReason),
  stop_sequence: option.Option(String),
  output_tokens: Int,
) -> MessageDeltaEvent

Create a MessageDeltaEvent

pub fn message_start(
  id: String,
  role: message.Role,
  model: String,
  input_tokens: Int,
) -> MessageStart

Create a MessageStart

pub fn stream_error(
  error_type: String,
  message: String,
) -> StreamError

Create a StreamError

pub fn stream_error_to_json(error: StreamError) -> json.Json

Encode a StreamError to JSON

pub fn text_block_start(index: Int) -> ContentBlockStart

Create a ContentBlockStart for a text block

pub fn text_delta(text: String) -> TextDelta

Create a TextDelta

pub fn text_delta_event(
  index: Int,
  text: String,
) -> ContentBlockDeltaEvent

Create a ContentBlockDeltaEvent for text

pub fn text_delta_to_json(delta: TextDelta) -> json.Json

Encode a TextDelta to JSON

pub fn tool_use_block_start(
  index: Int,
  id: String,
  name: String,
) -> ContentBlockStart

Create a ContentBlockStart for a tool use block

Search Document