rig 0.46.0

An opinionated library for building LLM powered applications.
Documentation
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Public facade for Rig.
//!
//! Re-exports `rig_core` at `rig::...` paths and, under the default `agent`
//! feature, the runtime from `rig_agent` at `rig::agent`. `rig::tool` then
//! carries the agent's tool registry alongside the `rig-core` tool contracts,
//! which are always available. `use rig::prelude::*;` brings in
//! [`Model`], [`DynModel`] and, with `agent`, the common agent types.
//!
//! Companion provider and vector-store crates are feature-gated modules, named
//! after their features wherever module naming allows:
//!
//! ```toml
//! [dependencies]
//! rig = { version = "*", features = ["lancedb", "fastembed"] }
//! serde_json = { version = "1", features = ["preserve_order", "float_roundtrip"] }
//! ```
//!
//! Depend on `rig-core` directly to skip this facade's companion integrations.
//! Core does not force serde_json's `preserve_order` or `float_roundtrip`; the
//! application dependency above restores the previous configuration. Without
//! those features, schema properties and JSON text in prompts, embeddings and
//! tool results use sorted object keys, and parsed floats can differ in their
//! low bits. Cargo unifies features: `rig-cassette` with `traffic` (including
//! `rig-traffic`) enables both; `rig-claude-cloak`, `rig-mcp-client` and
//! `rig-mongodb` through BSON enable `preserve_order`. Rig has no forwarding feature for either setting.

pub use rig_core::*;

/// The bundled `reqwest` transport (`rig-reqwest`). A provider client built
/// with no transport named (`OpenAI::from_env()`) sends through its shared
/// client; a config's `connect` builds one on any other `HttpClientExt`.
#[cfg(feature = "reqwest")]
#[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
pub use rig_reqwest;

/// The bundled `tokio-tungstenite` websocket backend (`rig-tungstenite`), on
/// native targets. With the `websocket` feature,
/// `model.responses_websocket().connect()` opens a session over it with no
/// backend named; without it, rig has no websocket backend and a session is
/// opened with `responses_websocket().connect_with(..)` and any
/// [`rig_core::ws_client::WebSocketClientExt`] implementation.
#[cfg(all(feature = "websocket", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
pub use rig_tungstenite;

/// Transport-agnostic HTTP contracts, plus the bundled reqwest transport type.
pub mod http_client {
    pub use rig_core::http_client::*;
    #[cfg(feature = "reqwest")]
    #[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
    pub use rig_reqwest::ReqwestClient;
}

#[cfg(feature = "agent")]
#[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
pub use rig_agent::{Agent, AgentBuilder, AgentRun, AgentRunner, TypedPromptResponse};

#[doc(hidden)]
pub use rig_core as __core;

/// The classic runtime's handler table and typed views
/// (`rig_agent::handlers`). What a handler implements is
/// `rig::serve`; the vocabulary is `rig::effect`.
#[cfg(feature = "agent")]
#[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
pub use rig_agent::handlers;

/// Effect-log recording and replay; optional runtime and native HTTP integrations.
#[cfg(feature = "cassette")]
#[cfg_attr(docsrs, doc(cfg(feature = "cassette")))]
pub use rig_cassette as cassette;

/// The sans-IO run layer of rig-agent (`rig_agent::run`): `AgentRun` and its
/// step/turn types, the run's spec, request preparation, output policy and
/// per-turn patch, its response and error types, the invalid-call decision
/// data, the streamed-turn assembler and the loop-side transcript helpers.
/// rig-core keeps only the message-model invariants (`rig::conversation::pairing`).
#[cfg(feature = "agent")]
#[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
pub mod run {
    pub use rig_agent::run::*;
}

/// Classic agent orchestration and lifecycle APIs.
#[cfg(feature = "agent")]
#[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
pub mod agent {
    pub use rig_agent::agent::*;

    /// Contextual tools for the classic agent runtime.
    pub mod tool {
        pub use rig_agent::tool::*;
    }
}

/// Low-level completion contracts plus the classic runtime's errors.
pub mod completion {
    #[cfg(feature = "agent")]
    pub use rig_agent::completion::{PromptError, StructuredOutputError};
    pub use rig_core::completion::*;
}

/// Classic typed extraction.
#[cfg(feature = "agent")]
#[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
pub mod extractor {
    pub use rig_agent::extractor::*;
}

/// Classic runtime integrations.
#[cfg(feature = "agent")]
#[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
pub mod integrations {
    pub use rig_agent::integrations::*;
}

/// Common portable imports plus additive classic-runtime conveniences.
pub mod prelude {
    // The contextual `Tool` and its mutable `ToolContext`.
    #[cfg(feature = "agent")]
    pub use crate::tool::{Tool, ToolContext};
    #[cfg(feature = "agent")]
    pub use rig_agent::prelude::{
        Agent, AgentBuilder, MultiTurnStreamItem, PromptError, RunEvents, StreamingResult,
        StructuredOutputError, ToolSet,
    };
    pub use rig_core::prelude::*;
}

/// Tools: contextual authoring, the erased tool set, and the live registry.
///
/// The tool contracts come from `rig-core` and need no
/// feature. The registry, tool set, and catalog belong to the agent runtime and
/// need the `agent` feature; they also live at `rig::agent::tool`.
pub mod tool {
    /// Derive a stable serialized key for a tool-context value.
    #[cfg(feature = "derive")]
    #[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
    pub use rig_derive::ContextValue;

    #[cfg(feature = "agent")]
    #[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
    pub use rig_agent::tool::{RegisteredTool, ToolCatalog, ToolSet};
    pub use rig_core::tool::builtin;
    pub use rig_core::tool::{
        ContextValue, DynamicTool, Tool, ToolContext, ToolContextError, ToolEmbedding,
        tool_definition, tool_name,
    };
    pub use rig_core::tool::{
        IntoToolOutput, ToolErrorKind, ToolExecutionError, ToolOutput, ToolResult,
    };

    /// MCP tool support from `rig-rmcp`, which supports native targets only.
    #[cfg(all(feature = "rmcp", not(target_family = "wasm")))]
    #[cfg_attr(docsrs, doc(cfg(feature = "rmcp")))]
    pub mod rmcp {
        pub use rig_rmcp::*;
    }
    /// The live registry, layered over the contracts above.
    #[cfg(feature = "agent")]
    #[cfg_attr(docsrs, doc(cfg(feature = "agent")))]
    pub use rig_agent::tool::server;
}

/// rig-agent's helpers for tests of code built on it.
#[cfg(all(feature = "agent", any(test, feature = "test-utils")))]
#[cfg_attr(docsrs, doc(cfg(feature = "test-utils")))]
pub mod test_utils {
    pub use rig_agent::test_utils::*;
}

#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(cfg(feature = "derive")))]
pub use rig_derive::rig_tool;

/// One conversation: its entries with stable ids and authors, its asides,
/// its halt, and the changes made to it that nobody has saved yet.
pub use rig_core::conversation;

/// Conversations over time (`rig-memory`): windows, compaction, and saving
/// and resuming them in the `rig-session` format.
#[cfg(feature = "memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "memory")))]
pub use rig_memory as memory;

/// Declares one feature-gated facade module per companion crate, each row giving
/// the module, its crate, and the features enabling it.
///
/// The single-feature arm is separate so rustdoc renders "Available on crate
/// feature `x` only" rather than a one-element `any`.
macro_rules! companion_modules {
    () => {};
    (
        $(#[doc = $doc:literal])*
        $module:ident = $krate:ident [$feature:literal];
        $($rest:tt)*
    ) => {
        $(#[doc = $doc])*
        #[cfg(feature = $feature)]
        #[cfg_attr(docsrs, doc(cfg(feature = $feature)))]
        pub mod $module {
            pub use $krate::*;
        }
        companion_modules! { $($rest)* }
    };
    (
        $(#[doc = $doc:literal])*
        $module:ident = $krate:ident [$($feature:literal),+ $(,)?];
        $($rest:tt)*
    ) => {
        $(#[doc = $doc])*
        #[cfg(any($(feature = $feature),+))]
        #[cfg_attr(docsrs, doc(cfg(any($(feature = $feature),+))))]
        pub mod $module {
            pub use $krate::*;
        }
        companion_modules! { $($rest)* }
    };
}

companion_modules! {
    /// Typed TypeSafe Jev judgments for routing and rubric evaluation.
    typesafeai = rig_typesafeai ["typesafeai"];
    bedrock = rig_bedrock ["bedrock"];
    /// Local CPU inference with validated Llama/SmolLM2 and native tool-capable Qwen3 models,
    /// and YOLOv8 pose estimation.
    candle = rig_candle ["candle"];
    fastembed = rig_fastembed [
        "fastembed",
        "fastembed-hf-hub",
        "fastembed-ort-download-binaries",
    ];
    gemini_grpc = rig_gemini_grpc ["gemini-grpc"];
    helixdb = rig_helixdb ["helixdb"];
    lancedb = rig_lancedb ["lancedb"];
    milvus = rig_milvus ["milvus"];
    mongodb = rig_mongodb ["mongodb"];
    neo4j = rig_neo4j ["neo4j"];
    postgres = rig_postgres ["postgres"];
    qdrant = rig_qdrant ["qdrant"];
    s3vectors = rig_s3vectors ["s3vectors"];
    scylladb = rig_scylladb ["scylladb"];
    sqlite = rig_sqlite ["sqlite"];
    surrealdb = rig_surrealdb ["surrealdb"];
    vectorize = rig_vectorize ["vectorize"];
    vertexai = rig_vertexai ["vertexai"];
}