starflow/api_request
Types
Represents possible errors that can occur during API requests.
Variants
NetworkError
: Connection or HTTP-related errors with error messageDecodingError
: Failed to decode request URL or response bodyInvalidResponse
: 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:
- Builds the request URL based on the provider
- Adds necessary headers
- Encodes the messages into the request body
- Sends the request
- Decodes the response
Arguments
model
: The model configuration to usemessages
: List of messages to send to the model
Returns
A Result containing either:
Ok(Response)
: The successful API responseError(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)