View Source Anthropic.Messages.Response (anthropic_community v0.4.3)

Handles parsing and encapsulation of responses from the Anthropic API.

This module defines the structure of a response from the Anthropic API and provides functionality to parse the raw response into a structured format. It deals with successful responses as well as various error conditions, translating HTTP responses into a consistent format for the caller to handle.

Summary

Functions

Parses an HTTP response from the Anthropic API.

Types

@type invocation() :: {atom(), [String.t()]}
@type t() :: %Anthropic.Messages.Response{
  content: String.t(),
  id: String.t(),
  invocations: [invocation()] | list(),
  model: String.t(),
  role: String.t(),
  stop_reason: String.t() | nil,
  stop_sequence: String.t() | nil,
  type: String.t(),
  usage: map()
}

Functions

@spec parse(
  {:error, Anthropic.Messages.Request.t()} | {:ok, Finch.Response.t()},
  Anthropic.Messages.Request.t()
) :: {:error, any()} | {:ok, struct()}

Parses an HTTP response from the Anthropic API.

This function handles different types of HTTP responses based on their status code. It extracts relevant fields from successful responses (status 200) and constructs a struct representing the parsed response. For error responses (status codes 400-499 for client errors and 500-599 for server errors), it returns an error tuple. Errors encountered during the HTTP request process are also passed through.

Parameters

  • response: A tuple containing the raw HTTP response. This can be either {:ok, %Finch.Response{}} indicating a potentially successful or error response, or {:error, response} indicating an error during the request process.

Returns

  • For successful responses (status 200), returns {:ok, response_struct} where response_struct is the struct representation of the parsed response.
  • For client errors (status codes 400-499) and server errors (status codes 500-599), returns {:error, response} where response is the original response indicating an error.
  • For errors during the request process, returns {:error, response} where response is the original error response.