ai-sdk-core 0.3.0

High-level APIs for AI SDK - text generation, embeddings, and tool execution
Documentation
use ai_sdk_provider::Error as ProviderError;

/// A convenience alias for `Result` where the error type is [`Error`].
pub type Result<T> = std::result::Result<T, Error>;

/// The error type for this crate.
pub type Error = Box<dyn std::error::Error + Send + Sync>;

/// Unified error type for text generation and streaming
#[derive(thiserror::Error, Debug)]
pub enum GenerateError {
    /// Missing model - must call .model() before execute()
    #[error("Missing model - must call .model() before execute()")]
    MissingModel,

    /// Missing prompt - must call .prompt() before execute()
    #[error("Missing prompt - must call .prompt() before execute()")]
    MissingPrompt,

    /// Provider error
    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),

    /// Tool execution error
    #[error("Tool execution error: {0}")]
    ToolError(#[from] ToolError),

    /// Maximum steps reached without completion
    #[error("Maximum steps reached without completion")]
    MaxStepsReached,

    /// Invalid parameters provided
    #[error("Invalid parameters: {0}")]
    InvalidParameters(String),

    /// Stream error
    #[error("Stream error: {0}")]
    StreamError(String),

    /// Model error
    #[error("Model error: {0}")]
    ModelError(String),
}

/// Error that can occur during embedding
#[derive(thiserror::Error, Debug)]
pub enum EmbedError {
    /// Missing model - must call .model() before execute()
    #[error("Missing model - must call .model() before execute()")]
    MissingModel,

    /// Missing value - must call .value() before execute()
    #[error("Missing value - must call .value() before execute()")]
    MissingValue,

    /// Empty response from embedding model
    #[error("Empty response from embedding model")]
    EmptyResponse,

    /// Provider error
    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),
}

/// Error that can occur during tool execution
#[derive(thiserror::Error, Debug)]
pub enum ToolError {
    /// Tool execution failed
    #[error("Tool execution failed: {0}")]
    ExecutionError(String),

    /// Tool not found
    #[error("Tool not found: {0}")]
    ToolNotFound(String),

    /// Invalid tool input
    #[error("Invalid tool input: {0}")]
    InvalidInput(String),

    /// Tool execution denied
    #[error("Tool execution denied")]
    ExecutionDenied,
}

impl ToolError {
    /// Create an execution error
    pub fn execution(msg: impl Into<String>) -> Self {
        ToolError::ExecutionError(msg.into())
    }

    /// Create a not found error
    pub fn not_found(name: impl Into<String>) -> Self {
        ToolError::ToolNotFound(name.into())
    }

    /// Create an invalid input error
    pub fn invalid_input(msg: impl Into<String>) -> Self {
        ToolError::InvalidInput(msg.into())
    }
}

/// Errors that can occur during object generation.
#[derive(Debug, thiserror::Error)]
pub enum GenerateObjectError {
    /// Missing model
    #[error("Model is required")]
    MissingModel,

    /// Missing prompt
    #[error("Prompt is required")]
    MissingPrompt,

    /// Missing output strategy
    #[error("Output strategy is required")]
    MissingOutputStrategy,

    /// Provider error
    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),

    /// Validation error
    #[error("Validation failed: {0}")]
    ValidationFailed(String),

    /// JSON parsing error
    #[error("JSON parsing error: {0}")]
    JsonError(#[from] serde_json::Error),

    /// No text content in response
    #[error("No text content in model response")]
    NoTextContent,

    /// Model error
    #[error("Model error: {0}")]
    ModelError(Box<dyn std::error::Error + Send + Sync>),
}

/// Errors that can occur during streaming object generation.
#[derive(Debug, thiserror::Error)]
pub enum StreamObjectError {
    /// Missing model
    #[error("Model is required")]
    MissingModel,

    /// Missing prompt
    #[error("Prompt is required")]
    MissingPrompt,

    /// Missing output strategy
    #[error("Output strategy is required")]
    MissingOutputStrategy,

    /// Provider error
    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),

    /// Validation error
    #[error("Validation failed: {0}")]
    ValidationFailed(String),

    /// Stream error
    #[error("Stream error: {0}")]
    StreamError(String),

    /// Model error
    #[error("Model error: {0}")]
    ModelError(Box<dyn std::error::Error + Send + Sync>),
}

/// Registry-specific errors
#[derive(Debug, thiserror::Error)]
pub enum RegistryError {
    /// Provider not found in registry
    #[error("No such provider: {provider_id} (available providers: {available_providers:?})")]
    NoSuchProvider {
        /// The provider ID that was not found
        provider_id: String,
        /// The model type being requested
        model_type: String,
        /// List of available provider IDs
        available_providers: Vec<String>,
    },

    /// Model not found in provider
    #[error("No such model: {model_id} (type: {model_type})")]
    NoSuchModel {
        /// The model ID that was not found
        model_id: String,
        /// The model type being requested
        model_type: String,
    },

    /// Invalid model ID format
    #[error("Invalid model ID: {message}")]
    InvalidModelId {
        /// The invalid model ID
        model_id: String,
        /// The model type being requested
        model_type: String,
        /// Error message explaining what's wrong
        message: String,
    },
}