starflow/api_request

Types

Represents possible errors that can occur during API requests.

Variants

  • NetworkError: Connection or HTTP-related errors with error message
  • DecodingError: Failed to decode request URL or response body
  • InvalidResponse: Received non-200 status code with status and body

Examples

case create_message(model, messages) {
  Error(NetworkError(msg)) -> // Handle network failure
  Error(DecodingError) -> // Handle malformed response
  Error(InvalidResponse(status, body)) -> // Handle API error response
  Ok(response) -> // Handle successful response
}
pub type APIError {
  NetworkError(String)
  DecodingError
  InvalidResponse(Int, String)
}

Constructors

  • NetworkError(String)
  • DecodingError
  • InvalidResponse(Int, String)

Functions

pub fn create_message(
  model: Model,
  messages: List(Message),
  tools: List(Tool),
) -> Result(Response, APIError)

Creates a new message by sending a request to the model’s API.

This function:

  1. Builds the request URL based on the provider
  2. Adds necessary headers
  3. Encodes the messages into the request body
  4. Sends the request
  5. Decodes the response

Arguments

  • model: The model configuration to use
  • messages: List of messages to send to the model

Returns

A Result containing either:

  • Ok(Response): The successful API response
  • Error(APIError): An error that occurred during the request

Examples

// Simple message creation
let messages = [
  state.Message(
    role: "user",
    content: [state.TextContent("Hello!")],
  )
]

case create_message(model, messages) {
  Ok(response) -> handle_response(response)
  Error(error) -> handle_error(error)
}

// With image content
let messages = [
  state.Message(
    role: "user",
    content: [
      state.TextContent("What's in this image?"),
      state.ImageContent(source: image_source),
    ],
  )
]

create_message(model, messages)
Search Document